From 6dd8fb12ad23bff70c08bb40ffeaf7ca75617468 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 1 Aug 2016 16:55:12 -0700 Subject: [PATCH] Ensure `fp.nthArg` returns a curried function. --- fp/_baseConvert.js | 12 ++++++++++-- fp/_mapping.js | 8 ++++---- lib/fp/template/modules/_util.jst | 1 + test/test-fp.js | 20 ++++++++++++++++++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index d294820d8..0def5f67c 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -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) { diff --git a/fp/_mapping.js b/fp/_mapping.js index cb36eedad..dd4abcb55 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -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', diff --git a/lib/fp/template/modules/_util.jst b/lib/fp/template/modules/_util.jst index d450396fc..f8148129e 100644 --- a/lib/fp/template/modules/_util.jst +++ b/lib/fp/template/modules/_util.jst @@ -10,5 +10,6 @@ module.exports = { 'keys': require('../_baseKeys'), 'rearg': require('../rearg'), 'spread': require('../spread'), + 'toInteger': require('../toInteger'), 'toPath': require('../toPath') }; diff --git a/test/test-fp.js b/test/test-fp.js index ce1f56f52..f06ac71c3 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -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);