toPath.js 756 B

1234567891011121314151617181920212223242526272829303132
  1. var arrayMap = require('./_arrayMap'),
  2. copyArray = require('./_copyArray'),
  3. isArray = require('./isArray'),
  4. isSymbol = require('./isSymbol'),
  5. stringToPath = require('./_stringToPath'),
  6. toKey = require('./_toKey');
  7. /**
  8. * Converts `value` to a property path array.
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 4.0.0
  13. * @category Util
  14. * @param {*} value The value to convert.
  15. * @returns {Array} Returns the new property path array.
  16. * @example
  17. *
  18. * _.toPath('a.b.c');
  19. * // => ['a', 'b', 'c']
  20. *
  21. * _.toPath('a[0].b.c');
  22. * // => ['a', '0', 'b', 'c']
  23. */
  24. function toPath(value) {
  25. if (isArray(value)) {
  26. return arrayMap(value, toKey);
  27. }
  28. return isSymbol(value) ? [value] : copyArray(stringToPath(value));
  29. }
  30. module.exports = toPath;