Ensure empty brackets are ignored by _.get and _.set.

This commit is contained in:
jdalton
2015-04-21 00:02:28 -07:00
parent c8314b215b
commit 3429b5d661
2 changed files with 14 additions and 1 deletions

View File

@@ -88,7 +88,7 @@
reInterpolate = /<%=([\s\S]+?)%>/g;
/** Used to match property names within property paths. */
var reIsDeepProp = /\.|\[(?:[^[\]]+|(["'])(?:(?!\1)[^\n\\]|\\.)*?)\1\]/,
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/,
reIsPlainProp = /^\w*$/,
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;

View File

@@ -13249,6 +13249,11 @@
strictEqual(func(object, ['a', 'b', 'c']), 3);
});
test('`_.' + methodName + '` should ignore empty brackets', 1, function() {
var object = { 'a': 1 };
strictEqual(func(object, 'a[]'), 1);
});
test('`_.' + methodName + '` should handle empty paths', 4, function() {
_.each([['', ''], [[], ['']]], function(paths) {
strictEqual(func({}, paths[0]), undefined);
@@ -13712,11 +13717,19 @@
deepEqual(object, { 'a,b,c': 4, 'a': { 'b': { 'c': 3 } } });
});
test('should ignore empty brackets', 1, function() {
var object = {};
_.set(object, 'a[]', 1);
deepEqual(object, { 'a': 1 });
});
test('should handle empty paths', 4, function() {
_.each([['', ''], [[], ['']]], function(paths, index) {
var object = {};
_.set(object, paths[0], 1);
deepEqual(object, index ? {} : { '': 1 });
_.set(object, paths[1], 2);
deepEqual(object, { '': 2 });
});