_stringToPath.js 840 B

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