identifier.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. class IdentifierGenerator {
  4. constructor() {
  5. // u 被框架占用了,不提供 u 开头的变量名
  6. this._chars = 'abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  7. this._nextIds = [0];
  8. }
  9. next() {
  10. const r = [];
  11. for (const char of this._nextIds) {
  12. r.unshift(this._chars[char]);
  13. }
  14. this._increment();
  15. const id = r.join('');
  16. if (keywords.includes(id)) {
  17. return this.next();
  18. }
  19. return id;
  20. }
  21. _increment() {
  22. for (let i = 0; i < this._nextIds.length; i++) {
  23. const val = ++this._nextIds[i];
  24. if (val >= this._chars.length) {
  25. this._nextIds[i] = 0;
  26. }
  27. else {
  28. return;
  29. }
  30. }
  31. this._nextIds.push(0);
  32. }
  33. *[Symbol.iterator]() {
  34. while (true) {
  35. yield this.next();
  36. }
  37. }
  38. }
  39. exports.default = IdentifierGenerator;
  40. const keywords = [
  41. 'abstract',
  42. 'arguments',
  43. 'await',
  44. 'boolean',
  45. 'break',
  46. 'byte',
  47. 'case',
  48. 'catch',
  49. 'char',
  50. 'class',
  51. 'const',
  52. 'continue',
  53. 'debugger',
  54. 'default',
  55. 'delete',
  56. 'do',
  57. 'double',
  58. 'else',
  59. 'enum',
  60. 'eval',
  61. 'export',
  62. 'extends',
  63. 'false',
  64. 'final',
  65. 'finally',
  66. 'float',
  67. 'for',
  68. 'function',
  69. 'goto',
  70. 'if',
  71. 'implements',
  72. 'import',
  73. 'in',
  74. 'instanceof',
  75. 'int',
  76. 'interface',
  77. 'let',
  78. 'long',
  79. 'native',
  80. 'new',
  81. 'null',
  82. 'package',
  83. 'private',
  84. 'protected',
  85. 'public',
  86. 'return',
  87. 'short',
  88. 'static',
  89. 'super',
  90. 'switch',
  91. 'synchronized',
  92. 'this',
  93. 'throw',
  94. 'throws',
  95. 'transient',
  96. 'true',
  97. 'try',
  98. 'typeof',
  99. 'var',
  100. 'void',
  101. 'volatile',
  102. 'while',
  103. 'with',
  104. 'yield',
  105. ];