spread.js 564 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. /**
  3. * Syntactic sugar for invoking a function and expanding an array for arguments.
  4. *
  5. * Common use case would be to use `Function.prototype.apply`.
  6. *
  7. * ```js
  8. * function f(x, y, z) {}
  9. * var args = [1, 2, 3];
  10. * f.apply(null, args);
  11. * ```
  12. *
  13. * With `spread` this example can be re-written.
  14. *
  15. * ```js
  16. * spread(function(x, y, z) {})([1, 2, 3]);
  17. * ```
  18. *
  19. * @param {Function} callback
  20. * @returns {Function}
  21. */
  22. module.exports = function spread(callback) {
  23. return function wrap(arr) {
  24. return callback.apply(null, arr);
  25. };
  26. };