toPath.js 806 B

12345678910111213141516171819202122232425262728
  1. var baseToString = require('./baseToString'),
  2. isArray = require('../lang/isArray');
  3. /** Used to match property names within property paths. */
  4. var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\n\\]|\\.)*?)\2)\]/g;
  5. /** Used to match backslashes in property paths. */
  6. var reEscapeChar = /\\(\\)?/g;
  7. /**
  8. * Converts `value` to property path array if it's not one.
  9. *
  10. * @private
  11. * @param {*} value The value to process.
  12. * @returns {Array} Returns the property path array.
  13. */
  14. function toPath(value) {
  15. if (isArray(value)) {
  16. return value;
  17. }
  18. var result = [];
  19. baseToString(value).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 = toPath;