mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Make fp versions of set and setWith immutable.
This commit is contained in:
@@ -22,6 +22,7 @@ function baseConvert(util, name, func) {
|
|||||||
|
|
||||||
var _ = isLib ? func : {
|
var _ = isLib ? func : {
|
||||||
'ary': util.ary,
|
'ary': util.ary,
|
||||||
|
'cloneDeep': util.cloneDeep,
|
||||||
'curry': util.curry,
|
'curry': util.curry,
|
||||||
'forEach': util.forEach,
|
'forEach': util.forEach,
|
||||||
'isFunction': util.isFunction,
|
'isFunction': util.isFunction,
|
||||||
@@ -31,6 +32,7 @@ function baseConvert(util, name, func) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var ary = _.ary,
|
var ary = _.ary,
|
||||||
|
cloneDeep = _.cloneDeep,
|
||||||
curry = _.curry,
|
curry = _.curry,
|
||||||
each = _.forEach,
|
each = _.forEach,
|
||||||
isFunction = _.isFunction,
|
isFunction = _.isFunction,
|
||||||
@@ -90,6 +92,21 @@ function baseConvert(util, name, func) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var immutSetWrap = function(func) {
|
||||||
|
return function() {
|
||||||
|
var index = -1,
|
||||||
|
length = arguments.length,
|
||||||
|
args = Array(length);
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
args[index] = arguments[index];
|
||||||
|
}
|
||||||
|
args[0] = cloneDeep(args[0]);
|
||||||
|
func.apply(undefined, args);
|
||||||
|
return args[0];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
var iterateeAry = function(func, n) {
|
var iterateeAry = function(func, n) {
|
||||||
return function() {
|
return function() {
|
||||||
var length = arguments.length,
|
var length = arguments.length,
|
||||||
@@ -160,6 +177,9 @@ function baseConvert(util, name, func) {
|
|||||||
else if (mutateMap.object[name]) {
|
else if (mutateMap.object[name]) {
|
||||||
func = immutObjectWrap(func);
|
func = immutObjectWrap(func);
|
||||||
}
|
}
|
||||||
|
else if (mutateMap.set[name]) {
|
||||||
|
func = immutSetWrap(func);
|
||||||
|
}
|
||||||
var result;
|
var result;
|
||||||
each(mapping.caps, function(cap) {
|
each(mapping.caps, function(cap) {
|
||||||
each(mapping.aryMethodMap[cap], function(otherName) {
|
each(mapping.aryMethodMap[cap], function(otherName) {
|
||||||
|
|||||||
@@ -85,15 +85,15 @@ module.exports = {
|
|||||||
'invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,' +
|
'invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,' +
|
||||||
'mean,minBy,merge,omit,overArgs,pad,padLeft,padRight,parseInt,partition,' +
|
'mean,minBy,merge,omit,overArgs,pad,padLeft,padRight,parseInt,partition,' +
|
||||||
'pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,remove,repeat,' +
|
'pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,remove,repeat,' +
|
||||||
'result,sampleSize,set,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,' +
|
'result,sampleSize,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,' +
|
||||||
'sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,' +
|
'sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,' +
|
||||||
'throttle,times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','),
|
'throttle,times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','),
|
||||||
3: (
|
3: (
|
||||||
'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' +
|
'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' +
|
||||||
'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' +
|
'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' +
|
||||||
'reduceRight,slice,transform,unionBy,xorBy,zipWith').split(','),
|
'reduceRight,set,slice,transform,unionBy,xorBy,zipWith').split(','),
|
||||||
4:
|
4:
|
||||||
['fill']
|
['fill', 'setWith']
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Used to map ary to rearg configs by method ary. */
|
/** Used to map ary to rearg configs by method ary. */
|
||||||
@@ -108,6 +108,7 @@ module.exports = {
|
|||||||
'clamp': [2, 0, 1],
|
'clamp': [2, 0, 1],
|
||||||
'reduce': [2, 0, 1],
|
'reduce': [2, 0, 1],
|
||||||
'reduceRight': [2, 0, 1],
|
'reduceRight': [2, 0, 1],
|
||||||
|
'setWith': [3, 2, 1, 0],
|
||||||
'slice': [2, 0, 1],
|
'slice': [2, 0, 1],
|
||||||
'transform': [2, 0, 1]
|
'transform': [2, 0, 1]
|
||||||
},
|
},
|
||||||
@@ -142,6 +143,10 @@ module.exports = {
|
|||||||
'extendWith': true,
|
'extendWith': true,
|
||||||
'merge': true,
|
'merge': true,
|
||||||
'mergeWith': true
|
'mergeWith': true
|
||||||
|
},
|
||||||
|
'set': {
|
||||||
|
'set': true,
|
||||||
|
'setWith': true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
'ary': require('lodash/function/ary'),
|
'ary': require('lodash/function/ary'),
|
||||||
|
'cloneDeep': require('lodash/lang/cloneDeep'),
|
||||||
'curry': require('lodash/function/curry'),
|
'curry': require('lodash/function/curry'),
|
||||||
'forEach': require('lodash/internal/arrayEach'),
|
'forEach': require('lodash/internal/arrayEach'),
|
||||||
'isFunction': require('lodash/lang/isFunction'),
|
'isFunction': require('lodash/lang/isFunction'),
|
||||||
|
|||||||
@@ -651,10 +651,11 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var array = [1, 2, 3],
|
var array = [1, 2, 3],
|
||||||
object = { 'a': 1 };
|
object = { 'a': 1 },
|
||||||
|
deepObject = { 'a': { 'b': 2, 'c': 3 } };
|
||||||
|
|
||||||
QUnit.test('should not mutate values', function(assert) {
|
QUnit.test('should not mutate values', function(assert) {
|
||||||
assert.expect(28);
|
assert.expect(32);
|
||||||
|
|
||||||
function Foo() {}
|
function Foo() {}
|
||||||
Foo.prototype = { 'b': 2 };
|
Foo.prototype = { 'b': 2 };
|
||||||
@@ -753,6 +754,18 @@
|
|||||||
|
|
||||||
assert.deepEqual(value, array, 'fp.reverse');
|
assert.deepEqual(value, array, 'fp.reverse');
|
||||||
assert.deepEqual(actual, [3, 2, 1], 'fp.reverse');
|
assert.deepEqual(actual, [3, 2, 1], 'fp.reverse');
|
||||||
|
|
||||||
|
value = _.cloneDeep(deepObject);
|
||||||
|
actual = fp.set(3, 'a.b', value);
|
||||||
|
|
||||||
|
assert.deepEqual(value, deepObject, 'fp.set');
|
||||||
|
assert.deepEqual(actual, { 'a': { 'b': 3, 'c': 3 } }, 'fp.set');
|
||||||
|
|
||||||
|
value = _.cloneDeep(deepObject);
|
||||||
|
actual = fp.setWith(Object, 4, 'd.e', value);
|
||||||
|
|
||||||
|
assert.deepEqual(value, deepObject, 'fp.setWith');
|
||||||
|
assert.deepEqual(actual, { 'a': { 'b': 2, 'c': 3 }, 'd': { 'e': 4 } }, 'fp.setWith');
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user