@amritk/helpers 0.25.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,3 +26,38 @@ export declare const regexFlagsFor: (pattern: string) => string;
26
26
  * @throws if `pattern` is not a valid regular expression.
27
27
  */
28
28
  export declare const regexLiteral: (pattern: string) => string;
29
+ /**
30
+ * The string a `pattern` matches verbatim, and where it is anchored, when the
31
+ * pattern is nothing but literal characters — `^x-`, `^\$ref$`, `^\/`. `null` for
32
+ * anything that needs a regex engine.
33
+ *
34
+ * Kept deliberately narrow: an escape counts only when it is one of the
35
+ * punctuation characters every mode reads as itself (`\d`, `\n` or `A`
36
+ * bail), and any surrogate bails, so the code-point matching the `u` flag does
37
+ * can never differ from the code-unit comparison that replaces it. Without the
38
+ * `m` flag, which no generated regex carries, `^` and `$` anchor to the ends of
39
+ * the whole string and nothing else, so `startsWith`, `endsWith` and `===` are
40
+ * exactly the same test.
41
+ */
42
+ export declare const literalPattern: (pattern: string) => {
43
+ readonly text: string;
44
+ readonly start: boolean;
45
+ readonly end: boolean;
46
+ } | null;
47
+ /**
48
+ * A boolean expression testing the string `subject` against `pattern`: a plain
49
+ * string comparison when the pattern is only literal characters, a regex
50
+ * literal's `.test` otherwise.
51
+ *
52
+ * Generated code tests the same few patterns on every key of every object —
53
+ * `^x-` for OpenAPI extensions, `^\/` for paths — and a regex costs several
54
+ * times what `startsWith` does for the same answer, more again inside the loop,
55
+ * where the literal is re-created on each pass. `subject` must be an expression
56
+ * that is already known to be a string, which every key and every `typeof`
57
+ * -guarded value is.
58
+ *
59
+ * @example
60
+ * regexTest('^x-', 'key') // → 'key.startsWith("x-")'
61
+ * regexTest('^\\d+$', 'key') // → '/^\\d+$/u.test(key)'
62
+ */
63
+ export declare const regexTest: (pattern: string, subject: string) => string;
@@ -62,8 +62,60 @@ const escapeRegexPattern = (pattern) => {
62
62
  };
63
63
  const regexFlagsFor = (pattern) => compileMode(pattern) ?? "";
64
64
  const regexLiteral = (pattern) => `/${escapeRegexPattern(pattern)}/${regexFlagsFor(pattern)}`;
65
+ const ESCAPABLE_PUNCTUATION = /* @__PURE__ */ new Set(["^", "$", "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|", "/", "-"]);
66
+ const METACHARACTERS = /* @__PURE__ */ new Set(["^", "$", "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|"]);
67
+ const literalPattern = (pattern) => {
68
+ const start = pattern.startsWith("^");
69
+ let body = start ? pattern.slice(1) : pattern;
70
+ let end = false;
71
+ if (body.endsWith("$")) {
72
+ let slashes = 0;
73
+ for (let i = body.length - 2; i >= 0 && body[i] === "\\"; i--)
74
+ slashes++;
75
+ if (slashes % 2 === 0) {
76
+ end = true;
77
+ body = body.slice(0, -1);
78
+ }
79
+ }
80
+ let text = "";
81
+ for (let i = 0; i < body.length; i++) {
82
+ const char = body[i];
83
+ const code = char.charCodeAt(0);
84
+ if (code >= 55296 && code <= 57343)
85
+ return null;
86
+ if (char === "\\") {
87
+ const next = body[i + 1];
88
+ if (next === void 0 || !ESCAPABLE_PUNCTUATION.has(next))
89
+ return null;
90
+ text += next;
91
+ i++;
92
+ continue;
93
+ }
94
+ if (METACHARACTERS.has(char))
95
+ return null;
96
+ text += char;
97
+ }
98
+ if (text === "" && !(start && end))
99
+ return null;
100
+ return { text, start, end };
101
+ };
102
+ const regexTest = (pattern, subject) => {
103
+ const literal = literalPattern(pattern);
104
+ if (literal === null)
105
+ return `${regexLiteral(pattern)}.test(${subject})`;
106
+ const text = JSON.stringify(literal.text);
107
+ if (literal.start && literal.end)
108
+ return `(${subject} === ${text})`;
109
+ if (literal.start)
110
+ return `${subject}.startsWith(${text})`;
111
+ if (literal.end)
112
+ return `${subject}.endsWith(${text})`;
113
+ return `${subject}.includes(${text})`;
114
+ };
65
115
  export {
66
116
  escapeRegexPattern,
117
+ literalPattern,
67
118
  regexFlagsFor,
68
- regexLiteral
119
+ regexLiteral,
120
+ regexTest
69
121
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.25.0",
3
+ "version": "0.26.1",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -41,7 +41,7 @@
41
41
  "test": "NODE_ENV=production vitest run --root ../.. helpers"
42
42
  },
43
43
  "dependencies": {
44
- "json-schema-typed": "^8.0.1"
44
+ "json-schema-typed": "^9.0.0"
45
45
  },
46
46
  "exports": {
47
47
  "./package.json": "./package.json",