stringToPath.cjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. // @ts-nocheck
  3. var __importDefault = (this && this.__importDefault) || function (mod) {
  4. return (mod && mod.__esModule) ? mod : { "default": mod };
  5. };
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. const memoizeCapped_js_1 = __importDefault(require("./memoizeCapped.cjs"));
  8. const charCodeOfDot = ".".charCodeAt(0);
  9. const reEscapeChar = /\\(\\)?/g;
  10. const rePropName = RegExp(
  11. // Match anything that isn't a dot or bracket.
  12. "[^.[\\]]+" +
  13. "|" +
  14. // Or match property names within brackets.
  15. "\\[(?:" +
  16. // Match a non-string expression.
  17. "([^\"'][^[]*)" +
  18. "|" +
  19. // Or match strings (supports escaping characters).
  20. "([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" +
  21. ")\\]" +
  22. "|" +
  23. // Or match "" as the space between consecutive dots or empty brackets.
  24. "(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))", "g");
  25. /**
  26. * Converts `string` to a property path array.
  27. *
  28. * @private
  29. * @param {string} string The string to convert.
  30. * @returns {Array} Returns the property path array.
  31. */
  32. const stringToPath = (0, memoizeCapped_js_1.default)((string) => {
  33. const result = [];
  34. if (string.charCodeAt(0) === charCodeOfDot) {
  35. result.push("");
  36. }
  37. string.replace(rePropName, (match, expression, quote, subString) => {
  38. let key = match;
  39. if (quote) {
  40. key = subString.replace(reEscapeChar, "$1");
  41. }
  42. else if (expression) {
  43. key = expression.trim();
  44. }
  45. result.push(key);
  46. });
  47. return result;
  48. });
  49. exports.default = stringToPath;