mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Ensure paths with consecutive empty brackets or dots are parsed correctly.
This commit is contained in:
10
lodash.js
10
lodash.js
@@ -129,7 +129,8 @@
|
|||||||
/** Used to match property names within property paths. */
|
/** Used to match property names within property paths. */
|
||||||
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
|
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
|
||||||
reIsPlainProp = /^\w*$/,
|
reIsPlainProp = /^\w*$/,
|
||||||
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(\.|\[\])(?:\4|$))/g;
|
reLeadingDot = /^\./,
|
||||||
|
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to match `RegExp`
|
* Used to match `RegExp`
|
||||||
@@ -6215,8 +6216,13 @@
|
|||||||
* @returns {Array} Returns the property path array.
|
* @returns {Array} Returns the property path array.
|
||||||
*/
|
*/
|
||||||
var stringToPath = memoize(function(string) {
|
var stringToPath = memoize(function(string) {
|
||||||
|
string = toString(string);
|
||||||
|
|
||||||
var result = [];
|
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));
|
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
28
test/test.js
28
test/test.js
@@ -23531,16 +23531,32 @@
|
|||||||
assert.deepEqual(actual, ['a', '-1.23', '["b"]', 'c', "['d']", '\ne\n', 'f', 'g']);
|
assert.deepEqual(actual, ['a', '-1.23', '["b"]', 'c', "['d']", '\ne\n', 'f', 'g']);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should not ignore consecutive brackets and dots', function(assert) {
|
QUnit.test('should handle consecutive empty brackets and dots', function(assert) {
|
||||||
assert.expect(4);
|
assert.expect(12);
|
||||||
|
|
||||||
var expected = ['a', ''];
|
var expected = ['', 'a'];
|
||||||
assert.deepEqual(_.toPath('a.'), expected);
|
assert.deepEqual(_.toPath('.a'), expected);
|
||||||
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'];
|
expected = ['a', '', 'b'];
|
||||||
assert.deepEqual(_.toPath('a..b'), expected);
|
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);
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user