mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 17:47:49 +00:00
Ensure _.set passes strict mode tests.
This commit is contained in:
@@ -9964,11 +9964,10 @@
|
|||||||
* // => 5
|
* // => 5
|
||||||
*/
|
*/
|
||||||
function set(object, path, value) {
|
function set(object, path, value) {
|
||||||
if (object == null) {
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
if (isKey(path, object)) {
|
if (isKey(path, object)) {
|
||||||
object[path] = value;
|
if (isObject(object)) {
|
||||||
|
object[path] = value;
|
||||||
|
}
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
path = toPath(path);
|
path = toPath(path);
|
||||||
@@ -9978,12 +9977,14 @@
|
|||||||
endIndex = length - 1,
|
endIndex = length - 1,
|
||||||
nested = object;
|
nested = object;
|
||||||
|
|
||||||
while (++index < length) {
|
while (nested != null && ++index < length) {
|
||||||
var key = path[index];
|
var key = path[index];
|
||||||
if (index == endIndex) {
|
if (isObject(nested)) {
|
||||||
nested[key] = value;
|
if (index == endIndex) {
|
||||||
} else if (nested[key] == null) {
|
nested[key] = value;
|
||||||
nested[key] = isIndex(path[index + 1]) ? [] : {};
|
} else if (nested[key] == null) {
|
||||||
|
nested[key] = isIndex(path[index + 1]) ? [] : {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
nested = nested[key];
|
nested = nested[key];
|
||||||
}
|
}
|
||||||
|
|||||||
17
test/test.js
17
test/test.js
@@ -13261,7 +13261,7 @@
|
|||||||
|
|
||||||
test('should follow `path` over non-plain objects', 2, function() {
|
test('should follow `path` over non-plain objects', 2, function() {
|
||||||
_.set(0, 'constructor.prototype.a', 'ok');
|
_.set(0, 'constructor.prototype.a', 'ok');
|
||||||
strictEqual(1..a, 'ok');
|
strictEqual(0..a, 'ok');
|
||||||
delete numberProto.a;
|
delete numberProto.a;
|
||||||
|
|
||||||
var object = { 'a': '' };
|
var object = { 'a': '' };
|
||||||
@@ -13269,6 +13269,21 @@
|
|||||||
strictEqual(stringProto.replace.b, 1);
|
strictEqual(stringProto.replace.b, 1);
|
||||||
delete stringProto.replace.b;
|
delete stringProto.replace.b;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should not error on paths over primitive values in strict mode', 2, function() {
|
||||||
|
numberProto.a = 0;
|
||||||
|
|
||||||
|
_.each(['a', 'a.a.a'], function(path) {
|
||||||
|
try {
|
||||||
|
_.set(0, path, 1);
|
||||||
|
strictEqual(0..a, 0);
|
||||||
|
} catch(e) {
|
||||||
|
ok(false, e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
delete numberProto.a;
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user