@amritk/helpers 0.2.2 → 0.3.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,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derives the root type name for a generated schema from its `title` keyword.
|
|
3
|
+
*
|
|
4
|
+
* We name the root after the schema itself instead of a generic "Document" so
|
|
5
|
+
* the generated types and parsers read naturally (e.g. an OpenAPI schema yields
|
|
6
|
+
* `OpenApi` / `parseOpenApi`). When the schema has no usable `title`, we fall
|
|
7
|
+
* back to "Document" to keep output deterministic.
|
|
8
|
+
*
|
|
9
|
+
* @param schema - The root JSON Schema. A boolean schema or one without a
|
|
10
|
+
* string `title` falls back to the default.
|
|
11
|
+
* @returns A PascalCase type name derived from the title, or "Document".
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
|
|
16
|
+
* deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
|
|
17
|
+
* deriveRootTypeName({ type: 'object' }) // 'Document'
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare const deriveRootTypeName: (schema: unknown) => string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/is-object.ts
|
|
2
|
+
var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
3
|
+
|
|
4
|
+
// src/derive-root-type-name.ts
|
|
5
|
+
var titleToPascalCase = (title) => {
|
|
6
|
+
const words = title.split(/[^a-zA-Z0-9]+/).filter(Boolean);
|
|
7
|
+
const pascal = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
8
|
+
return pascal.replace(/^\d+/, "");
|
|
9
|
+
};
|
|
10
|
+
var deriveRootTypeName = (schema) => {
|
|
11
|
+
if (!isObject(schema) || typeof schema["title"] !== "string") {
|
|
12
|
+
return "Document";
|
|
13
|
+
}
|
|
14
|
+
const name = titleToPascalCase(schema["title"]);
|
|
15
|
+
return name || "Document";
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
deriveRootTypeName
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Shared utilities for the mjst code generation ecosystem.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "bun run build:code && bun run build:types",
|
|
31
|
-
"build:code": "bun build ./src/build-dynamic-ref-map.ts ./src/extract-refs.ts ./src/generate-type-definition.ts ./src/has-ref.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",
|
|
31
|
+
"build:code": "bun build ./src/build-dynamic-ref-map.ts ./src/derive-root-type-name.ts ./src/extract-refs.ts ./src/generate-type-definition.ts ./src/has-ref.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",
|
|
32
32
|
"build:types": "tsc -p ."
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
@@ -41,6 +41,11 @@
|
|
|
41
41
|
"default": "./dist/build-dynamic-ref-map.js",
|
|
42
42
|
"types": "./dist/build-dynamic-ref-map.d.ts"
|
|
43
43
|
},
|
|
44
|
+
"./derive-root-type-name": {
|
|
45
|
+
"bun": "./src/derive-root-type-name.ts",
|
|
46
|
+
"default": "./dist/derive-root-type-name.js",
|
|
47
|
+
"types": "./dist/derive-root-type-name.d.ts"
|
|
48
|
+
},
|
|
44
49
|
"./extract-refs": {
|
|
45
50
|
"bun": "./src/extract-refs.ts",
|
|
46
51
|
"default": "./dist/extract-refs.js",
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { deriveRootTypeName } from './derive-root-type-name'
|
|
4
|
+
|
|
5
|
+
describe('derive-root-type-name', () => {
|
|
6
|
+
it('derives a PascalCase name from a single-word title', () => {
|
|
7
|
+
expect(deriveRootTypeName({ title: 'Document' })).toBe('Document')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('joins multi-word titles into PascalCase', () => {
|
|
11
|
+
expect(deriveRootTypeName({ title: 'OpenAPI Document' })).toBe('OpenAPIDocument')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('preserves acronyms intact', () => {
|
|
15
|
+
expect(deriveRootTypeName({ title: 'JSON Schema' })).toBe('JSONSchema')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('splits on non-alphanumeric separators', () => {
|
|
19
|
+
expect(deriveRootTypeName({ title: 'my-config_file.spec' })).toBe('MyConfigFileSpec')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('drops leading digits so the name is a valid identifier', () => {
|
|
23
|
+
expect(deriveRootTypeName({ title: '3 amigos' })).toBe('Amigos')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('falls back to Document when the title is missing', () => {
|
|
27
|
+
expect(deriveRootTypeName({ type: 'object' })).toBe('Document')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('falls back to Document when the title is not a string', () => {
|
|
31
|
+
expect(deriveRootTypeName({ title: 42 })).toBe('Document')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('falls back to Document for a title with no usable characters', () => {
|
|
35
|
+
expect(deriveRootTypeName({ title: ' --- ' })).toBe('Document')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('falls back to Document for boolean schemas', () => {
|
|
39
|
+
expect(deriveRootTypeName(true)).toBe('Document')
|
|
40
|
+
expect(deriveRootTypeName(false)).toBe('Document')
|
|
41
|
+
})
|
|
42
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isObject } from './is-object'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts an arbitrary title string into a PascalCase TypeScript identifier.
|
|
5
|
+
* Splits on any run of non-alphanumeric characters, capitalizes the first
|
|
6
|
+
* letter of each word while preserving the rest (so acronyms like "API" or
|
|
7
|
+
* "JSON" survive intact), and drops leading digits since an identifier may not
|
|
8
|
+
* start with a number.
|
|
9
|
+
*/
|
|
10
|
+
const titleToPascalCase = (title: string): string => {
|
|
11
|
+
const words = title.split(/[^a-zA-Z0-9]+/).filter(Boolean)
|
|
12
|
+
const pascal = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join('')
|
|
13
|
+
return pascal.replace(/^\d+/, '')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Derives the root type name for a generated schema from its `title` keyword.
|
|
18
|
+
*
|
|
19
|
+
* We name the root after the schema itself instead of a generic "Document" so
|
|
20
|
+
* the generated types and parsers read naturally (e.g. an OpenAPI schema yields
|
|
21
|
+
* `OpenApi` / `parseOpenApi`). When the schema has no usable `title`, we fall
|
|
22
|
+
* back to "Document" to keep output deterministic.
|
|
23
|
+
*
|
|
24
|
+
* @param schema - The root JSON Schema. A boolean schema or one without a
|
|
25
|
+
* string `title` falls back to the default.
|
|
26
|
+
* @returns A PascalCase type name derived from the title, or "Document".
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
|
|
31
|
+
* deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
|
|
32
|
+
* deriveRootTypeName({ type: 'object' }) // 'Document'
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export const deriveRootTypeName = (schema: unknown): string => {
|
|
36
|
+
if (!isObject(schema) || typeof schema['title'] !== 'string') {
|
|
37
|
+
return 'Document'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const name = titleToPascalCase(schema['title'])
|
|
41
|
+
return name || 'Document'
|
|
42
|
+
}
|