Files
lodash/.internal/stringToPath.js
2017-02-27 12:15:08 +01:00

45 lines
1.1 KiB
JavaScript

import memoizeCapped from './memoizeCapped.js'
const reEscapeChar = /\\(\\)?/g
const reLeadingDot = /^\./
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' + '|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'].*)' + '|' +
// Or match strings (supports escaping characters).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]'+ '|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))'
, 'g')
/**
* Converts `string` to a property path array.
*
* @private
* @param {string} string The string to convert.
* @returns {Array} Returns the property path array.
*/
const stringToPath = memoizeCapped((string) => {
const result = []
if (reLeadingDot.test(string)) {
result.push('')
}
string.replace(rePropName, (match, expression, quote, string) => {
let key = match
if (quote) {
key = string.replace(reEscapeChar, '$1')
}
else if (expression) {
key = expression.trim()
}
result.push(key)
})
return result
})
export default stringToPath