_stringToPath.js 786 B

12345678910111213141516171819202122232425
  1. var memoize = require('./memoize'),
  2. toString = require('./toString');
  3. /** Used to match property names within property paths. */
  4. var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(\.|\[\])(?:\4|$))/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 = memoize(function(string) {
  15. var result = [];
  16. toString(string).replace(rePropName, function(match, number, quote, string) {
  17. result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
  18. });
  19. return result;
  20. });
  21. module.exports = stringToPath;