UNPKG

850 BJavaScriptView Raw
1var memoizeCapped = require('./_memoizeCapped');
2
3/** Used to match property names within property paths. */
4var reLeadingDot = /^\./,
5 rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
6
7/** Used to match backslashes in property paths. */
8var reEscapeChar = /\\(\\)?/g;
9
10/**
11 * Converts `string` to a property path array.
12 *
13 * @private
14 * @param {string} string The string to convert.
15 * @returns {Array} Returns the property path array.
16 */
17var stringToPath = memoizeCapped(function(string) {
18 var result = [];
19 if (reLeadingDot.test(string)) {
20 result.push('');
21 }
22 string.replace(rePropName, function(match, number, quote, string) {
23 result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
24 });
25 return result;
26});
27
28module.exports = stringToPath;