mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Overwrite primitives in paths of set methods. [closes #2558]
This commit is contained in:
23
lodash.js
23
lodash.js
@@ -3696,20 +3696,19 @@
|
|||||||
nested = object;
|
nested = object;
|
||||||
|
|
||||||
while (nested != null && ++index < length) {
|
while (nested != null && ++index < length) {
|
||||||
var key = toKey(path[index]);
|
var key = toKey(path[index]),
|
||||||
if (isObject(nested)) {
|
newValue = value;
|
||||||
var newValue = value;
|
|
||||||
if (index != lastIndex) {
|
if (index != lastIndex) {
|
||||||
var objValue = nested[key];
|
var objValue = nested[key];
|
||||||
newValue = customizer ? customizer(objValue, key, nested) : undefined;
|
newValue = customizer ? customizer(objValue, key, nested) : undefined;
|
||||||
if (newValue === undefined) {
|
if (newValue === undefined) {
|
||||||
newValue = objValue == null
|
newValue = isObject(objValue)
|
||||||
? (isIndex(path[index + 1]) ? [] : {})
|
? objValue
|
||||||
: objValue;
|
: (isIndex(path[index + 1]) ? [] : {});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
assignValue(nested, key, newValue);
|
|
||||||
}
|
}
|
||||||
|
assignValue(nested, key, newValue);
|
||||||
nested = nested[key];
|
nested = nested[key];
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
|
|||||||
38
test/test.js
38
test/test.js
@@ -19865,41 +19865,15 @@
|
|||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should follow `path` over non-plain objects', function(assert) {
|
QUnit.test('`_.' + methodName + '` should overwrite primitives in the path', function(assert) {
|
||||||
assert.expect(4);
|
|
||||||
|
|
||||||
var object = { 'a': '' },
|
|
||||||
paths = ['constructor.prototype.a', ['constructor', 'prototype', 'a']];
|
|
||||||
|
|
||||||
lodashStable.each(paths, function(path) {
|
|
||||||
func(0, path, updater);
|
|
||||||
assert.strictEqual(0..a, value);
|
|
||||||
delete numberProto.a;
|
|
||||||
});
|
|
||||||
|
|
||||||
lodashStable.each(['a.replace.b', ['a', 'replace', 'b']], function(path) {
|
|
||||||
func(object, path, updater);
|
|
||||||
assert.strictEqual(stringProto.replace.b, value);
|
|
||||||
delete stringProto.replace.b;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should not error on paths over primitives in strict mode', function(assert) {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
lodashStable.each(['a', 'a.a.a'], function(path) {
|
lodashStable.each(['a.b', ['a', 'b']], function(path) {
|
||||||
numberProto.a = oldValue;
|
var object = { 'a': '' };
|
||||||
try {
|
|
||||||
func(0, path, updater);
|
|
||||||
assert.strictEqual(0..a, oldValue);
|
|
||||||
} catch (e) {
|
|
||||||
assert.ok(false, e.message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
delete numberProto.a;
|
func(object, path, updater);
|
||||||
|
assert.deepEqual(object, { 'a': { 'b': 2 } });
|
||||||
|
});;
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should not create an array for missing non-index property names that start with numbers', function(assert) {
|
QUnit.test('`_.' + methodName + '` should not create an array for missing non-index property names that start with numbers', function(assert) {
|
||||||
|
|||||||
Reference in New Issue
Block a user