@amritk/helpers 0.11.0 → 0.12.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/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  ![license](https://img.shields.io/badge/license-MIT-22c55e?style=flat-square) 
10
10
  ![JSON Schema](https://img.shields.io/badge/JSON%20Schema-2020--12-f97316?style=flat-square) 
11
11
  ![node](https://img.shields.io/badge/node-%E2%89%A520-339933?style=flat-square&logo=node.js&logoColor=white) 
12
- ![vibe coded](https://img.shields.io/badge/vibe%20coded-94%25-a855f7?style=flat-square)
12
+ ![vibe coded](https://img.shields.io/badge/vibe-coded-a855f7?style=flat-square)
13
13
 
14
14
  </div>
15
15
 
@@ -1,20 +1,27 @@
1
1
  /**
2
- * Derives the root type name for a generated schema from its `title` keyword.
2
+ * Derives the root type name for a generated schema.
3
3
  *
4
4
  * We name the root after the schema itself instead of a generic "Document" so
5
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.
6
+ * `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
7
+ * `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
8
+ * the caller can supply the schema's filename (without extension) as a
9
+ * `fallbackName` so the name still reflects the source. When neither yields a
10
+ * usable identifier we fall back to "Document" to keep output deterministic.
8
11
  *
9
12
  * @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".
13
+ * string `title` uses `fallbackName`.
14
+ * @param fallbackName - Optional name (typically the schema's base filename)
15
+ * used when the schema has no usable `title`. PascalCased the same way.
16
+ * @returns A PascalCase type name derived from the title or fallback, or
17
+ * "Document".
12
18
  *
13
19
  * @example
14
20
  * ```ts
15
21
  * deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
16
22
  * deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
23
+ * deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
17
24
  * deriveRootTypeName({ type: 'object' }) // 'Document'
18
25
  * ```
19
26
  */
20
- export declare const deriveRootTypeName: (schema: unknown) => string;
27
+ export declare const deriveRootTypeName: (schema: unknown, fallbackName?: string) => string;
@@ -12,28 +12,41 @@ const titleToPascalCase = (title) => {
12
12
  return pascal.replace(/^\d+/, '');
13
13
  };
14
14
  /**
15
- * Derives the root type name for a generated schema from its `title` keyword.
15
+ * Derives the root type name for a generated schema.
16
16
  *
17
17
  * We name the root after the schema itself instead of a generic "Document" so
18
18
  * the generated types and parsers read naturally (e.g. an OpenAPI schema yields
19
- * `OpenApi` / `parseOpenApi`). When the schema has no usable `title`, we fall
20
- * back to "Document" to keep output deterministic.
19
+ * `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
20
+ * `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
21
+ * the caller can supply the schema's filename (without extension) as a
22
+ * `fallbackName` so the name still reflects the source. When neither yields a
23
+ * usable identifier we fall back to "Document" to keep output deterministic.
21
24
  *
22
25
  * @param schema - The root JSON Schema. A boolean schema or one without a
23
- * string `title` falls back to the default.
24
- * @returns A PascalCase type name derived from the title, or "Document".
26
+ * string `title` uses `fallbackName`.
27
+ * @param fallbackName - Optional name (typically the schema's base filename)
28
+ * used when the schema has no usable `title`. PascalCased the same way.
29
+ * @returns A PascalCase type name derived from the title or fallback, or
30
+ * "Document".
25
31
  *
26
32
  * @example
27
33
  * ```ts
28
34
  * deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
29
35
  * deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
36
+ * deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
30
37
  * deriveRootTypeName({ type: 'object' }) // 'Document'
31
38
  * ```
32
39
  */
33
- export const deriveRootTypeName = (schema) => {
34
- if (!isObject(schema) || typeof schema['title'] !== 'string') {
35
- return 'Document';
40
+ export const deriveRootTypeName = (schema, fallbackName) => {
41
+ if (isObject(schema) && typeof schema['title'] === 'string') {
42
+ const fromTitle = titleToPascalCase(schema['title']);
43
+ if (fromTitle)
44
+ return fromTitle;
36
45
  }
37
- const name = titleToPascalCase(schema['title']);
38
- return name || 'Document';
46
+ if (typeof fallbackName === 'string') {
47
+ const fromFilename = titleToPascalCase(fallbackName);
48
+ if (fromFilename)
49
+ return fromFilename;
50
+ }
51
+ return 'Document';
39
52
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -14,29 +14,41 @@ const titleToPascalCase = (title: string): string => {
14
14
  }
15
15
 
16
16
  /**
17
- * Derives the root type name for a generated schema from its `title` keyword.
17
+ * Derives the root type name for a generated schema.
18
18
  *
19
19
  * We name the root after the schema itself instead of a generic "Document" so
20
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.
21
+ * `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
22
+ * `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
23
+ * the caller can supply the schema's filename (without extension) as a
24
+ * `fallbackName` so the name still reflects the source. When neither yields a
25
+ * usable identifier we fall back to "Document" to keep output deterministic.
23
26
  *
24
27
  * @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".
28
+ * string `title` uses `fallbackName`.
29
+ * @param fallbackName - Optional name (typically the schema's base filename)
30
+ * used when the schema has no usable `title`. PascalCased the same way.
31
+ * @returns A PascalCase type name derived from the title or fallback, or
32
+ * "Document".
27
33
  *
28
34
  * @example
29
35
  * ```ts
30
36
  * deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
31
37
  * deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
38
+ * deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
32
39
  * deriveRootTypeName({ type: 'object' }) // 'Document'
33
40
  * ```
34
41
  */
35
- export const deriveRootTypeName = (schema: unknown): string => {
36
- if (!isObject(schema) || typeof schema['title'] !== 'string') {
37
- return 'Document'
42
+ export const deriveRootTypeName = (schema: unknown, fallbackName?: string): string => {
43
+ if (isObject(schema) && typeof schema['title'] === 'string') {
44
+ const fromTitle = titleToPascalCase(schema['title'])
45
+ if (fromTitle) return fromTitle
38
46
  }
39
47
 
40
- const name = titleToPascalCase(schema['title'])
41
- return name || 'Document'
48
+ if (typeof fallbackName === 'string') {
49
+ const fromFilename = titleToPascalCase(fallbackName)
50
+ if (fromFilename) return fromFilename
51
+ }
52
+
53
+ return 'Document'
42
54
  }