mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-15 13:17:50 +00:00
Ensure complex paths work with _.get, _.result, & _.set.
This commit is contained in:
@@ -89,7 +89,7 @@
|
|||||||
|
|
||||||
/** Used to match property names within property paths. */
|
/** Used to match property names within property paths. */
|
||||||
var reIsDeepProp = /\.|\[(?:[^[\]]+|(["'])(?:(?!\1)[^\n\\]|\\.)*?)\1\]/,
|
var reIsDeepProp = /\.|\[(?:[^[\]]+|(["'])(?:(?!\1)[^\n\\]|\\.)*?)\1\]/,
|
||||||
rePropName = /[^.[\]]+|\[(?:(\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
|
rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special).
|
* Used to match `RegExp` [special characters](http://www.regular-expressions.info/characters.html#special).
|
||||||
@@ -102,6 +102,9 @@
|
|||||||
/** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
|
/** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
|
||||||
var reComboMark = /[\u0300-\u036f\ufe20-\ufe23]/g;
|
var reComboMark = /[\u0300-\u036f\ufe20-\ufe23]/g;
|
||||||
|
|
||||||
|
/** Used to match backslashes in property paths. */
|
||||||
|
var reEscapeChar = /\\(\\)?/g;
|
||||||
|
|
||||||
/** Used to match [ES template delimiters](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components). */
|
/** Used to match [ES template delimiters](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literal-lexical-components). */
|
||||||
var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
|
var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
|
||||||
|
|
||||||
@@ -4584,7 +4587,7 @@
|
|||||||
}
|
}
|
||||||
var result = [];
|
var result = [];
|
||||||
baseToString(value).replace(rePropName, function(match, number, quote, string) {
|
baseToString(value).replace(rePropName, function(match, number, quote, string) {
|
||||||
result.push(quote ? string : (number || match));
|
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -13139,6 +13139,14 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('`_.' + methodName + '` should handle complex paths', 2, function() {
|
||||||
|
var object = { 'a': { '-1.23': { '["b"]': { 'c': { "['d']": { 'e': 5 } } } } } };
|
||||||
|
|
||||||
|
_.each(['a[-1.23]["[\\"b\\"]"].c[\'[\\\'d\\\']\'].e', ['a', '-1.23', '["b"]', 'c', "['d']", 'e']], function(path) {
|
||||||
|
strictEqual(func(object, path), 5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should return `undefined` when `object` is nullish', 4, function() {
|
test('`_.' + methodName + '` should return `undefined` when `object` is nullish', 4, function() {
|
||||||
_.each(['constructor', ['constructor']], function(path) {
|
_.each(['constructor', ['constructor']], function(path) {
|
||||||
strictEqual(func(null, path), undefined);
|
strictEqual(func(null, path), undefined);
|
||||||
@@ -13595,6 +13603,16 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should handle complex paths', 2, function() {
|
||||||
|
var object = { 'a': { '1.23': { '["b"]': { 'c': { "['d']": { 'e': 5 } } } } } };
|
||||||
|
|
||||||
|
_.each(['a[-1.23]["[\\"b\\"]"].c[\'[\\\'d\\\']\'].e', ['a', '-1.23', '["b"]', 'c', "['d']", 'e']], function(path) {
|
||||||
|
_.set(object, path, 6);
|
||||||
|
strictEqual(object.a[-1.23]['["b"]'].c["['d']"].e, 6);
|
||||||
|
object.a[-1.23]['["b"]'].c["['d']"].e = 5;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('should create parts of `path` that are missing', 6, function() {
|
test('should create parts of `path` that are missing', 6, function() {
|
||||||
var object = {};
|
var object = {};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user