spread.js 564 B

12345678910111213141516171819202122232425262728
  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. *
  21. * @returns {Function}
  22. */
  23. export default function spread(callback) {
  24. return function wrap(arr) {
  25. return callback.apply(null, arr);
  26. };
  27. }