Add fp.assignAllWith, fp.extendAllWith, and fp.mergeAllWith.

This commit is contained in:
John-David Dalton
2016-06-06 19:58:51 -07:00
parent 43d1ca89c2
commit 138cf77916
3 changed files with 159 additions and 61 deletions

View File

@@ -71,11 +71,11 @@ function createCloner(func) {
* @param {Function} cloner The function to clone arguments.
* @returns {Function} Returns the new immutable function.
*/
function immutWrap(func, cloner) {
function wrapImmutable(func, cloner) {
return function() {
var length = arguments.length;
if (!length) {
return result;
return;
}
var args = Array(length);
while (length--) {
@@ -218,6 +218,77 @@ function baseConvert(util, name, func, options) {
/*--------------------------------------------------------------------------*/
/**
* Casts `func` to a function with an arity capped iteratee if needed.
*
* @private
* @param {string} name The name of the function to inspect.
* @param {Function} func The function to inspect.
* @returns {Function} Returns the cast function.
*/
function castCap(name, func) {
if (config.cap) {
var indexes = mapping.iterateeRearg[name];
if (indexes) {
return iterateeRearg(func, indexes);
}
var n = !isLib && mapping.iterateeAry[name];
if (n) {
return iterateeAry(func, n);
}
}
return func;
}
/**
* Casts `func` to a curried function if needed.
*
* @private
* @param {string} name The name of the function to inspect.
* @param {Function} func The function to inspect.
* @param {number} n The arity of `func`.
* @returns {Function} Returns the cast function.
*/
function castCurry(name, func, n) {
return (forceCurry || (config.curry && n > 1))
? curry(func, n)
: func;
}
/**
* Casts `func` to a fixed arity function if needed.
*
* @private
* @param {string} name The name of the function to inspect.
* @param {Function} func The function to inspect.
* @param {number} n The arity cap.
* @returns {Function} Returns the cast function.
*/
function castFixed(name, func, n) {
if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
var data = mapping.methodSpread[name],
start = data && data.start;
return start === undefined ? ary(func, n) : spread(func, start);
}
return func;
}
/**
* Casts `func` to an rearged function if needed.
*
* @private
* @param {string} name The name of the function to inspect.
* @param {Function} func The function to inspect.
* @param {number} n The arity of `func`.
* @returns {Function} Returns the cast function.
*/
function castRearg(name, func, n) {
return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
: func;
}
/**
* Creates a clone of `object` by `path`.
*
@@ -354,42 +425,27 @@ function baseConvert(util, name, func, options) {
}
else if (config.immutable) {
if (mutateMap.array[name]) {
wrapped = immutWrap(func, cloneArray);
wrapped = wrapImmutable(func, cloneArray);
}
else if (mutateMap.object[name]) {
wrapped = immutWrap(func, createCloner(func));
wrapped = wrapImmutable(func, createCloner(func));
}
else if (mutateMap.set[name]) {
wrapped = immutWrap(func, cloneByPath);
wrapped = wrapImmutable(func, cloneByPath);
}
}
each(aryMethodKeys, function(aryKey) {
each(mapping.aryMethod[aryKey], function(otherName) {
if (name == otherName) {
var aryN = !isLib && mapping.iterateeAry[name],
reargIndexes = mapping.iterateeRearg[name],
spreadStart = mapping.methodSpread[name];
var spreadData = mapping.methodSpread[name],
afterRearg = spreadData && spreadData.afterRearg;
result = wrapped;
if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
result = spreadStart === undefined
? ary(result, aryKey)
: spread(result, spreadStart);
}
if (config.rearg && aryKey > 1 && (forceRearg || !mapping.skipRearg[name])) {
result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[aryKey]);
}
if (config.cap) {
if (reargIndexes) {
result = iterateeRearg(result, reargIndexes);
} else if (aryN) {
result = iterateeAry(result, aryN);
}
}
if (forceCurry || (config.curry && aryKey > 1)) {
forceCurry && console.log(forceCurry, name);
result = curry(result, aryKey);
}
result = afterRearg
? castFixed(name, castRearg(name, wrapped, aryKey), aryKey)
: castRearg(name, castFixed(name, wrapped, aryKey), aryKey);
result = castCap(name, result);
result = castCurry(name, result, aryKey);
return false;
}
});

View File

@@ -8,6 +8,7 @@ exports.aliasToReal = {
'entriesIn': 'toPairsIn',
'extend': 'assignIn',
'extendAll': 'assignInAll',
'extendAllWith': 'assignInAllWith',
'extendWith': 'assignInWith',
'first': 'head',
@@ -76,27 +77,27 @@ exports.aryMethod = {
'trimStart', 'uniqueId', 'words'
],
'2': [
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindAll',
'bindKey', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN',
'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'defaultTo', 'delay',
'difference', 'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile',
'endsWith', 'eq', 'every', 'filter', 'find', 'findIndex', 'findKey', 'findLast',
'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth',
'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', 'meanBy', 'merge', 'minBy', 'multiply', 'nth',
'omit', 'omitBy', 'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt',
'partial', 'partialRight', 'partition', 'pick', 'pickBy', 'pull', 'pullAll',
'pullAt', 'random', 'range', 'rangeRight', 'rearg', 'reject', 'remove',
'repeat', 'restFrom', 'result', 'sampleSize', 'some', 'sortBy', 'sortedIndex',
'sortedIndexOf', 'sortedLastIndex', 'sortedLastIndexOf', 'sortedUniqBy',
'split', 'spreadFrom', 'startsWith', 'subtract', 'sumBy', 'take', 'takeRight',
'takeRightWhile', 'takeWhile', 'tap', 'throttle', 'thru', 'times', 'trimChars',
'trimCharsEnd', 'trimCharsStart', 'truncate', 'union', 'uniqBy', 'uniqWith',
'unset', 'unzipWith', 'without', 'wrap', 'xor', 'zip', 'zipObject',
'zipObjectDeep'
'add', 'after', 'ary', 'assign', 'assignAllWith', 'assignIn', 'assignInAllWith',
'at', 'before', 'bind', 'bindAll', 'bindKey', 'chunk', 'cloneDeepWith',
'cloneWith', 'concat', 'countBy', 'curryN', 'curryRightN', 'debounce',
'defaults', 'defaultsDeep', 'defaultTo', 'delay', 'difference', 'divide',
'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', 'every',
'filter', 'find', 'findIndex', 'findKey', 'findLast', 'findLastIndex',
'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth', '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', 'meanBy', 'merge', 'minBy', 'multiply', 'nth', 'omit', 'omitBy',
'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt', 'partial', 'partialRight',
'partition', 'pick', 'pickBy', 'pull', 'pullAll', 'pullAt', 'random', 'range',
'rangeRight', 'rearg', 'reject', 'remove', 'repeat', 'restFrom', 'result',
'sampleSize', 'some', 'sortBy', 'sortedIndex', 'sortedIndexOf', 'sortedLastIndex',
'sortedLastIndexOf', 'sortedUniqBy', 'split', 'spreadFrom', 'startsWith',
'subtract', 'sumBy', 'take', 'takeRight', 'takeRightWhile', 'takeWhile', 'tap',
'throttle', 'thru', 'times', 'trimChars', 'trimCharsEnd', 'trimCharsStart',
'truncate', 'union', 'uniqBy', 'uniqWith', 'unset', 'unzipWith', 'without',
'wrap', 'xor', 'zip', 'zipObject', 'zipObjectDeep'
],
'3': [
'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith',
@@ -167,7 +168,9 @@ exports.iterateeRearg = {
/** Used to map method names to rearg configs. */
exports.methodRearg = {
'assignInAllWith': [1, 2, 0],
'assignInWith': [1, 2, 0],
'assignAllWith': [1, 2, 0],
'assignWith': [1, 2, 0],
'differenceBy': [1, 2, 0],
'differenceWith': [1, 2, 0],
@@ -176,6 +179,7 @@ exports.methodRearg = {
'intersectionWith': [1, 2, 0],
'isEqualWith': [1, 2, 0],
'isMatchWith': [2, 1, 0],
'mergeAllWith': [1, 2, 0],
'mergeWith': [1, 2, 0],
'padChars': [2, 1, 0],
'padCharsEnd': [2, 1, 0],
@@ -195,16 +199,19 @@ exports.methodRearg = {
/** Used to map method names to spread configs. */
exports.methodSpread = {
'assignAll': 0,
'assignInAll': 0,
'defaultsAll': 0,
'defaultsDeepAll': 0,
'invokeArgs': 2,
'invokeArgsMap': 2,
'mergeAll': 0,
'partial': 1,
'partialRight': 1,
'without': 1
'assignAll': { 'start': 0 },
'assignAllWith': { 'afterRearg': true, 'start': 1 },
'assignInAll': { 'start': 0 },
'assignInAllWith': { 'afterRearg': true, 'start': 1 },
'defaultsAll': { 'start': 0 },
'defaultsDeepAll': { 'start': 0 },
'invokeArgs': { 'start': 2 },
'invokeArgsMap': { 'start': 2 },
'mergeAll': { 'start': 0 },
'mergeAllWith': { 'afterRearg': true, 'start': 1 },
'partial': { 'start': 1 },
'partialRight': { 'start': 1 },
'without': { 'start': 1 }
};
/** Used to identify methods which mutate arrays or objects. */
@@ -222,8 +229,10 @@ exports.mutate = {
'object': {
'assign': true,
'assignAll': true,
'assignAllWith': true,
'assignIn': true,
'assignInAll': true,
'assignInAllWith': true,
'assignInWith': true,
'assignWith': true,
'defaults': true,
@@ -232,7 +241,8 @@ exports.mutate = {
'defaultsDeepAll': true,
'merge': true,
'mergeAll': true,
'mergeWith': true
'mergeAllWith': true,
'mergeWith': true,
},
'set': {
'set': true,
@@ -273,7 +283,9 @@ exports.realToAlias = (function() {
/** Used to map method names to other names. */
exports.remap = {
'assignAll': 'assign',
'assignAllWith': 'assignWith',
'assignInAll': 'assignIn',
'assignInAllWith': 'assignInWith',
'curryN': 'curry',
'curryRightN': 'curryRight',
'defaultsAll': 'defaults',
@@ -289,6 +301,7 @@ exports.remap = {
'invokeArgsMap': 'invokeMap',
'lastIndexOfFrom': 'lastIndexOf',
'mergeAll': 'merge',
'mergeAllWith': 'mergeWith',
'padChars': 'pad',
'padCharsEnd': 'padEnd',
'padCharsStart': 'padStart',

View File

@@ -414,7 +414,7 @@
'wrap'
];
var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']),
var exceptions = _.without(funcMethods.concat('matchesProperty'), 'delay'),
expected = _.map(mapping.aryMethod[2], _.constant(true));
var actual = _.map(mapping.aryMethod[2], function(methodName) {
@@ -775,6 +775,35 @@
});
});
_.each(['assignAllWith', 'assignInAllWith', 'extendAllWith'], function(methodName) {
var func = fp[methodName];
QUnit.test('`fp.' + methodName + '` should provide the correct `customizer` arguments', function(assert) {
assert.expect(1);
var args;
func(function() {
args || (args = _.map(arguments, _.cloneDeep));
})([{ 'a': 1 }, { 'b': 2 }]);
assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }]);
});
QUnit.test('`fp.' + methodName + '` should not mutate values', function(assert) {
assert.expect(2);
var objects = [{ 'a': 1 }, { 'b': 2 }];
var actual = func(function(objValue, srcValue) {
return srcValue;
})(objects);
assert.deepEqual(objects[0], { 'a': 1 });
assert.deepEqual(actual, { 'a': 1, 'b': 2 });
});
});
/*--------------------------------------------------------------------------*/
QUnit.module('fp.castArray');