Ensure paths with consecutive empty brackets or dots are parsed correctly.

This commit is contained in:
John-David Dalton
2016-07-25 07:16:45 -07:00
parent 98f12fb8e1
commit c253e8d7b8
2 changed files with 30 additions and 8 deletions

View File

@@ -129,7 +129,8 @@
/** Used to match property names within property paths. */
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
reIsPlainProp = /^\w*$/,
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(\.|\[\])(?:\4|$))/g;
reLeadingDot = /^\./,
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
/**
* Used to match `RegExp`
@@ -6215,8 +6216,13 @@
* @returns {Array} Returns the property path array.
*/
var stringToPath = memoize(function(string) {
string = toString(string);
var result = [];
toString(string).replace(rePropName, function(match, number, quote, string) {
if (reLeadingDot.test(string)) {
result.push('');
}
string.replace(rePropName, function(match, number, quote, string) {
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
});
return result;

View File

@@ -23531,16 +23531,32 @@
assert.deepEqual(actual, ['a', '-1.23', '["b"]', 'c', "['d']", '\ne\n', 'f', 'g']);
});
QUnit.test('should not ignore consecutive brackets and dots', function(assert) {
assert.expect(4);
QUnit.test('should handle consecutive empty brackets and dots', function(assert) {
assert.expect(12);
var expected = ['a', ''];
assert.deepEqual(_.toPath('a.'), expected);
assert.deepEqual(_.toPath('a[]'), expected);
var expected = ['', 'a'];
assert.deepEqual(_.toPath('.a'), expected);
assert.deepEqual(_.toPath('[].a'), expected);
expected = ['', '', 'a'];
assert.deepEqual(_.toPath('..a'), expected);
assert.deepEqual(_.toPath('[][].a'), expected);
expected = ['a', '', 'b'];
assert.deepEqual(_.toPath('a..b'), expected);
assert.deepEqual(_.toPath('a[][]b'), expected);
assert.deepEqual(_.toPath('a[].b'), expected);
expected = ['a', '', '', 'b'];
assert.deepEqual(_.toPath('a...b'), expected);
assert.deepEqual(_.toPath('a[][].b'), expected);
expected = ['a', ''];
assert.deepEqual(_.toPath('a.'), expected);
assert.deepEqual(_.toPath('a[]'), expected);
expected = ['a', '', ''];
assert.deepEqual(_.toPath('a..'), expected);
assert.deepEqual(_.toPath('a[][]'), expected);
});
}());