_stringToPath.js 850 B

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