@amritk/helpers 0.1.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,138 @@
1
+ // src/ref-to-filename.ts
2
+ var toKebabCase = (value) => value.replace(/OAuth/g, "Oauth").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
3
+ var uriRefToFilename = (uri) => {
4
+ const hashIndex = uri.indexOf("#");
5
+ const baseUri = hashIndex === -1 ? uri : uri.slice(0, hashIndex);
6
+ const fragment = hashIndex === -1 ? "" : uri.slice(hashIndex + 1);
7
+ const withoutProtocol = baseUri.replace(/^https?:\/\/[^/]+\//, "");
8
+ const withoutExt = withoutProtocol.replace(/\.json$/, "");
9
+ const rawSegments = withoutExt.split("/");
10
+ const SKIP_KEYS = new Set(["definitions", "$defs"]);
11
+ const segments = [];
12
+ for (let i = 0;i < rawSegments.length; i++) {
13
+ const s = rawSegments[i];
14
+ if (SKIP_KEYS.has(s))
15
+ continue;
16
+ const prevRaw = rawSegments[i - 1];
17
+ if (/^\d+\.\d+/.test(s) && prevRaw !== undefined && SKIP_KEYS.has(prevRaw))
18
+ continue;
19
+ segments.push(s);
20
+ }
21
+ const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, "-")).join("-");
22
+ if (!fragment)
23
+ return baseName;
24
+ const fragSegments = fragment.split("/").filter((s) => s && !SKIP_KEYS.has(s) && s !== "properties");
25
+ const fragLast = fragSegments[fragSegments.length - 1];
26
+ if (!fragLast)
27
+ return baseName;
28
+ return `${baseName}-${toKebabCase(fragLast)}`;
29
+ };
30
+ var refToFilename = (ref) => {
31
+ if (ref.startsWith("http://") || ref.startsWith("https://")) {
32
+ return uriRefToFilename(ref);
33
+ }
34
+ const segments = ref.split("/");
35
+ let filename = segments[segments.length - 1];
36
+ if (/[A-Z]/.test(filename)) {
37
+ filename = toKebabCase(filename);
38
+ }
39
+ return filename;
40
+ };
41
+
42
+ // src/upgrade-draft07-schema.ts
43
+ var isDraft07Schema = (schema) => typeof schema["$schema"] === "string" && schema["$schema"].includes("draft-07");
44
+ var rewriteRefs = (obj, refMap, selfRef) => {
45
+ if (typeof obj !== "object" || obj === null)
46
+ return obj;
47
+ if (Array.isArray(obj))
48
+ return obj.map((item) => rewriteRefs(item, refMap, selfRef));
49
+ const record = obj;
50
+ const result = {};
51
+ for (const [key, value] of Object.entries(record)) {
52
+ if (key === "$ref" && typeof value === "string") {
53
+ if (refMap.has(value)) {
54
+ result[key] = refMap.get(value);
55
+ } else if (value === "#" && selfRef) {
56
+ result[key] = selfRef;
57
+ } else {
58
+ result[key] = value;
59
+ }
60
+ } else {
61
+ result[key] = rewriteRefs(value, refMap, selfRef);
62
+ }
63
+ }
64
+ return result;
65
+ };
66
+ var hoistNestedDefs = (defs) => {
67
+ const hoisted = {};
68
+ for (const [parentName, parentSchema] of Object.entries(defs)) {
69
+ if (typeof parentSchema !== "object" || parentSchema === null) {
70
+ hoisted[parentName] = parentSchema;
71
+ continue;
72
+ }
73
+ const parentObj = parentSchema;
74
+ const nestedDefs = parentObj["$defs"];
75
+ if (!nestedDefs || typeof nestedDefs !== "object") {
76
+ hoisted[parentName] = parentSchema;
77
+ continue;
78
+ }
79
+ const parentPrefix = parentName.startsWith("http://") || parentName.startsWith("https://") ? refToFilename(parentName) : parentName;
80
+ const localToHoisted = new Map;
81
+ for (const localName of Object.keys(nestedDefs)) {
82
+ const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
83
+ localToHoisted.set(`#/$defs/${localName}`, `#/$defs/${hoistedName}`);
84
+ }
85
+ const selfRef = `#/$defs/${parentPrefix}`;
86
+ const rewrittenParent = rewriteRefs(parentObj, localToHoisted, selfRef);
87
+ hoisted[parentName] = rewrittenParent;
88
+ for (const [localName, localSchema] of Object.entries(nestedDefs)) {
89
+ const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
90
+ hoisted[hoistedName] = rewriteRefs(localSchema, localToHoisted, selfRef);
91
+ }
92
+ }
93
+ return hoisted;
94
+ };
95
+ var upgradeDraft07Schema = (schema) => {
96
+ if (!isDraft07Schema(schema))
97
+ return schema;
98
+ const { definitions, $schema: _, ...rest } = schema;
99
+ const rawDefs = definitions ?? {};
100
+ const renamedDefs = {};
101
+ for (const [key, value] of Object.entries(rawDefs)) {
102
+ renamedDefs[key] = renameNestedDefs(value);
103
+ }
104
+ const hoistedDefs = hoistNestedDefs(renamedDefs);
105
+ for (const key of Object.keys(hoistedDefs)) {
106
+ if (key.startsWith("http://") || key.startsWith("https://")) {
107
+ const shortName = refToFilename(key);
108
+ if (shortName && !(shortName in hoistedDefs)) {
109
+ hoistedDefs[shortName] = hoistedDefs[key];
110
+ }
111
+ }
112
+ }
113
+ return {
114
+ ...rest,
115
+ $defs: hoistedDefs
116
+ };
117
+ };
118
+ var renameNestedDefs = (obj) => {
119
+ if (typeof obj !== "object" || obj === null)
120
+ return obj;
121
+ if (Array.isArray(obj))
122
+ return obj.map(renameNestedDefs);
123
+ const record = obj;
124
+ const result = {};
125
+ for (const [key, value] of Object.entries(record)) {
126
+ if (key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/")) {
127
+ result[key] = value.replace("#/definitions/", "#/$defs/");
128
+ } else {
129
+ const outKey = key === "definitions" ? "$defs" : key;
130
+ result[outKey] = renameNestedDefs(value);
131
+ }
132
+ }
133
+ return result;
134
+ };
135
+ export {
136
+ upgradeDraft07Schema,
137
+ isDraft07Schema
138
+ };
@@ -0,0 +1,2 @@
1
+ /** Parses the items of an array with a parser function */
2
+ export declare const validateArray: (input: unknown, parser: (input: unknown) => unknown) => any[];
@@ -0,0 +1,15 @@
1
+ // src/validate-array.ts
2
+ var validateArray = (input, parser) => {
3
+ if (!Array.isArray(input)) {
4
+ return [];
5
+ }
6
+ const len = input.length;
7
+ const result = new Array(len);
8
+ for (let i = 0;i < len; i++) {
9
+ result[i] = parser(input[i]);
10
+ }
11
+ return result;
12
+ };
13
+ export {
14
+ validateArray
15
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Parses the values of a record with a parser function.
3
+ * Uses for...in instead of Object.entries() to avoid allocating an intermediate array.
4
+ */
5
+ export declare const validateRecord: (input: unknown, parser: (input: unknown) => unknown) => Record<string, unknown>;
@@ -0,0 +1,18 @@
1
+ // src/is-object.ts
2
+ var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
3
+
4
+ // src/validate-record.ts
5
+ var validateRecord = (input, parser) => {
6
+ if (!isObject(input)) {
7
+ return {};
8
+ }
9
+ const record = input;
10
+ const result = {};
11
+ for (const key in record) {
12
+ result[key] = parser(record[key]);
13
+ }
14
+ return result;
15
+ };
16
+ export {
17
+ validateRecord
18
+ };
package/package.json ADDED
@@ -0,0 +1,105 @@
1
+ {
2
+ "name": "@amritk/helpers",
3
+ "version": "0.1.0",
4
+ "description": "Shared utilities for the mjst code generation ecosystem.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "amritk",
8
+ "keywords": [
9
+ "json-schema",
10
+ "typescript",
11
+ "mjst"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/amritk/mjst.git",
16
+ "directory": "packages/helpers"
17
+ },
18
+ "homepage": "https://github.com/amritk/mjst/tree/main/packages/helpers#readme",
19
+ "bugs": {
20
+ "url": "https://github.com/amritk/mjst/issues"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "scripts": {
26
+ "build": "bun run build:code && bun run build:types",
27
+ "build:code": "bun build ./src/build-dynamic-ref-map.ts ./src/extract-refs.ts ./src/generate-type-definition.ts ./src/is-object.ts ./src/parse-documentation.ts ./src/ref-to-filename.ts ./src/ref-to-name.ts ./src/resolve-dynamic-refs.ts ./src/resolve-ref.ts ./src/safe-accessor.ts ./src/schema-guards.ts ./src/upgrade-draft07-schema.ts ./src/validate-array.ts ./src/validate-record.ts --outdir=dist --root=src --target=node",
28
+ "build:types": "tsc -p ."
29
+ },
30
+ "dependencies": {
31
+ "json-schema-typed": "catalog:"
32
+ },
33
+ "exports": {
34
+ "./build-dynamic-ref-map": {
35
+ "bun": "./src/build-dynamic-ref-map.ts",
36
+ "default": "./dist/build-dynamic-ref-map.js",
37
+ "types": "./dist/build-dynamic-ref-map.d.ts"
38
+ },
39
+ "./extract-refs": {
40
+ "bun": "./src/extract-refs.ts",
41
+ "default": "./dist/extract-refs.js",
42
+ "types": "./dist/extract-refs.d.ts"
43
+ },
44
+ "./generate-type-definition": {
45
+ "bun": "./src/generate-type-definition.ts",
46
+ "default": "./dist/generate-type-definition.js",
47
+ "types": "./dist/generate-type-definition.d.ts"
48
+ },
49
+ "./is-object": {
50
+ "bun": "./src/is-object.ts",
51
+ "default": "./dist/is-object.js",
52
+ "types": "./dist/is-object.d.ts"
53
+ },
54
+ "./parse-documentation": {
55
+ "bun": "./src/parse-documentation.ts",
56
+ "default": "./dist/parse-documentation.js",
57
+ "types": "./dist/parse-documentation.d.ts"
58
+ },
59
+ "./ref-to-filename": {
60
+ "bun": "./src/ref-to-filename.ts",
61
+ "default": "./dist/ref-to-filename.js",
62
+ "types": "./dist/ref-to-filename.d.ts"
63
+ },
64
+ "./ref-to-name": {
65
+ "bun": "./src/ref-to-name.ts",
66
+ "default": "./dist/ref-to-name.js",
67
+ "types": "./dist/ref-to-name.d.ts"
68
+ },
69
+ "./resolve-dynamic-refs": {
70
+ "bun": "./src/resolve-dynamic-refs.ts",
71
+ "default": "./dist/resolve-dynamic-refs.js",
72
+ "types": "./dist/resolve-dynamic-refs.d.ts"
73
+ },
74
+ "./resolve-ref": {
75
+ "bun": "./src/resolve-ref.ts",
76
+ "default": "./dist/resolve-ref.js",
77
+ "types": "./dist/resolve-ref.d.ts"
78
+ },
79
+ "./safe-accessor": {
80
+ "bun": "./src/safe-accessor.ts",
81
+ "default": "./dist/safe-accessor.js",
82
+ "types": "./dist/safe-accessor.d.ts"
83
+ },
84
+ "./schema-guards": {
85
+ "bun": "./src/schema-guards.ts",
86
+ "default": "./dist/schema-guards.js",
87
+ "types": "./dist/schema-guards.d.ts"
88
+ },
89
+ "./upgrade-draft07-schema": {
90
+ "bun": "./src/upgrade-draft07-schema.ts",
91
+ "default": "./dist/upgrade-draft07-schema.js",
92
+ "types": "./dist/upgrade-draft07-schema.d.ts"
93
+ },
94
+ "./validate-array": {
95
+ "bun": "./src/validate-array.ts",
96
+ "default": "./dist/validate-array.js",
97
+ "types": "./dist/validate-array.d.ts"
98
+ },
99
+ "./validate-record": {
100
+ "bun": "./src/validate-record.ts",
101
+ "default": "./dist/validate-record.js",
102
+ "types": "./dist/validate-record.d.ts"
103
+ }
104
+ }
105
+ }