typia 6.5.1 → 6.5.3-dev.20240720
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/lib/CamelCase.d.ts +8 -14
- package/lib/PascalCase.d.ts +7 -14
- package/lib/Primitive.d.ts +6 -13
- package/lib/Resolved.d.ts +5 -12
- package/lib/SnakeCase.d.ts +4 -7
- package/lib/factories/internal/metadata/iterate_metadata_native.js +11 -0
- package/lib/factories/internal/metadata/iterate_metadata_native.js.map +1 -1
- package/lib/functional/$clone.js +3 -3
- package/lib/functional/$clone.js.map +1 -1
- package/lib/functional/_clone.mjs +3 -3
- package/lib/functional/_clone.mjs.map +1 -1
- package/lib/programmers/RandomProgrammer.js +5 -0
- package/lib/programmers/RandomProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js +2 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
- package/lib/typings/Equal.d.ts +15 -0
- package/lib/typings/Equal.js +3 -0
- package/lib/typings/Equal.js.map +1 -0
- package/lib/typings/IsTuple.d.ts +5 -0
- package/lib/typings/IsTuple.js +3 -0
- package/lib/typings/IsTuple.js.map +1 -0
- package/lib/typings/NativeClass.d.ts +1 -0
- package/lib/typings/NativeClass.js +3 -0
- package/lib/typings/NativeClass.js.map +1 -0
- package/lib/typings/ValueOf.d.ts +6 -0
- package/lib/typings/ValueOf.js +3 -0
- package/lib/typings/ValueOf.js.map +1 -0
- package/lib/utils/NamingConvention/NamingConvention.d.ts +2 -2
- package/lib/utils/NamingConvention/NamingConvention.js +53 -72
- package/lib/utils/NamingConvention/NamingConvention.js.map +1 -1
- package/lib/utils/NamingConvention/NamingConvention.mjs +42 -56
- package/lib/utils/NamingConvention/NamingConvention.mjs.map +1 -1
- package/lib/utils/StringUtil/StringUtil.js +1 -1
- package/lib/utils/StringUtil/StringUtil.js.map +1 -1
- package/lib/utils/StringUtil/StringUtil.mjs +1 -1
- package/lib/utils/StringUtil/StringUtil.mjs.map +1 -1
- package/package.json +2 -2
- package/src/CamelCase.ts +20 -66
- package/src/PascalCase.ts +16 -66
- package/src/Primitive.ts +12 -56
- package/src/Resolved.ts +6 -51
- package/src/SnakeCase.ts +5 -40
- package/src/factories/internal/metadata/iterate_metadata_native.ts +11 -0
- package/src/functional/$clone.ts +3 -3
- package/src/programmers/RandomProgrammer.ts +8 -0
- package/src/programmers/misc/MiscCloneProgrammer.ts +2 -1
- package/src/typings/Equal.ts +18 -0
- package/src/typings/IsTuple.ts +9 -0
- package/src/typings/NativeClass.ts +23 -0
- package/src/typings/ValueOf.ts +20 -0
- package/src/utils/NamingConvention/NamingConvention.ts +46 -43
- package/src/utils/StringUtil/StringUtil.ts +1 -1
package/src/Primitive.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import { Equal } from "./typings/Equal";
|
|
2
|
+
import { IsTuple } from "./typings/IsTuple";
|
|
3
|
+
import { NativeClass } from "./typings/NativeClass";
|
|
4
|
+
import { ValueOf } from "./typings/ValueOf";
|
|
5
|
+
|
|
6
|
+
import { Format } from "./tags";
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Primitive type of JSON.
|
|
3
10
|
*
|
|
@@ -35,8 +42,6 @@
|
|
|
35
42
|
export type Primitive<T> =
|
|
36
43
|
Equal<T, PrimitiveMain<T>> extends true ? T : PrimitiveMain<T>;
|
|
37
44
|
|
|
38
|
-
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
39
|
-
|
|
40
45
|
type PrimitiveMain<Instance> = Instance extends [never]
|
|
41
46
|
? never // (special trick for jsonable | null) type
|
|
42
47
|
: ValueOf<Instance> extends bigint
|
|
@@ -47,15 +52,17 @@ type PrimitiveMain<Instance> = Instance extends [never]
|
|
|
47
52
|
? never
|
|
48
53
|
: ValueOf<Instance> extends object
|
|
49
54
|
? Instance extends object
|
|
50
|
-
? Instance extends
|
|
51
|
-
?
|
|
55
|
+
? Instance extends Date
|
|
56
|
+
? string & Format<"date-time">
|
|
52
57
|
: Instance extends IJsonable<infer Raw>
|
|
53
58
|
? ValueOf<Raw> extends object
|
|
54
59
|
? Raw extends object
|
|
55
60
|
? PrimitiveObject<Raw> // object would be primitified
|
|
56
61
|
: never // cannot be
|
|
57
62
|
: ValueOf<Raw> // atomic value
|
|
58
|
-
:
|
|
63
|
+
: Instance extends Exclude<NativeClass, Date>
|
|
64
|
+
? never
|
|
65
|
+
: PrimitiveObject<Instance> // object would be primitified
|
|
59
66
|
: never // cannot be
|
|
60
67
|
: ValueOf<Instance>;
|
|
61
68
|
|
|
@@ -80,57 +87,6 @@ type PrimitiveTuple<T extends readonly any[]> = T extends []
|
|
|
80
87
|
? [PrimitiveMain<F>?, ...PrimitiveTuple<Rest>]
|
|
81
88
|
: [];
|
|
82
89
|
|
|
83
|
-
type ValueOf<Instance> =
|
|
84
|
-
IsValueOf<Instance, Boolean> extends true
|
|
85
|
-
? boolean
|
|
86
|
-
: IsValueOf<Instance, Number> extends true
|
|
87
|
-
? number
|
|
88
|
-
: IsValueOf<Instance, String> extends true
|
|
89
|
-
? string
|
|
90
|
-
: Instance;
|
|
91
|
-
|
|
92
|
-
type NativeClass =
|
|
93
|
-
| Set<any>
|
|
94
|
-
| Map<any, any>
|
|
95
|
-
| WeakSet<any>
|
|
96
|
-
| WeakMap<any, any>
|
|
97
|
-
| Uint8Array
|
|
98
|
-
| Uint8ClampedArray
|
|
99
|
-
| Uint16Array
|
|
100
|
-
| Uint32Array
|
|
101
|
-
| BigUint64Array
|
|
102
|
-
| Int8Array
|
|
103
|
-
| Int16Array
|
|
104
|
-
| Int32Array
|
|
105
|
-
| BigInt64Array
|
|
106
|
-
| Float32Array
|
|
107
|
-
| Float64Array
|
|
108
|
-
| ArrayBuffer
|
|
109
|
-
| SharedArrayBuffer
|
|
110
|
-
| DataView;
|
|
111
|
-
|
|
112
|
-
type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
|
|
113
|
-
never,
|
|
114
|
-
]
|
|
115
|
-
? false
|
|
116
|
-
: T extends readonly any[]
|
|
117
|
-
? number extends T["length"]
|
|
118
|
-
? false
|
|
119
|
-
: true
|
|
120
|
-
: false;
|
|
121
|
-
|
|
122
|
-
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
123
|
-
? Object extends IValueOf<infer U>
|
|
124
|
-
? Instance extends U
|
|
125
|
-
? false
|
|
126
|
-
: true // not Primitive, but Object
|
|
127
|
-
: false // cannot be
|
|
128
|
-
: false;
|
|
129
|
-
|
|
130
|
-
interface IValueOf<T> {
|
|
131
|
-
valueOf(): T;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
90
|
interface IJsonable<T> {
|
|
135
91
|
toJSON(): T;
|
|
136
92
|
}
|
package/src/Resolved.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { Equal } from "./typings/Equal";
|
|
2
|
+
import { IsTuple } from "./typings/IsTuple";
|
|
3
|
+
import { NativeClass } from "./typings/NativeClass";
|
|
4
|
+
import { ValueOf } from "./typings/ValueOf";
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* Resolved type erased every methods.
|
|
3
8
|
*
|
|
@@ -29,8 +34,6 @@
|
|
|
29
34
|
export type Resolved<T> =
|
|
30
35
|
Equal<T, ResolvedMain<T>> extends true ? T : ResolvedMain<T>;
|
|
31
36
|
|
|
32
|
-
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
33
|
-
|
|
34
37
|
type ResolvedMain<T> = T extends [never]
|
|
35
38
|
? never // (special trick for jsonable | null) type
|
|
36
39
|
: ValueOf<T> extends boolean | number | bigint | string
|
|
@@ -52,24 +55,7 @@ type ResolvedObject<T extends object> =
|
|
|
52
55
|
? Map<ResolvedMain<K>, ResolvedMain<V>>
|
|
53
56
|
: T extends WeakSet<any> | WeakMap<any, any>
|
|
54
57
|
? never
|
|
55
|
-
: T extends
|
|
56
|
-
| Date
|
|
57
|
-
| Uint8Array
|
|
58
|
-
| Uint8ClampedArray
|
|
59
|
-
| Uint16Array
|
|
60
|
-
| Uint32Array
|
|
61
|
-
| BigUint64Array
|
|
62
|
-
| Int8Array
|
|
63
|
-
| Int16Array
|
|
64
|
-
| Int32Array
|
|
65
|
-
| BigInt64Array
|
|
66
|
-
| Float32Array
|
|
67
|
-
| Float64Array
|
|
68
|
-
| ArrayBuffer
|
|
69
|
-
| SharedArrayBuffer
|
|
70
|
-
| DataView
|
|
71
|
-
| Blob
|
|
72
|
-
| File
|
|
58
|
+
: T extends NativeClass
|
|
73
59
|
? T
|
|
74
60
|
: {
|
|
75
61
|
[P in keyof T]: ResolvedMain<T[P]>;
|
|
@@ -86,34 +72,3 @@ type ResolvedTuple<T extends readonly any[]> = T extends []
|
|
|
86
72
|
: T extends [(infer F)?, ...infer Rest extends readonly any[]]
|
|
87
73
|
? [ResolvedMain<F>?, ...ResolvedTuple<Rest>]
|
|
88
74
|
: [];
|
|
89
|
-
|
|
90
|
-
type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
|
|
91
|
-
never,
|
|
92
|
-
]
|
|
93
|
-
? false
|
|
94
|
-
: T extends readonly any[]
|
|
95
|
-
? number extends T["length"]
|
|
96
|
-
? false
|
|
97
|
-
: true
|
|
98
|
-
: false;
|
|
99
|
-
|
|
100
|
-
type ValueOf<Instance> =
|
|
101
|
-
IsValueOf<Instance, Boolean> extends true
|
|
102
|
-
? boolean
|
|
103
|
-
: IsValueOf<Instance, Number> extends true
|
|
104
|
-
? number
|
|
105
|
-
: IsValueOf<Instance, String> extends true
|
|
106
|
-
? string
|
|
107
|
-
: Instance;
|
|
108
|
-
|
|
109
|
-
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
110
|
-
? Object extends IValueOf<infer Primitive>
|
|
111
|
-
? Instance extends Primitive
|
|
112
|
-
? false
|
|
113
|
-
: true // not Primitive, but Object
|
|
114
|
-
: false // cannot be
|
|
115
|
-
: false;
|
|
116
|
-
|
|
117
|
-
interface IValueOf<T> {
|
|
118
|
-
valueOf(): T;
|
|
119
|
-
}
|
package/src/SnakeCase.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Equal } from "./typings/Equal";
|
|
2
|
+
import { NativeClass } from "./typings/NativeClass";
|
|
3
|
+
import { ValueOf } from "./typings/ValueOf";
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Snake case type.
|
|
3
7
|
*
|
|
@@ -14,7 +18,6 @@ export type SnakeCase<T> =
|
|
|
14
18
|
/* -----------------------------------------------------------
|
|
15
19
|
OBJECT CONVERSION
|
|
16
20
|
----------------------------------------------------------- */
|
|
17
|
-
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
18
21
|
|
|
19
22
|
type SnakageMain<T> = T extends [never]
|
|
20
23
|
? never // special trick for (jsonable | null) type
|
|
@@ -37,24 +40,7 @@ type SnakageObject<T extends object> =
|
|
|
37
40
|
? Map<SnakageMain<K>, SnakageMain<V>>
|
|
38
41
|
: T extends WeakSet<any> | WeakMap<any, any>
|
|
39
42
|
? never
|
|
40
|
-
: T extends
|
|
41
|
-
| Date
|
|
42
|
-
| Uint8Array
|
|
43
|
-
| Uint8ClampedArray
|
|
44
|
-
| Uint16Array
|
|
45
|
-
| Uint32Array
|
|
46
|
-
| BigUint64Array
|
|
47
|
-
| Int8Array
|
|
48
|
-
| Int16Array
|
|
49
|
-
| Int32Array
|
|
50
|
-
| BigInt64Array
|
|
51
|
-
| Float32Array
|
|
52
|
-
| Float64Array
|
|
53
|
-
| ArrayBuffer
|
|
54
|
-
| SharedArrayBuffer
|
|
55
|
-
| DataView
|
|
56
|
-
| Blob
|
|
57
|
-
| File
|
|
43
|
+
: T extends NativeClass
|
|
58
44
|
? T
|
|
59
45
|
: {
|
|
60
46
|
[Key in keyof T as SnakageString<Key & string>]: SnakageMain<
|
|
@@ -86,27 +72,6 @@ type SnakageTuple<T extends readonly any[]> = T extends []
|
|
|
86
72
|
? [SnakageMain<F>?, ...SnakageTuple<Rest>]
|
|
87
73
|
: [];
|
|
88
74
|
|
|
89
|
-
type ValueOf<Instance> =
|
|
90
|
-
IsValueOf<Instance, Boolean> extends true
|
|
91
|
-
? boolean
|
|
92
|
-
: IsValueOf<Instance, Number> extends true
|
|
93
|
-
? number
|
|
94
|
-
: IsValueOf<Instance, String> extends true
|
|
95
|
-
? string
|
|
96
|
-
: Instance;
|
|
97
|
-
|
|
98
|
-
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
99
|
-
? Object extends IValueOf<infer Primitive>
|
|
100
|
-
? Instance extends Primitive
|
|
101
|
-
? false
|
|
102
|
-
: true // not Primitive, but Object
|
|
103
|
-
: false // cannot be
|
|
104
|
-
: false;
|
|
105
|
-
|
|
106
|
-
interface IValueOf<T> {
|
|
107
|
-
valueOf(): T;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
75
|
/* -----------------------------------------------------------
|
|
111
76
|
STRING CONVERTER
|
|
112
77
|
----------------------------------------------------------- */
|
|
@@ -182,6 +182,17 @@ const SIMPLES: Map<string, IClassInfo> = new Map([
|
|
|
182
182
|
})),
|
|
183
183
|
},
|
|
184
184
|
],
|
|
185
|
+
[
|
|
186
|
+
"RegExp",
|
|
187
|
+
{
|
|
188
|
+
methods: [
|
|
189
|
+
{
|
|
190
|
+
name: "test",
|
|
191
|
+
return: "boolean",
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
],
|
|
185
196
|
]);
|
|
186
197
|
const GENERICS: Array<IClassInfo & { name: string }> = [
|
|
187
198
|
"WeakMap",
|
package/src/functional/$clone.ts
CHANGED
|
@@ -25,10 +25,10 @@ const $cloneMain = (value: any): any => {
|
|
|
25
25
|
else if (value instanceof SharedArrayBuffer) return value.slice(0);
|
|
26
26
|
else if (value instanceof DataView)
|
|
27
27
|
return new DataView(value.buffer.slice(0));
|
|
28
|
-
else if (value instanceof
|
|
29
|
-
return new Blob([value], { type: value.type });
|
|
30
|
-
else if (value instanceof File)
|
|
28
|
+
else if (typeof File !== "undefined" && value instanceof File)
|
|
31
29
|
return new File([value], value.name, { type: value.type });
|
|
30
|
+
else if (typeof Blob !== "undefined" && value instanceof Blob)
|
|
31
|
+
return new Blob([value], { type: value.type });
|
|
32
32
|
else if (value instanceof Set) return new Set([...value].map($cloneMain));
|
|
33
33
|
else if (value instanceof Map)
|
|
34
34
|
return new Map(
|
|
@@ -650,6 +650,7 @@ export namespace RandomProgrammer {
|
|
|
650
650
|
else if (type === "DataView") return decode_native_data_view(importer);
|
|
651
651
|
else if (type === "Blob") return decode_native_blob(importer);
|
|
652
652
|
else if (type === "File") return decode_native_file(importer);
|
|
653
|
+
else if (type === "RegExp") return decode_regexp();
|
|
653
654
|
else
|
|
654
655
|
return ts.factory.createNewExpression(
|
|
655
656
|
ts.factory.createIdentifier(type),
|
|
@@ -866,6 +867,13 @@ export namespace RandomProgrammer {
|
|
|
866
867
|
)("buffer"),
|
|
867
868
|
],
|
|
868
869
|
);
|
|
870
|
+
|
|
871
|
+
const decode_regexp = () =>
|
|
872
|
+
ts.factory.createNewExpression(
|
|
873
|
+
ts.factory.createIdentifier("RegExp"),
|
|
874
|
+
[],
|
|
875
|
+
[ts.factory.createIdentifier("/(?:)/")],
|
|
876
|
+
);
|
|
869
877
|
}
|
|
870
878
|
|
|
871
879
|
type Atomic = boolean | number | string | bigint;
|
|
@@ -378,7 +378,8 @@ export namespace MiscCloneProgrammer {
|
|
|
378
378
|
type === "Int32Array" ||
|
|
379
379
|
type === "BigInt64Array" ||
|
|
380
380
|
type === "Float32Array" ||
|
|
381
|
-
type === "Float64Array"
|
|
381
|
+
type === "Float64Array" ||
|
|
382
|
+
type === "RegExp"
|
|
382
383
|
? decode_native_copyable(type)(input)
|
|
383
384
|
: type === "ArrayBuffer" || type === "SharedArrayBuffer"
|
|
384
385
|
? decode_native_buffer(type)(input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compare the equivalence of the two types X and Y.
|
|
3
|
+
*
|
|
4
|
+
* The two types X and Y refer to any type that can be expressed in the type script, such as the union type and the object type.
|
|
5
|
+
*
|
|
6
|
+
* @template X One of the types to compare
|
|
7
|
+
* @template Y One of the types to compare
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* type Answer = Equal<1 | 2, 1 | 2>; // true
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @author Kyungsu Kang - https://github.com/kakasoo
|
|
14
|
+
*/
|
|
15
|
+
export type Equal<X, Y> =
|
|
16
|
+
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
|
|
17
|
+
? true
|
|
18
|
+
: false;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type NativeClass =
|
|
2
|
+
| Date
|
|
3
|
+
| Set<any>
|
|
4
|
+
| Map<any, any>
|
|
5
|
+
| WeakSet<any>
|
|
6
|
+
| WeakMap<any, any>
|
|
7
|
+
| Uint8Array
|
|
8
|
+
| Uint8ClampedArray
|
|
9
|
+
| Uint16Array
|
|
10
|
+
| Uint32Array
|
|
11
|
+
| BigUint64Array
|
|
12
|
+
| Int8Array
|
|
13
|
+
| Int16Array
|
|
14
|
+
| Int32Array
|
|
15
|
+
| BigInt64Array
|
|
16
|
+
| Float32Array
|
|
17
|
+
| Float64Array
|
|
18
|
+
| ArrayBuffer
|
|
19
|
+
| SharedArrayBuffer
|
|
20
|
+
| DataView
|
|
21
|
+
| Blob
|
|
22
|
+
| File
|
|
23
|
+
| RegExp;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type ValueOf<Instance> =
|
|
2
|
+
IsValueOf<Instance, Boolean> extends true
|
|
3
|
+
? boolean
|
|
4
|
+
: IsValueOf<Instance, Number> extends true
|
|
5
|
+
? number
|
|
6
|
+
: IsValueOf<Instance, String> extends true
|
|
7
|
+
? string
|
|
8
|
+
: Instance;
|
|
9
|
+
|
|
10
|
+
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
11
|
+
? Object extends IValueOf<infer Primitive>
|
|
12
|
+
? Instance extends Primitive
|
|
13
|
+
? false
|
|
14
|
+
: true // not Primitive, but Object
|
|
15
|
+
: false // cannot be
|
|
16
|
+
: false;
|
|
17
|
+
|
|
18
|
+
interface IValueOf<T> {
|
|
19
|
+
valueOf(): T;
|
|
20
|
+
}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { StringUtil } from "../StringUtil";
|
|
2
2
|
|
|
3
3
|
export function snake(str: string): string {
|
|
4
|
+
if (str.length === 0) return str;
|
|
5
|
+
|
|
6
|
+
// PREFIX
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
+
let prefix: string = "";
|
|
9
|
+
for (let i: number = 0; i < str.length; i++) {
|
|
10
|
+
if (str[i] === "_") prefix += "_";
|
|
11
|
+
else break;
|
|
12
|
+
}
|
|
13
|
+
if (prefix.length !== 0) str = str.substring(prefix.length);
|
|
14
|
+
|
|
15
|
+
const out = (s: string) => `${prefix}${s}`;
|
|
16
|
+
|
|
17
|
+
// SNAKE CASE
|
|
18
|
+
const items: string[] = str.split("_");
|
|
19
|
+
if (items.length > 1) return out(items.map((s) => s.toLowerCase()).join("_"));
|
|
20
|
+
|
|
21
|
+
// CAMEL OR PASCAL CASE
|
|
4
22
|
const indexes: number[] = [];
|
|
5
23
|
for (let i: number = 0; i < str.length; i++) {
|
|
6
24
|
const code: number = str.charCodeAt(i);
|
|
@@ -23,29 +41,33 @@ export function snake(str: string): string {
|
|
|
23
41
|
ret += "_";
|
|
24
42
|
}
|
|
25
43
|
ret += str.substring(indexes[indexes.length - 1]!).toLowerCase();
|
|
26
|
-
return ret;
|
|
44
|
+
return out(ret);
|
|
27
45
|
}
|
|
28
46
|
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
export const camel = (str: string): string =>
|
|
48
|
+
unsnake({
|
|
49
|
+
plain: (str) =>
|
|
50
|
+
str.length
|
|
51
|
+
? str === str.toUpperCase()
|
|
52
|
+
? str.toLocaleLowerCase()
|
|
53
|
+
: `${str[0]!.toLowerCase()}${str.substring(1)}`
|
|
54
|
+
: str,
|
|
55
|
+
snake: (str, i) =>
|
|
56
|
+
i === 0 ? str.toLowerCase() : StringUtil.capitalize(str.toLowerCase()),
|
|
35
57
|
})(str);
|
|
36
|
-
}
|
|
37
58
|
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else return str;
|
|
59
|
+
export const pascal = (str: string): string =>
|
|
60
|
+
unsnake({
|
|
61
|
+
plain: (str) =>
|
|
62
|
+
str.length ? `${str[0]!.toUpperCase()}${str.substring(1)}` : str,
|
|
63
|
+
snake: StringUtil.capitalize,
|
|
44
64
|
})(str);
|
|
45
|
-
}
|
|
46
65
|
|
|
47
66
|
const unsnake =
|
|
48
|
-
(
|
|
67
|
+
(props: {
|
|
68
|
+
plain: (str: string) => string;
|
|
69
|
+
snake: (str: string, index: number) => string;
|
|
70
|
+
}) =>
|
|
49
71
|
(str: string): string => {
|
|
50
72
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
73
|
let prefix: string = "";
|
|
@@ -55,32 +77,13 @@ const unsnake =
|
|
|
55
77
|
}
|
|
56
78
|
if (prefix.length !== 0) str = str.substring(prefix.length);
|
|
57
79
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const ch: string = str[i]!;
|
|
61
|
-
if (ch !== "_") continue;
|
|
62
|
-
|
|
63
|
-
const last = indexes[indexes.length - 1];
|
|
64
|
-
if (last === undefined || last[0] + last[1] !== i) indexes.push([i, 1]);
|
|
65
|
-
else ++last[1];
|
|
66
|
-
}
|
|
67
|
-
if (indexes.length === 0) return prefix + escaper(str);
|
|
80
|
+
const out = (s: string) => `${prefix}${s}`;
|
|
81
|
+
if (str.length === 0) return out("");
|
|
68
82
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
else ret += str.substring(0, first);
|
|
76
|
-
else {
|
|
77
|
-
const [prevFirst, prevLength] = indexes[i - 1]!;
|
|
78
|
-
const piece: string = str.substring(prevFirst + prevLength, first);
|
|
79
|
-
if (piece.length) ret += StringUtil.capitalize(piece);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const last = indexes[indexes.length - 1]!;
|
|
83
|
-
const piece: string = str.substring(last[0] + last[1]);
|
|
84
|
-
if (last.length) ret += StringUtil.capitalize(piece);
|
|
85
|
-
return prefix + escaper(ret);
|
|
83
|
+
const items: string[] = str.split("_").filter((s) => s.length !== 0);
|
|
84
|
+
return items.length === 0
|
|
85
|
+
? out("")
|
|
86
|
+
: items.length === 1
|
|
87
|
+
? out(props.plain(items[0]!))
|
|
88
|
+
: out(items.map(props.snake).join(""));
|
|
86
89
|
};
|