diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index 5e71b64d8..d116bc9fe 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -167,17 +167,17 @@ function baseConvert(util, name, func) { each(mapping.caps, function(cap) { each(mapping.aryMethod[cap], function(otherName) { if (name == otherName) { - var indexes = mapping.iterateeRearg[name], - n = !isLib && mapping.aryIteratee[name]; + var aryN = !isLib && mapping.iterateeAry[name], + reargIndexes = mapping.iterateeRearg[name]; result = ary(func, cap); if (cap > 1 && !mapping.skipRearg[name]) { result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[cap]); } - if (indexes) { - result = iterateeRearg(result, indexes); - } else if (n) { - result = iterateeAry(result, n); + if (reargIndexes) { + result = iterateeRearg(result, reargIndexes); + } else if (aryN) { + result = iterateeAry(result, aryN); } if (cap > 1) { result = curry(result, cap); @@ -202,7 +202,7 @@ function baseConvert(util, name, func) { var pairs = []; each(mapping.caps, function(cap) { each(mapping.aryMethod[cap], function(key) { - var func = _[mapping.key[key] || key]; + var func = _[mapping.rekey[key] || key]; if (func) { pairs.push([key, wrap(key, func)]); } diff --git a/fp/_mapping.js b/fp/_mapping.js index c71d7724a..bed8bde5f 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -35,8 +35,59 @@ exports.aliasToReal = { 'zipObj': 'zipObject' }; +/** Used to map ary to method names. */ +exports.aryMethod = { + 1: [ + 'attempt', 'ceil', 'create', 'curry', 'curryRight', 'floor', 'fromPairs', + 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', 'over', + 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext', + 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words' + ], + 2: [ + 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindKey', + 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN', + 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference', + 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', 'every', + 'filter', 'find', 'find', 'findIndex', 'findKey', 'findLast', 'findLastIndex', + 'findLastKey', 'flatMap', 'forEach', 'forEachRight', 'forIn', 'forInRight', + 'forOwn', 'forOwnRight', 'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', + 'includes', 'indexOf', 'intersection', 'invertBy', 'invoke', 'invokeMap', + 'isEqual', 'isMatch', 'join', 'keyBy', 'lastIndexOf', 'lt', 'lte', 'map', + 'mapKeys', 'mapValues', 'matchesProperty', 'maxBy', 'merge', 'minBy', 'omit', + 'omitBy', 'orderBy', 'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt', + 'partial', 'partialRight', 'partition', 'pick', 'pickBy', 'pull', 'pullAll', + 'pullAt', 'random', 'range', 'rangeRight', 'rearg', 'reject', 'remove', + 'repeat', 'result', 'sampleSize', 'some', 'sortBy', 'sortedIndex', + 'sortedIndexOf', 'sortedLastIndex', 'sortedLastIndexOf', 'sortedUniqBy', + 'split', 'startsWith', 'subtract', 'sumBy', 'take', 'takeRight', 'takeRightWhile', + 'takeWhile', 'tap', 'throttle', 'thru', 'times', 'truncate', 'union', 'uniqBy', + 'uniqWith', 'unset', 'unzipWith', 'without', 'wrap', 'xor', 'zip', 'zipObject', + 'zipObjectDeep' + ], + 3: [ + 'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith', + 'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith', + 'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce', 'reduceRight', 'replace', + 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'transform', 'unionBy', + 'unionWith', 'xorBy', 'xorWith', 'zipWith' + ], + 4: [ + 'fill', 'setWith' + ] +}; + +/** Used to map ary to rearg configs. */ +exports.aryRearg = { + 2: [1, 0], + 3: [2, 1, 0], + 4: [3, 2, 0, 1] +}; + +/** Used to iterate `mapping.aryMethod` keys. */ +exports.caps = [1, 2, 3, 4]; + /** Used to map method names to their iteratee ary. */ -exports.aryIteratee = { +exports.iterateeAry = { 'assignWith': 2, 'assignInWith': 2, 'cloneDeepWith': 1, @@ -75,53 +126,6 @@ exports.aryIteratee = { 'transform': 2 }; -/** Used to map ary to method names. */ -exports.aryMethod = { - 1: [ - 'attempt', 'ceil', 'create', 'curry', 'curryRight', 'floor', 'fromPairs', - 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', 'over', - 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext', - 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words' - ], - 2: [ - 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindKey', - 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN', - 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference', - 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', 'every', - 'filter', 'find', 'find', 'findIndex', 'findKey', 'findLast', 'findLastIndex', - 'findLastKey', 'flatMap', 'forEach', 'forEachRight', 'forIn', 'forInRight', - 'forOwn', 'forOwnRight', 'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', - 'includes', 'indexOf', 'intersection', 'invertBy', 'invoke', 'invokeMap', - 'isEqual', 'isMatch', 'join', 'keyBy', 'lastIndexOf', 'lt', 'lte', 'map', - 'mapKeys', 'mapValues', 'matchesProperty', 'maxBy', 'merge', 'minBy', 'omit', - 'omitBy', 'orderBy', 'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt', - 'partition', 'pick', 'pickBy', 'pull', 'pullAll', 'pullAt', 'random', 'range', - 'rangeRight', 'rearg', 'reject', 'remove', 'repeat', 'result', 'sampleSize', - 'some', 'sortBy', 'sortedIndex', 'sortedIndexOf', 'sortedLastIndex', - 'sortedLastIndexOf', 'sortedUniqBy', 'split', 'startsWith', 'subtract', - 'sumBy', 'take', 'takeRight', 'takeRightWhile', 'takeWhile', 'tap', 'throttle', - 'thru', 'times', 'truncate', 'union', 'uniqBy', 'uniqWith', 'unset', 'unzipWith', - 'without', 'wrap', 'xor', 'zip', 'zipObject', 'zipObjectDeep' - ], - 3: [ - 'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith', - 'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith', - 'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce', 'reduceRight', 'replace', - 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'transform', 'unionBy', - 'unionWith', 'xorBy', 'xorWith', 'zipWith' - ], - 4: [ - 'fill', 'setWith' - ] -}; - -/** Used to map ary to rearg configs. */ -exports.aryRearg = { - 2: [1, 0], - 3: [2, 1, 0], - 4: [3, 2, 0, 1] -}; - /** Used to map method names to iteratee rearg configs. */ exports.iterateeRearg = { 'findKey': [1], @@ -143,16 +147,6 @@ exports.methodRearg = { 'transform': [2, 0, 1] }; -/** Used to iterate `mapping.aryMethod` keys. */ -exports.caps = [1, 2, 3, 4]; - -/** Used to map keys to other keys. */ -exports.key = { - 'curryN': 'curry', - 'curryRightN': 'curryRight', - 'getOr': 'get' -}; - /** Used to identify methods which mutate arrays or objects. */ exports.mutate = { 'array': { @@ -208,6 +202,13 @@ exports.realToAlias = (function() { return result; }()); +/** Used to map keys to other keys. */ +exports.rekey = { + 'curryN': 'curry', + 'curryRightN': 'curryRight', + 'getOr': 'get' +}; + /** Used to track methods that skip `_.rearg`. */ exports.skipRearg = { 'assign': true, @@ -216,6 +217,8 @@ exports.skipRearg = { 'difference': true, 'matchesProperty': true, 'merge': true, + 'partial': true, + 'partialRight': true, 'random': true, 'range': true, 'rangeRight': true, diff --git a/test/test-fp.js b/test/test-fp.js index cee1baa49..e2cca7201 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -96,7 +96,7 @@ var aryCap = index + 1; var methodNames = _.filter(mapping.aryMethod[aryCap], function(methodName) { - var key = _.result(mapping.key, methodName, methodName), + var key = _.result(mapping.rekey, methodName, methodName), arity = _[key].length; return arity != 0 && arity < aryCap; @@ -160,8 +160,8 @@ assert.expect(1); var funcMethods = [ - 'after', 'ary', 'before', 'bind', 'bindKey', 'cloneDeepWith', 'cloneWith', - 'curryN', 'debounce', 'delay', 'overArgs', 'rearg', 'throttle', 'wrap' + 'after', 'ary', 'before', 'bind', 'bindKey', 'curryN', 'debounce', 'delay', + 'overArgs', 'rearg', 'throttle', 'wrap' ]; var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']), @@ -341,35 +341,6 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('curry methods'); - - _.each(['curry', 'curryRight'], function(methodName) { - var func = fp[methodName]; - - QUnit.test('`_.' + methodName + '` should only accept a `func` param', function(assert) { - assert.expect(1); - - assert.raises(function() { func(1, _.noop); }, TypeError); - }); - }); - - /*--------------------------------------------------------------------------*/ - - QUnit.module('curryN methods'); - - _.each(['curryN', 'curryRightN'], function(methodName) { - var func = fp[methodName]; - - QUnit.test('`_.' + methodName + '` accept an `arity` param', function(assert) { - assert.expect(1); - - var actual = func(1, function(a, b) { return [a, b]; })('a'); - assert.deepEqual(actual, ['a', undefined]); - }); - }); - - /*--------------------------------------------------------------------------*/ - QUnit.module('key methods'); (function() { @@ -551,46 +522,6 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('reduce methods'); - - _.each(['reduce', 'reduceRight'], function(methodName) { - var func = fp[methodName], - isReduce = methodName == 'reduce'; - - QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an array', function(assert) { - assert.expect(1); - - var args, - array = [1, 2, 3]; - - func(function() { - args || (args = slice.call(arguments)); - })(0, array); - - assert.deepEqual(args, isReduce ? [0, 1] : [0, 3]); - }); - - QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an object', function(assert) { - assert.expect(1); - - var args, - object = { 'a': 1, 'b': 2 }, - isFIFO = _.keys(object)[0] == 'a'; - - var expected = isFIFO - ? (isReduce ? [0, 1] : [0, 2]) - : (isReduce ? [0, 2] : [0, 1]); - - func(function() { - args || (args = slice.call(arguments)); - })(0, object); - - assert.deepEqual(args, expected); - }); - }); - - /*--------------------------------------------------------------------------*/ - QUnit.module('with methods'); (function() { @@ -644,6 +575,35 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('fp.curry and fp.curryRight'); + + _.each(['curry', 'curryRight'], function(methodName) { + var func = fp[methodName]; + + QUnit.test('`_.' + methodName + '` should only accept a `func` param', function(assert) { + assert.expect(1); + + assert.raises(function() { func(1, _.noop); }, TypeError); + }); + }); + + /*--------------------------------------------------------------------------*/ + + QUnit.module('fp.curryN and fp.curryRightN'); + + _.each(['curryN', 'curryRightN'], function(methodName) { + var func = fp[methodName]; + + QUnit.test('`_.' + methodName + '` accept an `arity` param', function(assert) { + assert.expect(1); + + var actual = func(1, function(a, b) { return [a, b]; })('a'); + assert.deepEqual(actual, ['a', undefined]); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('fp.difference'); (function() { @@ -937,6 +897,46 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('fp.reduce and fp.reduceRight'); + + _.each(['reduce', 'reduceRight'], function(methodName) { + var func = fp[methodName], + isReduce = methodName == 'reduce'; + + QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an array', function(assert) { + assert.expect(1); + + var args, + array = [1, 2, 3]; + + func(function() { + args || (args = slice.call(arguments)); + })(0, array); + + assert.deepEqual(args, isReduce ? [0, 1] : [0, 3]); + }); + + QUnit.test('`_.' + methodName + '` should provide the correct `iteratee` arguments when iterating an object', function(assert) { + assert.expect(1); + + var args, + object = { 'a': 1, 'b': 2 }, + isFIFO = _.keys(object)[0] == 'a'; + + var expected = isFIFO + ? (isReduce ? [0, 1] : [0, 2]) + : (isReduce ? [0, 2] : [0, 1]); + + func(function() { + args || (args = slice.call(arguments)); + })(0, object); + + assert.deepEqual(args, expected); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('fp.runInContext'); (function() {