function.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. var toArray = require('./array').toArray;
  3. var argsToArray = function argsToArray(fn) {
  4. // must not be a fat arrow since it breaks the arguments
  5. return function () {
  6. return fn(toArray(arguments));
  7. };
  8. };
  9. var curry = function curry(fn) {
  10. return function curried() {
  11. var that = this;
  12. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  13. args[_key] = arguments[_key];
  14. }
  15. if (args.length < fn.length) {
  16. return curried.bind.apply(curried, [that].concat(args));
  17. } else {
  18. return fn.apply(that, args);
  19. }
  20. };
  21. };
  22. var combine = argsToArray(function (fns) {
  23. return fns.reverse().reduce(function (fnc, fn) {
  24. return function (val) {
  25. return fnc(fn(val));
  26. };
  27. });
  28. });
  29. // cannot use fat arrow function since we don't get the arguments, then
  30. var promisify = function promisify(fn) {
  31. var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  32. return function () {
  33. // copy passed arguments to a new array
  34. var args = toArray(arguments);
  35. return new Promise(function (resolve, reject) {
  36. // add a callback to the arguments, which resolves the promise with the result
  37. args.push(function (err, data) {
  38. if (err) {
  39. reject(err);
  40. } else {
  41. resolve(data);
  42. }
  43. });
  44. // call the original function with our callback flavoured arguments
  45. fn.apply(context, args);
  46. });
  47. };
  48. };
  49. /**
  50. returns a function which executes promises one after another
  51. @param promiseGenerators an array of functions which return a promise
  52. @return function which executes the promises
  53. */
  54. var enqueue = function enqueue(promiseGenerators) {
  55. return promiseGenerators.reduce(function (f, promiseGenerator) {
  56. return function () {
  57. return f().then(promiseGenerator);
  58. };
  59. });
  60. };
  61. /**
  62. Returns a function which executes promises one after another. The resulting function
  63. returns a promise, which gets filled with an array of the results of the single promises.
  64. @param promiseGenerators an array of functions which return a promise
  65. @return function which executes the promises
  66. */
  67. var enqueueWithResults = function () {
  68. var fnQueue = function fnQueue(results, promiseGenerators) {
  69. return promiseGenerators.reduce(function (f, promiseGenerator) {
  70. return function () {
  71. return f().then(function (result) {
  72. results.push(result);
  73. return promiseGenerator();
  74. });
  75. };
  76. });
  77. };
  78. return function (promiseGenerators) {
  79. return function () {
  80. var results = [];
  81. return fnQueue(results, promiseGenerators)().then(function (result) {
  82. results.push(result);
  83. return results;
  84. });
  85. };
  86. };
  87. }();
  88. module.exports = {
  89. argsToArray: argsToArray,
  90. combine: combine,
  91. curry: curry,
  92. promisify: promisify,
  93. enqueue: enqueue,
  94. enqueueWithResults: enqueueWithResults
  95. };