Skip to content

Commit 13f94a0

Browse files
committed
refactor
1 parent f93a9db commit 13f94a0

File tree

11 files changed

+483
-366
lines changed

11 files changed

+483
-366
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Definition, DefinitionOrBoolean } from "typescript-json-schema";
2+
3+
export type Defs = [string, Definition][];
4+
export type Generator = (name: string, pkg: string) => string;
5+
6+
export const defs = (entry: DefinitionOrBoolean): Defs => {
7+
return typeof entry !== "boolean" ? Object.entries(entry as Definition) : [];
8+
};
9+
10+
export const constsNs = "Consts";
11+
export const enumsNs = "Enums";
12+
export const constsPath = "/Consts/";
13+
export const enumsPath = "/Enums/";
14+
15+
export const getValue = (
16+
schema: DefinitionOrBoolean,
17+
stringNotEnum?: boolean
18+
): string => {
19+
if (typeof schema === "boolean") {
20+
return schema ? "true" : "false";
21+
}
22+
if (schema.const) {
23+
return `"${schema.const}"`;
24+
}
25+
if (Object.hasOwn(schema, "hide")) {
26+
return `null`;
27+
}
28+
if (
29+
schema.$ref &&
30+
(schema.$ref.includes(constsPath) || schema.$ref.includes(enumsPath))
31+
) {
32+
const name =
33+
stringNotEnum && schema.$ref.includes(".")
34+
? schema.$ref.substring(schema.$ref.lastIndexOf(".") + 1)
35+
: schema.$ref.substring(schema.$ref.lastIndexOf("/") + 1);
36+
return `Identifiers.${name}`;
37+
}
38+
return "";
39+
};
40+
41+
export const getDefault = (schema: DefinitionOrBoolean): string => {
42+
if (typeof schema === "object" && Object.hasOwn(schema, "default")) {
43+
if (schema.type === "string") {
44+
return `"${schema.default}"`;
45+
}
46+
return `${schema.default}`;
47+
}
48+
return "";
49+
};
50+
51+
export const isConst = (schema: DefinitionOrBoolean) => {
52+
return (
53+
typeof schema === "object" &&
54+
(Object.hasOwn(schema, "const") ||
55+
Object.hasOwn(schema, "hide") ||
56+
(Object.hasOwn(schema, "$ref") &&
57+
schema.$ref &&
58+
(schema.$ref.includes(constsPath) ||
59+
(schema.$ref.includes(enumsPath) && schema.$ref.includes(".")))))
60+
);
61+
};
62+
63+
export const isEnum = (schema: DefinitionOrBoolean) => {
64+
return (
65+
typeof schema === "object" &&
66+
(Object.hasOwn(schema, "enum") ||
67+
(Object.hasOwn(schema, "$ref") &&
68+
schema.$ref &&
69+
schema.$ref.includes(enumsPath)))
70+
);
71+
};
72+
73+
export const firstLetterUpperCase = (str: string) => {
74+
return str.charAt(0).toUpperCase() + str.slice(1);
75+
};

0 commit comments

Comments
 (0)