Ensure fp.nthArg returns a curried function.

This commit is contained in:
John-David Dalton
2016-08-01 16:55:12 -07:00
parent a4999575d0
commit 6dd8fb12ad
4 changed files with 33 additions and 8 deletions

View File

@@ -143,6 +143,7 @@ function baseConvert(util, name, func, options) {
'keys': util.keys,
'rearg': util.rearg,
'spread': util.spread,
'toInteger': util.toInteger,
'toPath': util.toPath
};
@@ -156,6 +157,7 @@ function baseConvert(util, name, func, options) {
keys = helpers.keys,
rearg = helpers.rearg,
spread = helpers.spread,
toInteger = helpers.toInteger,
toPath = helpers.toPath;
var aryMethodKeys = keys(mapping.aryMethod);
@@ -209,10 +211,16 @@ function baseConvert(util, name, func, options) {
return func;
};
},
'nthArg': function(nthArg) {
return function(n) {
var arity = n < 0 ? 1 : (toInteger(n) + 1);
return curry(nthArg(n), arity);
};
},
'rearg': function(rearg) {
return function(func, indexes) {
var n = indexes ? indexes.length : 0;
return curry(rearg(func, indexes), n);
var arity = indexes ? indexes.length : 0;
return curry(rearg(func, indexes), arity);
};
},
'runInContext': function(runInContext) {

View File

@@ -73,10 +73,10 @@ exports.aryMethod = {
'1': [
'assignAll', 'assignInAll', 'attempt', 'castArray', 'ceil', 'create',
'curry', 'curryRight', 'defaultsAll', 'defaultsDeepAll', 'floor', 'flow',
'flowRight', 'fromPairs', 'invert', 'iteratee', 'memoize', 'method',
'mergeAll', 'methodOf', 'mixin', 'over', 'overEvery', 'overSome', 'rest',
'reverse', 'round', 'runInContext', 'spread', 'template', 'trim', 'trimEnd',
'trimStart', 'uniqueId', 'words', 'zipAll'
'flowRight', 'fromPairs', 'invert', 'iteratee', 'memoize', 'method', 'mergeAll',
'methodOf', 'mixin', 'nthArg', 'over', 'overEvery', 'overSome','rest', 'reverse',
'round', 'runInContext', 'spread', 'template', 'trim', 'trimEnd', 'trimStart',
'uniqueId', 'words', 'zipAll'
],
'2': [
'add', 'after', 'ary', 'assign', 'assignAllWith', 'assignIn', 'assignInAllWith',

View File

@@ -10,5 +10,6 @@ module.exports = {
'keys': require('../_baseKeys'),
'rearg': require('../rearg'),
'spread': require('../spread'),
'toInteger': require('../toInteger'),
'toPath': require('../toPath')
};

View File

@@ -371,7 +371,7 @@
'method', 'methodOf', 'rest', 'runInContext'
];
var exceptions = funcMethods.concat('mixin', 'template'),
var exceptions = funcMethods.concat('mixin', 'nthArg', 'template'),
expected = _.map(mapping.aryMethod[1], _.constant(true));
var actual = _.map(mapping.aryMethod[1], function(methodName) {
@@ -1588,6 +1588,22 @@
/*--------------------------------------------------------------------------*/
QUnit.module('fp.nthArg');
(function() {
QUnit.test('should return a curried function', function(assert) {
assert.expect(2);
var func = fp.nthArg(1);
assert.strictEqual(func(1)(2), 2);
func = fp.nthArg(-1);
assert.strictEqual(func(1), 1);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('fp.over');
(function() {
@@ -1793,7 +1809,7 @@
assert.deepEqual(rearged('c', 'a', 'b'), ['a', 'b', 'c']);
});
QUnit.test('should curry the rearged function', function(assert) {
QUnit.test('should return a curried function', function(assert) {
assert.expect(1);
var rearged = fp.rearg([1, 2, 0], fn);