@amritk/helpers 0.9.0 → 0.10.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.
|
@@ -68,11 +68,21 @@ const isObjectLikeSchema = (schema) => {
|
|
|
68
68
|
const getBooleanSubSchemaType = (schema) => {
|
|
69
69
|
return schema ? 'unknown' : 'never';
|
|
70
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Renders a possibly multi-line description as JSDoc body lines, prefixing each
|
|
73
|
+
* line with `${indent}* `. Blank lines become a bare `${indent}*` so we never
|
|
74
|
+
* emit trailing whitespace. Without this, embedded newlines would leave
|
|
75
|
+
* continuation lines unprefixed and break the comment block.
|
|
76
|
+
*/
|
|
77
|
+
const renderJsDocBody = (description, indent) => description
|
|
78
|
+
.split('\n')
|
|
79
|
+
.map((line) => (line.length > 0 ? `${indent}* ${line}\n` : `${indent}*\n`))
|
|
80
|
+
.join('');
|
|
71
81
|
const buildJsDocBlock = (title, description, commentUrl) => {
|
|
72
82
|
let block = `/**\n`;
|
|
73
83
|
block += `* ${title}\n`;
|
|
74
84
|
block += `*\n`;
|
|
75
|
-
block +=
|
|
85
|
+
block += renderJsDocBody(description, '');
|
|
76
86
|
if (commentUrl?.startsWith('http')) {
|
|
77
87
|
block += `* \n`;
|
|
78
88
|
block += `* @see {@link ${commentUrl}}\n`;
|
|
@@ -80,6 +90,19 @@ const buildJsDocBlock = (title, description, commentUrl) => {
|
|
|
80
90
|
block += `*/\n`;
|
|
81
91
|
return block;
|
|
82
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* Builds the inline JSDoc comment that precedes a generated property. A
|
|
95
|
+
* single-line description stays compact (` /** text *\/`); a multi-line
|
|
96
|
+
* description expands into an asterisk-prefixed block so every line is
|
|
97
|
+
* commented. The returned string always ends with a newline, ready for the
|
|
98
|
+
* property declaration to follow.
|
|
99
|
+
*/
|
|
100
|
+
const buildInlinePropertyComment = (description) => {
|
|
101
|
+
if (!description.includes('\n')) {
|
|
102
|
+
return ` /** ${description} */\n`;
|
|
103
|
+
}
|
|
104
|
+
return ` /**\n${renderJsDocBody(description, ' ')} */\n`;
|
|
105
|
+
};
|
|
83
106
|
/**
|
|
84
107
|
* Converts a JSON Schema type to its TypeScript equivalent, applying any
|
|
85
108
|
* `x-mjst` brand. Branding is type-level only, so we compute the underlying
|
|
@@ -299,9 +322,8 @@ const getUnbrandedType = (schema, options = {}) => {
|
|
|
299
322
|
first = false;
|
|
300
323
|
if (inlineDescription) {
|
|
301
324
|
properties +=
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
' */\n ' +
|
|
325
|
+
buildInlinePropertyComment(inlineDescription) +
|
|
326
|
+
' ' +
|
|
305
327
|
readonlyPrefix +
|
|
306
328
|
safeKey(key) +
|
|
307
329
|
optional +
|
|
@@ -444,7 +466,8 @@ export const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
444
466
|
? propSchema.$comment
|
|
445
467
|
: undefined;
|
|
446
468
|
if (inlineDescription) {
|
|
447
|
-
properties +=
|
|
469
|
+
properties +=
|
|
470
|
+
buildInlinePropertyComment(inlineDescription) + ' ' + quotedKey + optional + ': ' + propType + ';';
|
|
448
471
|
}
|
|
449
472
|
else {
|
|
450
473
|
properties += ' ' + quotedKey + optional + ': ' + propType + ';';
|
|
@@ -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
|
+
};
|