Files
lodash/.internal/stringToPath.js
2017-02-07 01:43:30 -08:00

42 lines
1.1 KiB
JavaScript

import memoizeCapped from './memoizeCapped.js'
/** Used to match property names within property paths. */
const reLeadingDot = /^\./
const rePropName = RegExp([
// Match anything that isn't a dot or bracket.
'[^.[\\]]+',
// Or match property names within brackets.
'\\[(?:' +
// Match numbers.
'(-?\\d+(?:\\.\\d+)?)' + '|' +
// Or match strings (supports escaping quotation marks).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]',
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))'
].join('|'), 'g')
/** Used to match backslashes in property paths. */
const reEscapeChar = /\\(\\)?/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, number, quote, string) => {
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match))
})
return result
})
export default stringToPath