curry.js 801 B

12345678910111213141516171819202122232425
  1. "use strict";
  2. var toPosInt = require("../../number/to-pos-integer")
  3. , callable = require("../../object/valid-callable")
  4. , defineLength = require("../_define-length")
  5. , slice = Array.prototype.slice
  6. , apply = Function.prototype.apply
  7. , curry;
  8. curry = function self(fn, length, preArgs) {
  9. return defineLength(
  10. function () {
  11. var args = preArgs
  12. ? preArgs.concat(slice.call(arguments, 0, length - preArgs.length))
  13. : slice.call(arguments, 0, length);
  14. return args.length === length ? apply.call(fn, this, args) : self(fn, length, args);
  15. },
  16. preArgs ? length - preArgs.length : length
  17. );
  18. };
  19. module.exports = function (/* Length*/) {
  20. var length = arguments[0];
  21. return curry(callable(this), isNaN(length) ? toPosInt(this.length) : toPosInt(length));
  22. };