mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 16:47:49 +00:00
Avoid unnecessary cloning in fp set methods. [closes #2017]
This commit is contained in:
@@ -44,7 +44,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
|
|
||||||
var helpers = isLib ? func : {
|
var helpers = isLib ? func : {
|
||||||
'ary': util.ary,
|
'ary': util.ary,
|
||||||
'cloneDeep': util.cloneDeep,
|
'clone': util.clone,
|
||||||
'curry': util.curry,
|
'curry': util.curry,
|
||||||
'forEach': util.forEach,
|
'forEach': util.forEach,
|
||||||
'isArray': util.isArray,
|
'isArray': util.isArray,
|
||||||
@@ -52,18 +52,20 @@ function baseConvert(util, name, func, options) {
|
|||||||
'iteratee': util.iteratee,
|
'iteratee': util.iteratee,
|
||||||
'keys': util.keys,
|
'keys': util.keys,
|
||||||
'rearg': util.rearg,
|
'rearg': util.rearg,
|
||||||
'spread': util.spread
|
'spread': util.spread,
|
||||||
|
'toPath': util.toPath
|
||||||
};
|
};
|
||||||
|
|
||||||
var ary = helpers.ary,
|
var ary = helpers.ary,
|
||||||
cloneDeep = helpers.cloneDeep,
|
clone = helpers.clone,
|
||||||
curry = helpers.curry,
|
curry = helpers.curry,
|
||||||
each = helpers.forEach,
|
each = helpers.forEach,
|
||||||
isArray = helpers.isArray,
|
isArray = helpers.isArray,
|
||||||
isFunction = helpers.isFunction,
|
isFunction = helpers.isFunction,
|
||||||
keys = helpers.keys,
|
keys = helpers.keys,
|
||||||
rearg = helpers.rearg,
|
rearg = helpers.rearg,
|
||||||
spread = helpers.spread;
|
spread = helpers.spread,
|
||||||
|
toPath = helpers.toPath;
|
||||||
|
|
||||||
var aryMethodKeys = keys(mapping.aryMethod);
|
var aryMethodKeys = keys(mapping.aryMethod);
|
||||||
|
|
||||||
@@ -89,6 +91,26 @@ function baseConvert(util, name, func, options) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var cloneByPath = function(object, path) {
|
||||||
|
path = toPath(path);
|
||||||
|
|
||||||
|
var index = -1,
|
||||||
|
length = path.length,
|
||||||
|
result = clone(Object(object)),
|
||||||
|
nested = result;
|
||||||
|
|
||||||
|
while (nested != null && ++index < length) {
|
||||||
|
var key = path[index],
|
||||||
|
value = nested[key];
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
nested[key] = clone(Object(value));
|
||||||
|
}
|
||||||
|
nested = nested[key];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
var createCloner = function(func) {
|
var createCloner = function(func) {
|
||||||
return function(object) {
|
return function(object) {
|
||||||
return func({}, object);
|
return func({}, object);
|
||||||
@@ -105,7 +127,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
while (length--) {
|
while (length--) {
|
||||||
args[length] = arguments[length];
|
args[length] = arguments[length];
|
||||||
}
|
}
|
||||||
var result = args[0] = cloner(args[0]);
|
var result = args[0] = cloner.apply(undefined, args);
|
||||||
func.apply(undefined, args);
|
func.apply(undefined, args);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -215,7 +237,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
wrapped = immutWrap(func, createCloner(func));
|
wrapped = immutWrap(func, createCloner(func));
|
||||||
}
|
}
|
||||||
else if (mutateMap.set[name]) {
|
else if (mutateMap.set[name]) {
|
||||||
wrapped = immutWrap(func, cloneDeep);
|
wrapped = immutWrap(func, cloneByPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var result;
|
var result;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
'ary': require('../ary'),
|
'ary': require('../ary'),
|
||||||
'cloneDeep': require('../cloneDeep'),
|
'clone': require('../clone'),
|
||||||
'curry': require('../curry'),
|
'curry': require('../curry'),
|
||||||
'forEach': require('../_arrayEach'),
|
'forEach': require('../_arrayEach'),
|
||||||
'isArray': require('../isArray'),
|
'isArray': require('../isArray'),
|
||||||
@@ -8,5 +8,6 @@ module.exports = {
|
|||||||
'iteratee': require('../iteratee'),
|
'iteratee': require('../iteratee'),
|
||||||
'keys': require('../_baseKeys'),
|
'keys': require('../_baseKeys'),
|
||||||
'rearg': require('../rearg'),
|
'rearg': require('../rearg'),
|
||||||
'spread': require('../spread')
|
'spread': require('../spread'),
|
||||||
|
'toPath': require('../toPath')
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
"istanbul": "0.4.2",
|
"istanbul": "0.4.2",
|
||||||
"jquery": "^2.2.0",
|
"jquery": "^2.2.0",
|
||||||
"jscs": "^2.10.1",
|
"jscs": "^2.10.1",
|
||||||
"lodash": "^3.10.1",
|
"lodash": "4.4.0",
|
||||||
"platform": "^1.3.1",
|
"platform": "^1.3.1",
|
||||||
"qunit-extras": "^1.4.5",
|
"qunit-extras": "^1.4.5",
|
||||||
"qunitjs": "~1.21.0",
|
"qunitjs": "~1.21.0",
|
||||||
|
|||||||
@@ -691,7 +691,7 @@
|
|||||||
actual = fp.unset('a.b')(value);
|
actual = fp.unset('a.b')(value);
|
||||||
|
|
||||||
assert.deepEqual(value, deepObject, 'fp.unset');
|
assert.deepEqual(value, deepObject, 'fp.unset');
|
||||||
assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.set');
|
assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.unset');
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -731,6 +731,42 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('set methods');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var array = [1, 2, 3],
|
||||||
|
object = { 'a': 1 },
|
||||||
|
deepObject = { 'a': { 'b': 2, 'c': 3 } };
|
||||||
|
|
||||||
|
QUnit.test('should only clone objects in `path`', function(assert) {
|
||||||
|
assert.expect(8);
|
||||||
|
|
||||||
|
var object = { 'a': { 'b': { 'c': 1 }, 'd': { 'e': 1 } } },
|
||||||
|
value = _.cloneDeep(object),
|
||||||
|
actual = fp.set('a.b.c.d.e', 3, value);
|
||||||
|
|
||||||
|
assert.ok(_.isObject(actual.a.b.c), 'fp.set');
|
||||||
|
assert.ok(_.isNumber(actual.a.b.c), 'fp.set');
|
||||||
|
|
||||||
|
assert.strictEqual(actual.a.b.c.d.e, 3, 'fp.set');
|
||||||
|
assert.strictEqual(actual.d, value.d, 'fp.set');
|
||||||
|
|
||||||
|
value = _.cloneDeep(object);
|
||||||
|
actual = fp.setWith(Object)('a.b.c')(2)(value);
|
||||||
|
|
||||||
|
assert.strictEqual(actual.a.b.c, 2, 'fp.setWith');
|
||||||
|
assert.strictEqual(actual.d, value.d, 'fp.setWith');
|
||||||
|
|
||||||
|
value = _.cloneDeep(object);
|
||||||
|
actual = fp.unset('a.b')(value);
|
||||||
|
|
||||||
|
assert.notOk('b' in actual, 'fp.unset');
|
||||||
|
assert.strictEqual(actual.d, value.d, 'fp.unset');
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('with methods');
|
QUnit.module('with methods');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user