@amritk/helpers 0.15.4 → 0.16.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.
- package/dist/build-dynamic-ref-map.js +2 -2
- package/dist/build-resource-registry.js +2 -3
- package/dist/derive-root-type-name.js +9 -5
- package/dist/escape-regex-pattern.js +42 -35
- package/dist/extract-refs.js +3 -2
- package/dist/fold-nullable.js +6 -4
- package/dist/generate-index-barrel.js +7 -11
- package/dist/generate-type-definition.js +153 -135
- package/dist/graft-external-schemas.js +3 -2
- package/dist/max-schema-depth.d.ts +8 -1
- package/dist/max-schema-depth.js +3 -3
- package/dist/mjst-extension.js +3 -2
- package/dist/parse-documentation.js +1 -1
- package/dist/prune-external-schemas.js +3 -2
- package/dist/quote-js-string.js +3 -1
- package/dist/read-key.d.ts +12 -0
- package/dist/read-key.js +2 -0
- package/dist/ref-to-name.d.ts +41 -0
- package/dist/ref-to-name.js +11 -5
- package/dist/resolve-dynamic-refs.js +7 -7
- package/dist/safe-accessor.d.ts +1 -1
- package/dist/safe-accessor.js +1 -1
- package/dist/string-length-check.js +2 -2
- package/dist/unknown-key-check.d.ts +14 -3
- package/dist/unknown-key-check.js +2 -2
- package/dist/upgrade-draft07-schema.js +6 -2
- package/dist/walk-ref-graph.js +42 -29
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@ import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
|
3
3
|
import { readKey } from "./read-key.js";
|
|
4
4
|
import { isSchemaObject } from "./schema-guards.js";
|
|
5
5
|
const buildDynamicRefMap = (rootSchema) => {
|
|
6
|
-
const map =
|
|
6
|
+
const map = /* @__PURE__ */ Object.create(null);
|
|
7
7
|
if (!isSchemaObject(rootSchema))
|
|
8
8
|
return map;
|
|
9
9
|
const walk = (node, pointer, depth, inSchemaMap) => {
|
|
@@ -17,7 +17,7 @@ const buildDynamicRefMap = (rootSchema) => {
|
|
|
17
17
|
}
|
|
18
18
|
const record = node;
|
|
19
19
|
const anchor = readKey(record, "$dynamicAnchor");
|
|
20
|
-
if (typeof anchor === "string" && !(`#${anchor}`
|
|
20
|
+
if (typeof anchor === "string" && !Object.hasOwn(map, `#${anchor}`)) {
|
|
21
21
|
map[`#${anchor}`] = `#${pointer}`;
|
|
22
22
|
}
|
|
23
23
|
for (const key of Object.keys(record)) {
|
|
@@ -45,9 +45,8 @@ const registries = /* @__PURE__ */ new WeakMap();
|
|
|
45
45
|
const buildResourceRegistry = (root) => {
|
|
46
46
|
if (root === null || typeof root !== "object")
|
|
47
47
|
return null;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return memoized ?? null;
|
|
48
|
+
if (registries.has(root))
|
|
49
|
+
return registries.get(root) ?? null;
|
|
51
50
|
const resources = /* @__PURE__ */ new Map();
|
|
52
51
|
const anchors = /* @__PURE__ */ new Map();
|
|
53
52
|
const dynamicAnchors = /* @__PURE__ */ new Map();
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { isObject } from "./is-object.js";
|
|
2
|
+
import { readKey } from "./read-key.js";
|
|
3
|
+
import { isIdentifierStart, toPascalWords } from "./ref-to-name.js";
|
|
2
4
|
const titleToPascalCase = (title) => {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
const pascal = toPascalWords(title).replace(/^\d+/, "");
|
|
6
|
+
if (pascal === "" || isIdentifierStart(pascal))
|
|
7
|
+
return pascal;
|
|
8
|
+
return `_${pascal}`;
|
|
6
9
|
};
|
|
7
10
|
const deriveRootTypeName = (schema, fallbackName) => {
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
const title = isObject(schema) ? readKey(schema, "title") : void 0;
|
|
12
|
+
if (typeof title === "string") {
|
|
13
|
+
const fromTitle = titleToPascalCase(title);
|
|
10
14
|
if (fromTitle)
|
|
11
15
|
return fromTitle;
|
|
12
16
|
}
|
|
@@ -8,52 +8,59 @@ const lineTerminatorEscapes = {
|
|
|
8
8
|
8233: "\\u2029"
|
|
9
9
|
// PARAGRAPH SEPARATOR
|
|
10
10
|
};
|
|
11
|
+
const unicodeEscape = (code) => `\\u${code.toString(16).padStart(4, "0")}`;
|
|
11
12
|
const escapeCache = /* @__PURE__ */ new Map();
|
|
12
|
-
const
|
|
13
|
+
const modeCache = /* @__PURE__ */ new Map();
|
|
13
14
|
const ESCAPE_CACHE_LIMIT = 1e3;
|
|
14
|
-
const
|
|
15
|
-
|
|
15
|
+
const evictIfFull = (cache) => {
|
|
16
|
+
if (cache.size >= ESCAPE_CACHE_LIMIT)
|
|
17
|
+
cache.delete(cache.keys().next().value);
|
|
18
|
+
};
|
|
19
|
+
const compileMode = (pattern) => {
|
|
20
|
+
const cached = modeCache.get(pattern);
|
|
16
21
|
if (cached !== void 0)
|
|
17
22
|
return cached;
|
|
23
|
+
let mode;
|
|
18
24
|
try {
|
|
19
|
-
new RegExp(pattern);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return terminatorEscape;
|
|
29
|
-
return match;
|
|
30
|
-
});
|
|
31
|
-
if (escaped === "") {
|
|
32
|
-
escapeCache.set(pattern, "(?:)");
|
|
33
|
-
return "(?:)";
|
|
34
|
-
}
|
|
35
|
-
if (escapeCache.size >= ESCAPE_CACHE_LIMIT) {
|
|
36
|
-
escapeCache.delete(escapeCache.keys().next().value);
|
|
25
|
+
new RegExp(pattern, "u");
|
|
26
|
+
mode = "u";
|
|
27
|
+
} catch {
|
|
28
|
+
try {
|
|
29
|
+
new RegExp(pattern);
|
|
30
|
+
mode = "";
|
|
31
|
+
} catch {
|
|
32
|
+
mode = null;
|
|
33
|
+
}
|
|
37
34
|
}
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
evictIfFull(modeCache);
|
|
36
|
+
modeCache.set(pattern, mode);
|
|
37
|
+
return mode;
|
|
40
38
|
};
|
|
41
|
-
const
|
|
42
|
-
const cached =
|
|
39
|
+
const escapeRegexPattern = (pattern) => {
|
|
40
|
+
const cached = escapeCache.get(pattern);
|
|
43
41
|
if (cached !== void 0)
|
|
44
42
|
return cached;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
if (compileMode(pattern) === null) {
|
|
44
|
+
try {
|
|
45
|
+
new RegExp(pattern, "u");
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(`Invalid regex pattern ${JSON.stringify(pattern)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
51
49
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
const escaped = pattern.replace(/\\[\s\S]|[/\n\r\u2028\u2029]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, (match) => {
|
|
51
|
+
if (match.length !== 1)
|
|
52
|
+
return match;
|
|
53
|
+
if (match === "/")
|
|
54
|
+
return "\\/";
|
|
55
|
+
const code = match.charCodeAt(0);
|
|
56
|
+
return lineTerminatorEscapes[code] ?? unicodeEscape(code);
|
|
57
|
+
});
|
|
58
|
+
const result = escaped === "" ? "(?:)" : escaped;
|
|
59
|
+
evictIfFull(escapeCache);
|
|
60
|
+
escapeCache.set(pattern, result);
|
|
61
|
+
return result;
|
|
56
62
|
};
|
|
63
|
+
const regexFlagsFor = (pattern) => compileMode(pattern) ?? "";
|
|
57
64
|
const regexLiteral = (pattern) => `/${escapeRegexPattern(pattern)}/${regexFlagsFor(pattern)}`;
|
|
58
65
|
export {
|
|
59
66
|
escapeRegexPattern,
|
package/dist/extract-refs.js
CHANGED
|
@@ -24,8 +24,9 @@ const extractRefs = (schema) => {
|
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
26
|
const record = obj;
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
const ref = inSchemaMap ? void 0 : readKey(record, "$ref");
|
|
28
|
+
if (typeof ref === "string" && isResolvableRef(ref)) {
|
|
29
|
+
refs.add(ref);
|
|
29
30
|
}
|
|
30
31
|
for (const key of Object.keys(record)) {
|
|
31
32
|
if (isDataPosition(key, inSchemaMap))
|
package/dist/fold-nullable.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { assignKey } from "./assign-key.js";
|
|
2
2
|
import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
|
|
3
|
+
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
4
|
const foldNullable = (schema) => {
|
|
4
|
-
const folded = fold(schema);
|
|
5
|
+
const folded = fold(schema, 0);
|
|
5
6
|
return folded;
|
|
6
7
|
};
|
|
7
|
-
const fold = (node, inSchemaMap = false) => {
|
|
8
|
+
const fold = (node, depth, inSchemaMap = false) => {
|
|
9
|
+
assertSchemaDepth(depth, "foldNullable");
|
|
8
10
|
if (Array.isArray(node)) {
|
|
9
11
|
let changed2 = false;
|
|
10
12
|
const next2 = node.map((item) => {
|
|
11
|
-
const folded = fold(item, inSchemaMap);
|
|
13
|
+
const folded = fold(item, depth + 1, inSchemaMap);
|
|
12
14
|
if (folded !== item)
|
|
13
15
|
changed2 = true;
|
|
14
16
|
return folded;
|
|
@@ -25,7 +27,7 @@ const fold = (node, inSchemaMap = false) => {
|
|
|
25
27
|
for (const key of Object.keys(record)) {
|
|
26
28
|
if (isDataPosition(key, inSchemaMap))
|
|
27
29
|
continue;
|
|
28
|
-
const folded = fold(record[key], entersSchemaMap(key, inSchemaMap));
|
|
30
|
+
const folded = fold(record[key], depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
29
31
|
if (folded !== record[key])
|
|
30
32
|
changed = true;
|
|
31
33
|
assignKey(next, key, folded);
|
|
@@ -19,25 +19,21 @@ const exportNameAt = (content, at, prefix) => {
|
|
|
19
19
|
return end > at + prefix.length ? content.slice(at + prefix.length, end) : null;
|
|
20
20
|
};
|
|
21
21
|
const isLineTerminator = (code) => code === 10 || code === 13 || code === 8232 || code === 8233;
|
|
22
|
+
const EXPORT_KEYWORD = "export ";
|
|
22
23
|
const collectExportNames = (content, typeNames, constNames) => {
|
|
23
|
-
let
|
|
24
|
-
while (
|
|
25
|
-
if (
|
|
26
|
-
const typeName = exportNameAt(content,
|
|
24
|
+
let at = content.indexOf(EXPORT_KEYWORD);
|
|
25
|
+
while (at !== -1) {
|
|
26
|
+
if (at === 0 || isLineTerminator(content.charCodeAt(at - 1))) {
|
|
27
|
+
const typeName = exportNameAt(content, at, "export type ");
|
|
27
28
|
if (typeName !== null) {
|
|
28
29
|
typeNames.push(typeName);
|
|
29
30
|
} else {
|
|
30
|
-
const constName = exportNameAt(content,
|
|
31
|
+
const constName = exportNameAt(content, at, "export const ");
|
|
31
32
|
if (constName !== null)
|
|
32
33
|
constNames.push(constName);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
while (next < content.length && !isLineTerminator(content.charCodeAt(next)))
|
|
37
|
-
next++;
|
|
38
|
-
if (next >= content.length)
|
|
39
|
-
break;
|
|
40
|
-
lineStart = content.charCodeAt(next) === 13 && content.charCodeAt(next + 1) === 10 ? next + 2 : next + 1;
|
|
36
|
+
at = content.indexOf(EXPORT_KEYWORD, at + EXPORT_KEYWORD.length);
|
|
41
37
|
}
|
|
42
38
|
};
|
|
43
39
|
const generateIndexBarrel = (files, options = {}) => {
|