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