quote.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.stringifyPath = exports.quoteKey = exports.isValidVariableName = exports.IS_VALID_IDENTIFIER = exports.quoteString = void 0;
  4. /**
  5. * Match all characters that need to be escaped in a string. Modified from
  6. * source to match single quotes instead of double.
  7. *
  8. * Source: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
  9. */
  10. const ESCAPABLE = /[\\\'\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  11. /**
  12. * Map of characters to escape characters.
  13. */
  14. const META_CHARS = new Map([
  15. ["\b", "\\b"],
  16. ["\t", "\\t"],
  17. ["\n", "\\n"],
  18. ["\f", "\\f"],
  19. ["\r", "\\r"],
  20. ["'", "\\'"],
  21. ['"', '\\"'],
  22. ["\\", "\\\\"],
  23. ]);
  24. /**
  25. * Escape any character into its literal JavaScript string.
  26. *
  27. * @param {string} char
  28. * @return {string}
  29. */
  30. function escapeChar(char) {
  31. return (META_CHARS.get(char) ||
  32. `\\u${`0000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
  33. }
  34. /**
  35. * Quote a string.
  36. */
  37. function quoteString(str) {
  38. return `'${str.replace(ESCAPABLE, escapeChar)}'`;
  39. }
  40. exports.quoteString = quoteString;
  41. /**
  42. * JavaScript reserved keywords.
  43. */
  44. const RESERVED_WORDS = new Set(("break else new var case finally return void catch for switch while " +
  45. "continue function this with default if throw delete in try " +
  46. "do instanceof typeof abstract enum int short boolean export " +
  47. "interface static byte extends long super char final native synchronized " +
  48. "class float package throws const goto private transient debugger " +
  49. "implements protected volatile double import public let yield").split(" "));
  50. /**
  51. * Test for valid JavaScript identifier.
  52. */
  53. exports.IS_VALID_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
  54. /**
  55. * Check if a variable name is valid.
  56. */
  57. function isValidVariableName(name) {
  58. return (typeof name === "string" &&
  59. !RESERVED_WORDS.has(name) &&
  60. exports.IS_VALID_IDENTIFIER.test(name));
  61. }
  62. exports.isValidVariableName = isValidVariableName;
  63. /**
  64. * Quote JavaScript key access.
  65. */
  66. function quoteKey(key, next) {
  67. return isValidVariableName(key) ? key : next(key);
  68. }
  69. exports.quoteKey = quoteKey;
  70. /**
  71. * Serialize the path to a string.
  72. */
  73. function stringifyPath(path, next) {
  74. let result = "";
  75. for (const key of path) {
  76. if (isValidVariableName(key)) {
  77. result += `.${key}`;
  78. }
  79. else {
  80. result += `[${next(key)}]`;
  81. }
  82. }
  83. return result;
  84. }
  85. exports.stringifyPath = stringifyPath;
  86. //# sourceMappingURL=quote.js.map