@nestia/migrate 4.6.1-dev.20250117 → 4.6.1
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 +87 -87
- package/lib/bundles/NEST_TEMPLATE.js +66 -66
- package/lib/bundles/NEST_TEMPLATE.js.map +1 -1
- package/lib/bundles/SDK_TEMPLATE.js +30 -30
- package/lib/bundles/SDK_TEMPLATE.js.map +1 -1
- package/lib/index.mjs +92 -92
- package/lib/utils/openapi-down-convert/converter.js +2 -2
- package/package.json +6 -6
- package/src/MigrateApplication.ts +107 -107
- package/src/analyzers/MigrateApplicationAnalyzer.ts +18 -18
- package/src/analyzers/MigrateControllerAnalyzer.ts +51 -51
- package/src/archivers/MigrateFileArchiver.ts +38 -38
- package/src/bundles/NEST_TEMPLATE.ts +66 -66
- package/src/bundles/SDK_TEMPLATE.ts +30 -30
- package/src/executable/bundle.js +125 -125
- package/src/executable/migrate.ts +7 -7
- package/src/factories/TypeLiteralFactory.ts +57 -57
- package/src/index.ts +4 -4
- package/src/internal/MigrateCommander.ts +86 -86
- package/src/internal/MigrateInquirer.ts +89 -89
- package/src/module.ts +8 -8
- package/src/programmers/MigrateApiFileProgrammer.ts +49 -49
- package/src/programmers/MigrateApiFunctionProgrammer.ts +210 -210
- package/src/programmers/MigrateApiNamespaceProgrammer.ts +417 -417
- package/src/programmers/MigrateApiProgrammer.ts +103 -103
- package/src/programmers/MigrateApiSimulatationProgrammer.ts +324 -324
- package/src/programmers/MigrateApiStartProgrammer.ts +194 -194
- package/src/programmers/MigrateDtoProgrammer.ts +87 -87
- package/src/programmers/MigrateE2eFileProgrammer.ts +117 -117
- package/src/programmers/MigrateE2eProgrammer.ts +34 -34
- package/src/programmers/MigrateImportProgrammer.ts +118 -118
- package/src/programmers/MigrateNestControllerProgrammer.ts +50 -50
- package/src/programmers/MigrateNestMethodProgrammer.ts +393 -393
- package/src/programmers/MigrateNestModuleProgrammer.ts +65 -65
- package/src/programmers/MigrateNestProgrammer.ts +81 -81
- package/src/programmers/MigrateSchemaProgrammer.ts +373 -373
- package/src/structures/IHttpMigrateController.ts +8 -8
- package/src/structures/IHttpMigrateDto.ts +8 -8
- package/src/structures/IHttpMigrateFile.ts +5 -5
- package/src/structures/IHttpMigrateProgram.ts +27 -27
- package/src/structures/IHttpMigrateRoute.ts +1 -1
- package/src/structures/IHttpMigrateSchema.ts +4 -4
- package/src/utils/FilePrinter.ts +36 -36
- package/src/utils/MapUtil.ts +13 -13
- package/src/utils/OpenApiTypeChecker.ts +73 -73
- package/src/utils/SetupWizard.ts +12 -12
- package/src/utils/StringUtil.ts +113 -113
- package/src/utils/openapi-down-convert/RefVisitor.ts +139 -139
- package/src/utils/openapi-down-convert/converter.ts +527 -527
@@ -1,8 +1,8 @@
|
|
1
|
-
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
2
|
-
|
3
|
-
export interface IHttpMigrateController {
|
4
|
-
name: string;
|
5
|
-
path: string;
|
6
|
-
location: string;
|
7
|
-
routes: IHttpMigrateRoute[];
|
8
|
-
}
|
1
|
+
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
2
|
+
|
3
|
+
export interface IHttpMigrateController {
|
4
|
+
name: string;
|
5
|
+
path: string;
|
6
|
+
location: string;
|
7
|
+
routes: IHttpMigrateRoute[];
|
8
|
+
}
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import { OpenApi } from "@samchon/openapi";
|
2
|
-
|
3
|
-
export interface IHttpMigrateDto {
|
4
|
-
name: string;
|
5
|
-
location: string;
|
6
|
-
schema: OpenApi.IJsonSchema | null;
|
7
|
-
children: IHttpMigrateDto[];
|
8
|
-
}
|
1
|
+
import { OpenApi } from "@samchon/openapi";
|
2
|
+
|
3
|
+
export interface IHttpMigrateDto {
|
4
|
+
name: string;
|
5
|
+
location: string;
|
6
|
+
schema: OpenApi.IJsonSchema | null;
|
7
|
+
children: IHttpMigrateDto[];
|
8
|
+
}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
export interface IHttpMigrateFile {
|
2
|
-
location: string;
|
3
|
-
file: string;
|
4
|
-
content: string;
|
5
|
-
}
|
1
|
+
export interface IHttpMigrateFile {
|
2
|
+
location: string;
|
3
|
+
file: string;
|
4
|
+
content: string;
|
5
|
+
}
|
@@ -1,27 +1,27 @@
|
|
1
|
-
import { OpenApi } from "@samchon/openapi";
|
2
|
-
|
3
|
-
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
4
|
-
|
5
|
-
export interface IHttpMigrateProgram extends IHttpMigrateProgram.IProps {
|
6
|
-
routes: IHttpMigrateRoute[];
|
7
|
-
errors: IHttpMigrateProgram.IError[];
|
8
|
-
}
|
9
|
-
export namespace IHttpMigrateProgram {
|
10
|
-
export interface IProps {
|
11
|
-
mode: "nest" | "sdk";
|
12
|
-
simulate: boolean;
|
13
|
-
e2e: boolean;
|
14
|
-
document: OpenApi.IDocument;
|
15
|
-
}
|
16
|
-
export interface IConfig {
|
17
|
-
mode: "nest" | "sdk";
|
18
|
-
simulate: boolean;
|
19
|
-
e2e: boolean;
|
20
|
-
}
|
21
|
-
export interface IError {
|
22
|
-
method: string;
|
23
|
-
path: string;
|
24
|
-
operation: () => OpenApi.IOperation;
|
25
|
-
messages: string[];
|
26
|
-
}
|
27
|
-
}
|
1
|
+
import { OpenApi } from "@samchon/openapi";
|
2
|
+
|
3
|
+
import { IHttpMigrateRoute } from "./IHttpMigrateRoute";
|
4
|
+
|
5
|
+
export interface IHttpMigrateProgram extends IHttpMigrateProgram.IProps {
|
6
|
+
routes: IHttpMigrateRoute[];
|
7
|
+
errors: IHttpMigrateProgram.IError[];
|
8
|
+
}
|
9
|
+
export namespace IHttpMigrateProgram {
|
10
|
+
export interface IProps {
|
11
|
+
mode: "nest" | "sdk";
|
12
|
+
simulate: boolean;
|
13
|
+
e2e: boolean;
|
14
|
+
document: OpenApi.IDocument;
|
15
|
+
}
|
16
|
+
export interface IConfig {
|
17
|
+
mode: "nest" | "sdk";
|
18
|
+
simulate: boolean;
|
19
|
+
e2e: boolean;
|
20
|
+
}
|
21
|
+
export interface IError {
|
22
|
+
method: string;
|
23
|
+
path: string;
|
24
|
+
operation: () => OpenApi.IOperation;
|
25
|
+
messages: string[];
|
26
|
+
}
|
27
|
+
}
|
@@ -1 +1 @@
|
|
1
|
-
export { IHttpMigrateRoute } from "@samchon/openapi";
|
1
|
+
export { IHttpMigrateRoute } from "@samchon/openapi";
|
@@ -1,4 +1,4 @@
|
|
1
|
-
export interface IHttpMigrateSchema {
|
2
|
-
name: string;
|
3
|
-
children: IHttpMigrateSchema[];
|
4
|
-
}
|
1
|
+
export interface IHttpMigrateSchema {
|
2
|
+
name: string;
|
3
|
+
children: IHttpMigrateSchema[];
|
4
|
+
}
|
package/src/utils/FilePrinter.ts
CHANGED
@@ -1,36 +1,36 @@
|
|
1
|
-
import ts from "typescript";
|
2
|
-
|
3
|
-
export namespace FilePrinter {
|
4
|
-
export const description = <Node extends ts.Node>(
|
5
|
-
node: Node,
|
6
|
-
comment: string,
|
7
|
-
): Node => {
|
8
|
-
if (comment.length === 0) return node;
|
9
|
-
ts.addSyntheticLeadingComment(
|
10
|
-
node,
|
11
|
-
ts.SyntaxKind.MultiLineCommentTrivia,
|
12
|
-
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
13
|
-
true,
|
14
|
-
);
|
15
|
-
return node;
|
16
|
-
};
|
17
|
-
|
18
|
-
export const newLine = () =>
|
19
|
-
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
20
|
-
|
21
|
-
export const write = (props: {
|
22
|
-
statements: ts.Statement[];
|
23
|
-
top?: string;
|
24
|
-
}): string => {
|
25
|
-
const script: string = ts
|
26
|
-
.createPrinter()
|
27
|
-
.printFile(
|
28
|
-
ts.factory.createSourceFile(
|
29
|
-
props.statements,
|
30
|
-
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
|
31
|
-
ts.NodeFlags.None,
|
32
|
-
),
|
33
|
-
);
|
34
|
-
return (props.top ?? "") + script;
|
35
|
-
};
|
36
|
-
}
|
1
|
+
import ts from "typescript";
|
2
|
+
|
3
|
+
export namespace FilePrinter {
|
4
|
+
export const description = <Node extends ts.Node>(
|
5
|
+
node: Node,
|
6
|
+
comment: string,
|
7
|
+
): Node => {
|
8
|
+
if (comment.length === 0) return node;
|
9
|
+
ts.addSyntheticLeadingComment(
|
10
|
+
node,
|
11
|
+
ts.SyntaxKind.MultiLineCommentTrivia,
|
12
|
+
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
13
|
+
true,
|
14
|
+
);
|
15
|
+
return node;
|
16
|
+
};
|
17
|
+
|
18
|
+
export const newLine = () =>
|
19
|
+
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
20
|
+
|
21
|
+
export const write = (props: {
|
22
|
+
statements: ts.Statement[];
|
23
|
+
top?: string;
|
24
|
+
}): string => {
|
25
|
+
const script: string = ts
|
26
|
+
.createPrinter()
|
27
|
+
.printFile(
|
28
|
+
ts.factory.createSourceFile(
|
29
|
+
props.statements,
|
30
|
+
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
|
31
|
+
ts.NodeFlags.None,
|
32
|
+
),
|
33
|
+
);
|
34
|
+
return (props.top ?? "") + script;
|
35
|
+
};
|
36
|
+
}
|
package/src/utils/MapUtil.ts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
export namespace MapUtil {
|
2
|
-
export const take =
|
3
|
-
<Key, T>(dict: Map<Key, T>) =>
|
4
|
-
(key: Key) =>
|
5
|
-
(generator: () => T): T => {
|
6
|
-
const oldbie: T | undefined = dict.get(key);
|
7
|
-
if (oldbie) return oldbie;
|
8
|
-
|
9
|
-
const value: T = generator();
|
10
|
-
dict.set(key, value);
|
11
|
-
return value;
|
12
|
-
};
|
13
|
-
}
|
1
|
+
export namespace MapUtil {
|
2
|
+
export const take =
|
3
|
+
<Key, T>(dict: Map<Key, T>) =>
|
4
|
+
(key: Key) =>
|
5
|
+
(generator: () => T): T => {
|
6
|
+
const oldbie: T | undefined = dict.get(key);
|
7
|
+
if (oldbie) return oldbie;
|
8
|
+
|
9
|
+
const value: T = generator();
|
10
|
+
dict.set(key, value);
|
11
|
+
return value;
|
12
|
+
};
|
13
|
+
}
|
@@ -1,73 +1,73 @@
|
|
1
|
-
import { OpenApi } from "@samchon/openapi";
|
2
|
-
|
3
|
-
export namespace OpenApiTypeChecker {
|
4
|
-
export const isOneOf = (
|
5
|
-
schema: OpenApi.IJsonSchema,
|
6
|
-
): schema is OpenApi.IJsonSchema.IOneOf =>
|
7
|
-
(schema as any).oneOf !== undefined;
|
8
|
-
|
9
|
-
export const isNull = (
|
10
|
-
schema: OpenApi.IJsonSchema,
|
11
|
-
): schema is OpenApi.IJsonSchema.INull =>
|
12
|
-
(schema as OpenApi.IJsonSchema.INull).type === "null";
|
13
|
-
|
14
|
-
export const isConstant = (
|
15
|
-
schema: OpenApi.IJsonSchema,
|
16
|
-
): schema is OpenApi.IJsonSchema.IConstant =>
|
17
|
-
(schema as OpenApi.IJsonSchema.IConstant).const !== undefined;
|
18
|
-
|
19
|
-
export const isBoolean = (
|
20
|
-
schema: OpenApi.IJsonSchema,
|
21
|
-
): schema is OpenApi.IJsonSchema.IBoolean =>
|
22
|
-
(schema as OpenApi.IJsonSchema.IBoolean).type === "boolean";
|
23
|
-
|
24
|
-
export const isInteger = (
|
25
|
-
schema: OpenApi.IJsonSchema,
|
26
|
-
): schema is OpenApi.IJsonSchema.IInteger =>
|
27
|
-
(schema as OpenApi.IJsonSchema.IInteger).type === "integer";
|
28
|
-
|
29
|
-
export const isNumber = (
|
30
|
-
schema: OpenApi.IJsonSchema,
|
31
|
-
): schema is OpenApi.IJsonSchema.INumber =>
|
32
|
-
(schema as OpenApi.IJsonSchema.INumber).type === "number";
|
33
|
-
|
34
|
-
export const isString = (
|
35
|
-
schema: OpenApi.IJsonSchema,
|
36
|
-
): schema is OpenApi.IJsonSchema.IString =>
|
37
|
-
(schema as OpenApi.IJsonSchema.IString).type === "string";
|
38
|
-
|
39
|
-
export const isArray = (
|
40
|
-
schema: OpenApi.IJsonSchema,
|
41
|
-
): schema is OpenApi.IJsonSchema.IArray =>
|
42
|
-
(schema as OpenApi.IJsonSchema.IArray).type === "array" &&
|
43
|
-
(schema as OpenApi.IJsonSchema.IArray).items !== undefined;
|
44
|
-
|
45
|
-
export const isTuple = (
|
46
|
-
schema: OpenApi.IJsonSchema,
|
47
|
-
): schema is OpenApi.IJsonSchema.ITuple =>
|
48
|
-
(schema as OpenApi.IJsonSchema.ITuple).type === "array" &&
|
49
|
-
(schema as OpenApi.IJsonSchema.ITuple).prefixItems !== undefined;
|
50
|
-
|
51
|
-
export const isObject = (
|
52
|
-
schema: OpenApi.IJsonSchema,
|
53
|
-
): schema is OpenApi.IJsonSchema.IObject =>
|
54
|
-
(schema as OpenApi.IJsonSchema.IObject).type === "object";
|
55
|
-
|
56
|
-
export const isReference = (
|
57
|
-
schema: OpenApi.IJsonSchema,
|
58
|
-
): schema is OpenApi.IJsonSchema.IReference =>
|
59
|
-
(schema as OpenApi.IJsonSchema.IReference).$ref !== undefined;
|
60
|
-
|
61
|
-
export const isUnknown = (
|
62
|
-
schema: OpenApi.IJsonSchema,
|
63
|
-
): schema is OpenApi.IJsonSchema.IUnknown =>
|
64
|
-
(schema as OpenApi.IJsonSchema.__ISignificant<any>).type === undefined &&
|
65
|
-
!isOneOf(schema) &&
|
66
|
-
!isReference(schema) &&
|
67
|
-
!isConstant(schema);
|
68
|
-
|
69
|
-
// export const isNullable = (schema: OpenApi.IJsonSchema): boolean =>
|
70
|
-
// SwaggerTypeChecker.isOneOf(schema)
|
71
|
-
// ? schema.oneOf.some(isNullable)
|
72
|
-
// : (schema as OpenApi.IJsonSchema.INullOnly).type === "null";
|
73
|
-
}
|
1
|
+
import { OpenApi } from "@samchon/openapi";
|
2
|
+
|
3
|
+
export namespace OpenApiTypeChecker {
|
4
|
+
export const isOneOf = (
|
5
|
+
schema: OpenApi.IJsonSchema,
|
6
|
+
): schema is OpenApi.IJsonSchema.IOneOf =>
|
7
|
+
(schema as any).oneOf !== undefined;
|
8
|
+
|
9
|
+
export const isNull = (
|
10
|
+
schema: OpenApi.IJsonSchema,
|
11
|
+
): schema is OpenApi.IJsonSchema.INull =>
|
12
|
+
(schema as OpenApi.IJsonSchema.INull).type === "null";
|
13
|
+
|
14
|
+
export const isConstant = (
|
15
|
+
schema: OpenApi.IJsonSchema,
|
16
|
+
): schema is OpenApi.IJsonSchema.IConstant =>
|
17
|
+
(schema as OpenApi.IJsonSchema.IConstant).const !== undefined;
|
18
|
+
|
19
|
+
export const isBoolean = (
|
20
|
+
schema: OpenApi.IJsonSchema,
|
21
|
+
): schema is OpenApi.IJsonSchema.IBoolean =>
|
22
|
+
(schema as OpenApi.IJsonSchema.IBoolean).type === "boolean";
|
23
|
+
|
24
|
+
export const isInteger = (
|
25
|
+
schema: OpenApi.IJsonSchema,
|
26
|
+
): schema is OpenApi.IJsonSchema.IInteger =>
|
27
|
+
(schema as OpenApi.IJsonSchema.IInteger).type === "integer";
|
28
|
+
|
29
|
+
export const isNumber = (
|
30
|
+
schema: OpenApi.IJsonSchema,
|
31
|
+
): schema is OpenApi.IJsonSchema.INumber =>
|
32
|
+
(schema as OpenApi.IJsonSchema.INumber).type === "number";
|
33
|
+
|
34
|
+
export const isString = (
|
35
|
+
schema: OpenApi.IJsonSchema,
|
36
|
+
): schema is OpenApi.IJsonSchema.IString =>
|
37
|
+
(schema as OpenApi.IJsonSchema.IString).type === "string";
|
38
|
+
|
39
|
+
export const isArray = (
|
40
|
+
schema: OpenApi.IJsonSchema,
|
41
|
+
): schema is OpenApi.IJsonSchema.IArray =>
|
42
|
+
(schema as OpenApi.IJsonSchema.IArray).type === "array" &&
|
43
|
+
(schema as OpenApi.IJsonSchema.IArray).items !== undefined;
|
44
|
+
|
45
|
+
export const isTuple = (
|
46
|
+
schema: OpenApi.IJsonSchema,
|
47
|
+
): schema is OpenApi.IJsonSchema.ITuple =>
|
48
|
+
(schema as OpenApi.IJsonSchema.ITuple).type === "array" &&
|
49
|
+
(schema as OpenApi.IJsonSchema.ITuple).prefixItems !== undefined;
|
50
|
+
|
51
|
+
export const isObject = (
|
52
|
+
schema: OpenApi.IJsonSchema,
|
53
|
+
): schema is OpenApi.IJsonSchema.IObject =>
|
54
|
+
(schema as OpenApi.IJsonSchema.IObject).type === "object";
|
55
|
+
|
56
|
+
export const isReference = (
|
57
|
+
schema: OpenApi.IJsonSchema,
|
58
|
+
): schema is OpenApi.IJsonSchema.IReference =>
|
59
|
+
(schema as OpenApi.IJsonSchema.IReference).$ref !== undefined;
|
60
|
+
|
61
|
+
export const isUnknown = (
|
62
|
+
schema: OpenApi.IJsonSchema,
|
63
|
+
): schema is OpenApi.IJsonSchema.IUnknown =>
|
64
|
+
(schema as OpenApi.IJsonSchema.__ISignificant<any>).type === undefined &&
|
65
|
+
!isOneOf(schema) &&
|
66
|
+
!isReference(schema) &&
|
67
|
+
!isConstant(schema);
|
68
|
+
|
69
|
+
// export const isNullable = (schema: OpenApi.IJsonSchema): boolean =>
|
70
|
+
// SwaggerTypeChecker.isOneOf(schema)
|
71
|
+
// ? schema.oneOf.some(isNullable)
|
72
|
+
// : (schema as OpenApi.IJsonSchema.INullOnly).type === "null";
|
73
|
+
}
|
package/src/utils/SetupWizard.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import cp from "child_process";
|
2
|
-
|
3
|
-
export namespace SetupWizard {
|
4
|
-
export const setup = (output: string) => {
|
5
|
-
execute(output)("npm install");
|
6
|
-
};
|
7
|
-
|
8
|
-
const execute = (cwd: string) => (command: string, fake?: string) => {
|
9
|
-
console.log(fake ?? command);
|
10
|
-
cp.execSync(command, { cwd, stdio: "inherit" });
|
11
|
-
};
|
12
|
-
}
|
1
|
+
import cp from "child_process";
|
2
|
+
|
3
|
+
export namespace SetupWizard {
|
4
|
+
export const setup = (output: string) => {
|
5
|
+
execute(output)("npm install");
|
6
|
+
};
|
7
|
+
|
8
|
+
const execute = (cwd: string) => (command: string, fake?: string) => {
|
9
|
+
console.log(fake ?? command);
|
10
|
+
cp.execSync(command, { cwd, stdio: "inherit" });
|
11
|
+
};
|
12
|
+
}
|
package/src/utils/StringUtil.ts
CHANGED
@@ -1,113 +1,113 @@
|
|
1
|
-
export namespace StringUtil {
|
2
|
-
export const capitalize = (str: string) =>
|
3
|
-
str[0].toUpperCase() + str.slice(1).toLowerCase();
|
4
|
-
|
5
|
-
export const splitWithNormalization = (path: string) =>
|
6
|
-
path
|
7
|
-
.split("/")
|
8
|
-
.map((str) => normalize(str.trim()))
|
9
|
-
.filter((str) => !!str.length);
|
10
|
-
|
11
|
-
export const escapeDuplicate =
|
12
|
-
(keep: string[]) =>
|
13
|
-
(change: string): string =>
|
14
|
-
keep.includes(change) ? escapeDuplicate(keep)(`_${change}`) : change;
|
15
|
-
|
16
|
-
export const escapeNonVariable = (str: string): string => {
|
17
|
-
str = escape(str);
|
18
|
-
for (const [before, after] of VARIABLE_REPLACERS)
|
19
|
-
str = str.split(before).join(after);
|
20
|
-
for (let i: number = 0; i <= 9; ++i)
|
21
|
-
if (str[0] === i.toString()) {
|
22
|
-
str = "_" + str;
|
23
|
-
break;
|
24
|
-
}
|
25
|
-
if (str === "") return "_empty_";
|
26
|
-
return str;
|
27
|
-
};
|
28
|
-
}
|
29
|
-
|
30
|
-
const escape = (str: string): string => {
|
31
|
-
str = str.trim();
|
32
|
-
if (RESERVED.has(str)) return `_${str}`;
|
33
|
-
else if (str.length !== 0 && "0" <= str[0] && str[0] <= "9") str = `_${str}`;
|
34
|
-
return str;
|
35
|
-
};
|
36
|
-
|
37
|
-
const normalize = (str: string): string =>
|
38
|
-
escape(str.split(".").join("_").split("-").join("_"));
|
39
|
-
|
40
|
-
const RESERVED: Set<string> = new Set([
|
41
|
-
"break",
|
42
|
-
"case",
|
43
|
-
"catch",
|
44
|
-
"class",
|
45
|
-
"const",
|
46
|
-
"continue",
|
47
|
-
"debugger",
|
48
|
-
"default",
|
49
|
-
"delete",
|
50
|
-
"do",
|
51
|
-
"else",
|
52
|
-
"enum",
|
53
|
-
"export",
|
54
|
-
"extends",
|
55
|
-
"false",
|
56
|
-
"finally",
|
57
|
-
"for",
|
58
|
-
"function",
|
59
|
-
"if",
|
60
|
-
"import",
|
61
|
-
"in",
|
62
|
-
"instanceof",
|
63
|
-
"module",
|
64
|
-
"new",
|
65
|
-
"null",
|
66
|
-
"package",
|
67
|
-
"public",
|
68
|
-
"private",
|
69
|
-
"protected",
|
70
|
-
"return",
|
71
|
-
"super",
|
72
|
-
"switch",
|
73
|
-
"this",
|
74
|
-
"throw",
|
75
|
-
"true",
|
76
|
-
"try",
|
77
|
-
"typeof",
|
78
|
-
"var",
|
79
|
-
"void",
|
80
|
-
"while",
|
81
|
-
"with",
|
82
|
-
]);
|
83
|
-
|
84
|
-
const VARIABLE_REPLACERS: [string, string][] = [
|
85
|
-
["`", "_backquote_"],
|
86
|
-
["!", "_exclamation_"],
|
87
|
-
["@", "_at_"],
|
88
|
-
["#", "_hash_"],
|
89
|
-
["$", "_dollar_"],
|
90
|
-
["%", "_percent_"],
|
91
|
-
["^", "_caret_"],
|
92
|
-
["&", "_and_"],
|
93
|
-
["*", "_star_"],
|
94
|
-
["(", "_lparen_"],
|
95
|
-
[")", "_rparen_"],
|
96
|
-
["-", "_"],
|
97
|
-
["+", "_plus_"],
|
98
|
-
["|", "_or_"],
|
99
|
-
["{", "_blt_"],
|
100
|
-
["}", "_bgt_"],
|
101
|
-
["<", "_lt_"],
|
102
|
-
[">", "_gt_"],
|
103
|
-
["[", "_alt_"],
|
104
|
-
["]", "_agt_"],
|
105
|
-
[",", "_comma_"],
|
106
|
-
["'", "_singlequote_"],
|
107
|
-
['"', "_doublequote_"],
|
108
|
-
[" ", "_space_"],
|
109
|
-
["?", "_question_"],
|
110
|
-
[":", "_colon_"],
|
111
|
-
[";", "_semicolon_"],
|
112
|
-
["...", "_rest_"],
|
113
|
-
];
|
1
|
+
export namespace StringUtil {
|
2
|
+
export const capitalize = (str: string) =>
|
3
|
+
str[0].toUpperCase() + str.slice(1).toLowerCase();
|
4
|
+
|
5
|
+
export const splitWithNormalization = (path: string) =>
|
6
|
+
path
|
7
|
+
.split("/")
|
8
|
+
.map((str) => normalize(str.trim()))
|
9
|
+
.filter((str) => !!str.length);
|
10
|
+
|
11
|
+
export const escapeDuplicate =
|
12
|
+
(keep: string[]) =>
|
13
|
+
(change: string): string =>
|
14
|
+
keep.includes(change) ? escapeDuplicate(keep)(`_${change}`) : change;
|
15
|
+
|
16
|
+
export const escapeNonVariable = (str: string): string => {
|
17
|
+
str = escape(str);
|
18
|
+
for (const [before, after] of VARIABLE_REPLACERS)
|
19
|
+
str = str.split(before).join(after);
|
20
|
+
for (let i: number = 0; i <= 9; ++i)
|
21
|
+
if (str[0] === i.toString()) {
|
22
|
+
str = "_" + str;
|
23
|
+
break;
|
24
|
+
}
|
25
|
+
if (str === "") return "_empty_";
|
26
|
+
return str;
|
27
|
+
};
|
28
|
+
}
|
29
|
+
|
30
|
+
const escape = (str: string): string => {
|
31
|
+
str = str.trim();
|
32
|
+
if (RESERVED.has(str)) return `_${str}`;
|
33
|
+
else if (str.length !== 0 && "0" <= str[0] && str[0] <= "9") str = `_${str}`;
|
34
|
+
return str;
|
35
|
+
};
|
36
|
+
|
37
|
+
const normalize = (str: string): string =>
|
38
|
+
escape(str.split(".").join("_").split("-").join("_"));
|
39
|
+
|
40
|
+
const RESERVED: Set<string> = new Set([
|
41
|
+
"break",
|
42
|
+
"case",
|
43
|
+
"catch",
|
44
|
+
"class",
|
45
|
+
"const",
|
46
|
+
"continue",
|
47
|
+
"debugger",
|
48
|
+
"default",
|
49
|
+
"delete",
|
50
|
+
"do",
|
51
|
+
"else",
|
52
|
+
"enum",
|
53
|
+
"export",
|
54
|
+
"extends",
|
55
|
+
"false",
|
56
|
+
"finally",
|
57
|
+
"for",
|
58
|
+
"function",
|
59
|
+
"if",
|
60
|
+
"import",
|
61
|
+
"in",
|
62
|
+
"instanceof",
|
63
|
+
"module",
|
64
|
+
"new",
|
65
|
+
"null",
|
66
|
+
"package",
|
67
|
+
"public",
|
68
|
+
"private",
|
69
|
+
"protected",
|
70
|
+
"return",
|
71
|
+
"super",
|
72
|
+
"switch",
|
73
|
+
"this",
|
74
|
+
"throw",
|
75
|
+
"true",
|
76
|
+
"try",
|
77
|
+
"typeof",
|
78
|
+
"var",
|
79
|
+
"void",
|
80
|
+
"while",
|
81
|
+
"with",
|
82
|
+
]);
|
83
|
+
|
84
|
+
const VARIABLE_REPLACERS: [string, string][] = [
|
85
|
+
["`", "_backquote_"],
|
86
|
+
["!", "_exclamation_"],
|
87
|
+
["@", "_at_"],
|
88
|
+
["#", "_hash_"],
|
89
|
+
["$", "_dollar_"],
|
90
|
+
["%", "_percent_"],
|
91
|
+
["^", "_caret_"],
|
92
|
+
["&", "_and_"],
|
93
|
+
["*", "_star_"],
|
94
|
+
["(", "_lparen_"],
|
95
|
+
[")", "_rparen_"],
|
96
|
+
["-", "_"],
|
97
|
+
["+", "_plus_"],
|
98
|
+
["|", "_or_"],
|
99
|
+
["{", "_blt_"],
|
100
|
+
["}", "_bgt_"],
|
101
|
+
["<", "_lt_"],
|
102
|
+
[">", "_gt_"],
|
103
|
+
["[", "_alt_"],
|
104
|
+
["]", "_agt_"],
|
105
|
+
[",", "_comma_"],
|
106
|
+
["'", "_singlequote_"],
|
107
|
+
['"', "_doublequote_"],
|
108
|
+
[" ", "_space_"],
|
109
|
+
["?", "_question_"],
|
110
|
+
[":", "_colon_"],
|
111
|
+
[";", "_semicolon_"],
|
112
|
+
["...", "_rest_"],
|
113
|
+
];
|