curry.js 784 B

123456789101112131415161718192021222324
  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, apply = Function.prototype.apply
  6. , curry;
  7. curry = function self(fn, length, preArgs) {
  8. return defineLength(function () {
  9. var args = preArgs ?
  10. preArgs.concat(slice.call(arguments, 0, length - preArgs.length)) :
  11. slice.call(arguments, 0, length);
  12. return (args.length === length) ? apply.call(fn, this, args) :
  13. self(fn, length, args);
  14. }, preArgs ? (length - preArgs.length) : length);
  15. };
  16. module.exports = function (/*length*/) {
  17. var length = arguments[0];
  18. return curry(callable(this),
  19. isNaN(length) ? toPosInt(this.length) : toPosInt(length));
  20. };