Expand stringToPath to matching more non-string expressions.

This commit is contained in:
John-David Dalton
2017-02-07 10:48:46 -08:00
parent 30c32908a3
commit 5e38259044

View File

@@ -7,8 +7,8 @@ const rePropName = RegExp(
'[^.[\\]]+' + '|' + '[^.[\\]]+' + '|' +
// Or match property names within brackets. // Or match property names within brackets.
'\\[(?:' + '\\[(?:' +
// Match numbers. // Match a non-string expression.
'(-?\\d+(?:\\.\\d+)?)' + '|' + '([^"\'].*)' + '|' +
// Or match strings (supports escaping characters). // Or match strings (supports escaping characters).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' + '(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]'+ '|' + ')\\]'+ '|' +
@@ -28,8 +28,15 @@ const stringToPath = memoizeCapped(string => {
if (reLeadingDot.test(string)) { if (reLeadingDot.test(string)) {
result.push('') result.push('')
} }
string.replace(rePropName, (match, number, quote, string) => { string.replace(rePropName, (match, expression, quote, string) => {
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match)) let key = match
if (quote) {
key = string.replace(reEscapeChar, '$1')
}
else if (expression) {
key = expression.trim()
}
result.push(key)
}) })
return result return result
}) })