UNPKG

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