@amritk/helpers 0.9.0 → 0.10.0

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.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Default key-count cutoff below which an unknown-key sweep inlines `!==`
3
+ * comparisons instead of a hoisted `Set`. Chosen so typical schema objects stay
4
+ * on the faster inline path while pathologically wide objects fall back to the
5
+ * `Set`'s O(1) lookup.
6
+ */
7
+ export declare const INLINE_KEY_LIMIT = 16;
8
+ /** The pieces a caller needs to emit an `additionalProperties: false` sweep. */
9
+ export type UnknownKeyCheck = {
10
+ /**
11
+ * Declarations to emit once before the sweep — a single hoisted `Set` when the
12
+ * key count exceeds the inline limit, otherwise empty (the inline form is
13
+ * stateless). Each entry is a full statement with no trailing `;` or newline,
14
+ * so callers can punctuate to match their surrounding style.
15
+ */
16
+ readonly declarations: readonly string[];
17
+ /**
18
+ * Builds the boolean expression that is true when `keyVar` is NOT one of the
19
+ * known keys — an inline chain of `!==` comparisons, a `Set.has` miss, or the
20
+ * constant `true` when there are no known keys (every key is undeclared).
21
+ */
22
+ readonly isUnknown: (keyVar: string) => string;
23
+ /**
24
+ * The complement of {@link isUnknown}: true when `keyVar` IS a known key — an
25
+ * inline chain of `===` comparisons, a `Set.has` hit, or the constant `false`
26
+ * when there are no known keys.
27
+ */
28
+ readonly isKnown: (keyVar: string) => string;
29
+ };
30
+ /**
31
+ * Builds the "is this an undeclared key" test used by `additionalProperties:
32
+ * false` sweeps in the generated parsers and validators.
33
+ *
34
+ * For a small number of known keys V8 evaluates a chain of `!==` string
35
+ * comparisons faster than `Set.has` (which has to hash the string), and the
36
+ * inline form skips the per-module `Set` allocation — the same shape Ajv and
37
+ * TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
38
+ * `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
39
+ *
40
+ * @example
41
+ * const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
42
+ * check.declarations // []
43
+ * check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
44
+ * check.isKnown('_k') // '_k === "id" || _k === "name"'
45
+ */
46
+ export declare const unknownKeyCheck: (knownKeys: readonly string[], setName: string, inlineLimit?: number) => UnknownKeyCheck;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Default key-count cutoff below which an unknown-key sweep inlines `!==`
3
+ * comparisons instead of a hoisted `Set`. Chosen so typical schema objects stay
4
+ * on the faster inline path while pathologically wide objects fall back to the
5
+ * `Set`'s O(1) lookup.
6
+ */
7
+ export const INLINE_KEY_LIMIT = 16;
8
+ /**
9
+ * Builds the "is this an undeclared key" test used by `additionalProperties:
10
+ * false` sweeps in the generated parsers and validators.
11
+ *
12
+ * For a small number of known keys V8 evaluates a chain of `!==` string
13
+ * comparisons faster than `Set.has` (which has to hash the string), and the
14
+ * inline form skips the per-module `Set` allocation — the same shape Ajv and
15
+ * TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
16
+ * `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
17
+ *
18
+ * @example
19
+ * const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
20
+ * check.declarations // []
21
+ * check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
22
+ * check.isKnown('_k') // '_k === "id" || _k === "name"'
23
+ */
24
+ export const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
25
+ if (knownKeys.length === 0) {
26
+ return { declarations: [], isUnknown: () => 'true', isKnown: () => 'false' };
27
+ }
28
+ if (knownKeys.length <= inlineLimit) {
29
+ return {
30
+ declarations: [],
31
+ isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(' && '),
32
+ isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(' || '),
33
+ };
34
+ }
35
+ return {
36
+ declarations: [`const ${setName} = new Set(${JSON.stringify(knownKeys)})`],
37
+ isUnknown: (keyVar) => `!${setName}.has(${keyVar})`,
38
+ isKnown: (keyVar) => `${setName}.has(${keyVar})`,
39
+ };
40
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "license": "MIT",