@simplysm/core-common 13.0.69 → 13.0.71
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 +66 -267
- package/dist/common.types.d.ts +14 -14
- package/dist/errors/argument-error.d.ts +10 -10
- package/dist/errors/argument-error.d.ts.map +1 -1
- package/dist/errors/argument-error.js +2 -2
- package/dist/errors/argument-error.js.map +1 -1
- package/dist/errors/not-implemented-error.d.ts +8 -8
- package/dist/errors/not-implemented-error.js +2 -2
- package/dist/errors/not-implemented-error.js.map +1 -1
- package/dist/errors/sd-error.d.ts +10 -10
- package/dist/errors/sd-error.d.ts.map +1 -1
- package/dist/errors/timeout-error.d.ts +10 -10
- package/dist/errors/timeout-error.js +3 -3
- package/dist/errors/timeout-error.js.map +1 -1
- package/dist/extensions/arr-ext.d.ts +2 -2
- package/dist/extensions/arr-ext.helpers.d.ts +8 -8
- package/dist/extensions/arr-ext.helpers.js +1 -1
- package/dist/extensions/arr-ext.helpers.js.map +1 -1
- package/dist/extensions/arr-ext.js +13 -13
- package/dist/extensions/arr-ext.js.map +1 -1
- package/dist/extensions/arr-ext.types.d.ts +57 -57
- package/dist/extensions/arr-ext.types.d.ts.map +1 -1
- package/dist/extensions/map-ext.d.ts +16 -16
- package/dist/extensions/set-ext.d.ts +11 -11
- package/dist/features/debounce-queue.d.ts +17 -15
- package/dist/features/debounce-queue.d.ts.map +1 -1
- package/dist/features/debounce-queue.js +6 -6
- package/dist/features/debounce-queue.js.map +1 -1
- package/dist/features/event-emitter.d.ts +20 -20
- package/dist/features/event-emitter.js +17 -17
- package/dist/features/serial-queue.d.ts +11 -11
- package/dist/features/serial-queue.js +5 -5
- package/dist/features/serial-queue.js.map +1 -1
- package/dist/globals.d.ts +4 -4
- package/dist/types/date-only.d.ts +64 -64
- package/dist/types/date-only.d.ts.map +1 -1
- package/dist/types/date-only.js +63 -63
- package/dist/types/date-time.d.ts +37 -37
- package/dist/types/date-time.d.ts.map +1 -1
- package/dist/types/date-time.js +54 -37
- package/dist/types/date-time.js.map +1 -1
- package/dist/types/lazy-gc-map.d.ts +26 -26
- package/dist/types/lazy-gc-map.d.ts.map +1 -1
- package/dist/types/lazy-gc-map.js +26 -26
- package/dist/types/lazy-gc-map.js.map +1 -1
- package/dist/types/time.d.ts +25 -25
- package/dist/types/time.d.ts.map +1 -1
- package/dist/types/time.js +25 -25
- package/dist/types/time.js.map +1 -1
- package/dist/types/uuid.d.ts +11 -11
- package/dist/types/uuid.d.ts.map +1 -1
- package/dist/types/uuid.js +12 -12
- package/dist/types/uuid.js.map +1 -1
- package/dist/utils/bytes.d.ts +17 -17
- package/dist/utils/bytes.js +4 -4
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/date-format.d.ts +45 -45
- package/dist/utils/date-format.js +1 -1
- package/dist/utils/date-format.js.map +1 -1
- package/dist/utils/error.d.ts +4 -4
- package/dist/utils/json.d.ts +17 -17
- package/dist/utils/json.js +3 -3
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/num.d.ts +23 -23
- package/dist/utils/obj.d.ts +111 -111
- package/dist/utils/obj.d.ts.map +1 -1
- package/dist/utils/obj.js +3 -3
- package/dist/utils/obj.js.map +1 -1
- package/dist/utils/path.d.ts +10 -10
- package/dist/utils/primitive.d.ts +5 -5
- package/dist/utils/primitive.js +1 -1
- package/dist/utils/primitive.js.map +1 -1
- package/dist/utils/str.d.ts +46 -46
- package/dist/utils/str.d.ts.map +1 -1
- package/dist/utils/str.js +5 -5
- package/dist/utils/str.js.map +1 -1
- package/dist/utils/template-strings.d.ts +26 -26
- package/dist/utils/transferable.d.ts +18 -18
- package/dist/utils/transferable.js +1 -1
- package/dist/utils/transferable.js.map +1 -1
- package/dist/utils/wait.d.ts +9 -9
- package/dist/utils/xml.d.ts +13 -13
- package/dist/utils/xml.d.ts.map +1 -1
- package/dist/utils/xml.js +1 -0
- package/dist/utils/xml.js.map +1 -1
- package/dist/zip/sd-zip.d.ts +22 -22
- package/dist/zip/sd-zip.js +16 -16
- package/package.json +4 -4
- package/src/common.types.ts +17 -17
- package/src/errors/argument-error.ts +15 -15
- package/src/errors/not-implemented-error.ts +9 -9
- package/src/errors/sd-error.ts +12 -12
- package/src/errors/timeout-error.ts +12 -12
- package/src/extensions/arr-ext.helpers.ts +10 -10
- package/src/extensions/arr-ext.ts +57 -57
- package/src/extensions/arr-ext.types.ts +59 -59
- package/src/extensions/map-ext.ts +16 -16
- package/src/extensions/set-ext.ts +11 -11
- package/src/features/debounce-queue.ts +21 -19
- package/src/features/event-emitter.ts +25 -25
- package/src/features/serial-queue.ts +13 -13
- package/src/globals.ts +4 -4
- package/src/index.ts +1 -1
- package/src/types/date-only.ts +83 -83
- package/src/types/date-time.ts +64 -44
- package/src/types/lazy-gc-map.ts +45 -45
- package/src/types/time.ts +34 -34
- package/src/types/uuid.ts +17 -17
- package/src/utils/bytes.ts +35 -35
- package/src/utils/date-format.ts +65 -65
- package/src/utils/error.ts +4 -4
- package/src/utils/json.ts +39 -39
- package/src/utils/num.ts +23 -23
- package/src/utils/obj.ts +138 -138
- package/src/utils/path.ts +10 -10
- package/src/utils/primitive.ts +6 -6
- package/src/utils/str.ts +260 -261
- package/src/utils/template-strings.ts +29 -29
- package/src/utils/transferable.ts +284 -284
- package/src/utils/wait.ts +10 -10
- package/src/utils/xml.ts +20 -19
- package/src/zip/sd-zip.ts +25 -25
- package/tests/errors/errors.spec.ts +80 -0
- package/tests/extensions/array-extension.spec.ts +796 -0
- package/tests/extensions/map-extension.spec.ts +147 -0
- package/tests/extensions/set-extension.spec.ts +74 -0
- package/tests/types/date-only.spec.ts +638 -0
- package/tests/types/date-time.spec.ts +391 -0
- package/tests/types/lazy-gc-map.spec.ts +692 -0
- package/tests/types/time.spec.ts +559 -0
- package/tests/types/uuid.spec.ts +74 -0
- package/tests/utils/bytes-utils.spec.ts +230 -0
- package/tests/utils/date-format.spec.ts +373 -0
- package/tests/utils/debounce-queue.spec.ts +272 -0
- package/tests/utils/json.spec.ts +486 -0
- package/tests/utils/number.spec.ts +157 -0
- package/tests/utils/object.spec.ts +829 -0
- package/tests/utils/path.spec.ts +78 -0
- package/tests/utils/primitive.spec.ts +43 -0
- package/tests/utils/sd-event-emitter.spec.ts +216 -0
- package/tests/utils/serial-queue.spec.ts +365 -0
- package/tests/utils/string.spec.ts +281 -0
- package/tests/utils/template-strings.spec.ts +57 -0
- package/tests/utils/transferable.spec.ts +703 -0
- package/tests/utils/wait.spec.ts +145 -0
- package/tests/utils/xml.spec.ts +146 -0
- package/tests/zip/sd-zip.spec.ts +238 -0
- package/docs/extensions.md +0 -503
- package/docs/features.md +0 -109
- package/docs/types.md +0 -486
- package/docs/utils.md +0 -780
package/src/utils/primitive.ts
CHANGED
|
@@ -6,13 +6,13 @@ import { ArgumentError } from "../errors/argument-error";
|
|
|
6
6
|
import type { PrimitiveTypeMap, PrimitiveTypeStr } from "../common.types";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Infer PrimitiveTypeStr from a value
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* Checks the type of a value at runtime and returns the corresponding PrimitiveTypeStr.
|
|
12
12
|
*
|
|
13
|
-
* @param value
|
|
14
|
-
* @returns
|
|
15
|
-
* @throws ArgumentError
|
|
13
|
+
* @param value The value to infer the type from
|
|
14
|
+
* @returns The PrimitiveTypeStr corresponding to the value
|
|
15
|
+
* @throws ArgumentError If the type is not supported
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* getPrimitiveTypeStr("hello") // "string"
|
|
@@ -29,5 +29,5 @@ export function getPrimitiveTypeStr(value: PrimitiveTypeMap[PrimitiveTypeStr]):
|
|
|
29
29
|
if (value instanceof Time) return "Time";
|
|
30
30
|
if (value instanceof Uuid) return "Uuid";
|
|
31
31
|
if (value instanceof Uint8Array) return "Bytes";
|
|
32
|
-
throw new ArgumentError("
|
|
32
|
+
throw new ArgumentError("Unknown value type.", { type: typeof value });
|
|
33
33
|
}
|
package/src/utils/str.ts
CHANGED
|
@@ -1,261 +1,260 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
//#region
|
|
6
|
-
|
|
7
|
-
//
|
|
8
|
-
const suffixTable = {
|
|
9
|
-
을: { t: "을", f: "를" },
|
|
10
|
-
은: { t: "은", f: "는" },
|
|
11
|
-
이: { t: "이", f: "가" },
|
|
12
|
-
와: { t: "과", f: "와" },
|
|
13
|
-
랑: { t: "이랑", f: "랑" },
|
|
14
|
-
로: { t: "으로", f: "로" },
|
|
15
|
-
라: { t: "이라", f: "라" },
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
*
|
|
20
|
-
* @param text
|
|
21
|
-
* @param type
|
|
22
|
-
* - `"을"`: 을/를
|
|
23
|
-
* - `"은"`: 은/는
|
|
24
|
-
* - `"이"`: 이/가
|
|
25
|
-
* - `"와"`: 과/와
|
|
26
|
-
* - `"랑"`: 이랑/랑
|
|
27
|
-
* - `"로"`: 으로/로
|
|
28
|
-
* - `"라"`: 이라/라
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*/
|
|
34
|
-
export function
|
|
35
|
-
text: string,
|
|
36
|
-
type: "을" | "은" | "이" | "와" | "랑" | "로" | "라",
|
|
37
|
-
): string {
|
|
38
|
-
const table = suffixTable;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"
|
|
110
|
-
"
|
|
111
|
-
"
|
|
112
|
-
"
|
|
113
|
-
"
|
|
114
|
-
"
|
|
115
|
-
"
|
|
116
|
-
"
|
|
117
|
-
"
|
|
118
|
-
"
|
|
119
|
-
"
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
"
|
|
123
|
-
"
|
|
124
|
-
"
|
|
125
|
-
"
|
|
126
|
-
"
|
|
127
|
-
"
|
|
128
|
-
"
|
|
129
|
-
"
|
|
130
|
-
"
|
|
131
|
-
"
|
|
132
|
-
"
|
|
133
|
-
"
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* -
|
|
145
|
-
* -
|
|
146
|
-
* -
|
|
147
|
-
* -
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
*
|
|
164
|
-
* @example "
|
|
165
|
-
* @example "
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.replace(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
* @example "
|
|
177
|
-
* @example "
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
.replace(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* @example "
|
|
190
|
-
* @example "
|
|
191
|
-
* @example "
|
|
192
|
-
* @example "
|
|
193
|
-
* @example "
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @example "
|
|
204
|
-
* @example "
|
|
205
|
-
* @example "
|
|
206
|
-
* @example "
|
|
207
|
-
* @example "
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
.replace(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* @
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* @param
|
|
248
|
-
* @param
|
|
249
|
-
* @
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* strInsert("
|
|
254
|
-
* strInsert("abc",
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
//#endregion
|
|
1
|
+
/**
|
|
2
|
+
* String utility functions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
//#region Korean particle handling
|
|
6
|
+
|
|
7
|
+
// Korean particle mapping table (created only once when module loads)
|
|
8
|
+
const suffixTable = {
|
|
9
|
+
을: { t: "을", f: "를" },
|
|
10
|
+
은: { t: "은", f: "는" },
|
|
11
|
+
이: { t: "이", f: "가" },
|
|
12
|
+
와: { t: "과", f: "와" },
|
|
13
|
+
랑: { t: "이랑", f: "랑" },
|
|
14
|
+
로: { t: "으로", f: "로" },
|
|
15
|
+
라: { t: "이라", f: "라" },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Return the appropriate Korean particle based on the final consonant
|
|
20
|
+
* @param text The text to check
|
|
21
|
+
* @param type The particle type
|
|
22
|
+
* - `"을"`: 을/를 (eul/reul - object particle)
|
|
23
|
+
* - `"은"`: 은/는 (eun/neun - subject particle)
|
|
24
|
+
* - `"이"`: 이/가 (i/ga - subject particle)
|
|
25
|
+
* - `"와"`: 과/와 (gwa/wa - and particle)
|
|
26
|
+
* - `"랑"`: 이랑/랑 (irang/rang - and particle)
|
|
27
|
+
* - `"로"`: 으로/로 (euro/ro - instrumental particle)
|
|
28
|
+
* - `"라"`: 이라/라 (ira/ra - copula particle)
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* koreanGetSuffix("Apple", "을") // "를"
|
|
32
|
+
* koreanGetSuffix("책", "이") // "이"
|
|
33
|
+
*/
|
|
34
|
+
export function koreanGetSuffix(
|
|
35
|
+
text: string,
|
|
36
|
+
type: "을" | "은" | "이" | "와" | "랑" | "로" | "라",
|
|
37
|
+
): string {
|
|
38
|
+
const table = suffixTable;
|
|
39
|
+
|
|
40
|
+
if (text.length === 0) {
|
|
41
|
+
return table[type].f;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const lastCharCode = text.charCodeAt(text.length - 1);
|
|
45
|
+
|
|
46
|
+
// Hangul range check (0xAC00 ~ 0xD7A3)
|
|
47
|
+
if (lastCharCode < 0xac00 || lastCharCode > 0xd7a3) {
|
|
48
|
+
return table[type].f;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Determine if final consonant (jongseong) exists
|
|
52
|
+
const jongseongIndex = (lastCharCode - 0xac00) % 28;
|
|
53
|
+
const hasLast = jongseongIndex !== 0;
|
|
54
|
+
|
|
55
|
+
// Special handling for "로" particle: when final consonant is ㄹ (jongseong index 8), use "로"
|
|
56
|
+
if (type === "로" && jongseongIndex === 8) {
|
|
57
|
+
return table[type].f;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return hasLast ? table[type].t : table[type].f;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
|
|
65
|
+
//#region Full-width to half-width conversion
|
|
66
|
+
|
|
67
|
+
// Full-width to half-width mapping table (created only once when module loads)
|
|
68
|
+
const fullWidthCharMap: Record<string, string> = {
|
|
69
|
+
"A": "A",
|
|
70
|
+
"B": "B",
|
|
71
|
+
"C": "C",
|
|
72
|
+
"D": "D",
|
|
73
|
+
"E": "E",
|
|
74
|
+
"F": "F",
|
|
75
|
+
"G": "G",
|
|
76
|
+
"H": "H",
|
|
77
|
+
"I": "I",
|
|
78
|
+
"J": "J",
|
|
79
|
+
"K": "K",
|
|
80
|
+
"L": "L",
|
|
81
|
+
"M": "M",
|
|
82
|
+
"N": "N",
|
|
83
|
+
"O": "O",
|
|
84
|
+
"P": "P",
|
|
85
|
+
"Q": "Q",
|
|
86
|
+
"R": "R",
|
|
87
|
+
"S": "S",
|
|
88
|
+
"T": "T",
|
|
89
|
+
"U": "U",
|
|
90
|
+
"V": "V",
|
|
91
|
+
"W": "W",
|
|
92
|
+
"X": "X",
|
|
93
|
+
"Y": "Y",
|
|
94
|
+
"Z": "Z",
|
|
95
|
+
"a": "a",
|
|
96
|
+
"b": "b",
|
|
97
|
+
"c": "c",
|
|
98
|
+
"d": "d",
|
|
99
|
+
"e": "e",
|
|
100
|
+
"f": "f",
|
|
101
|
+
"g": "g",
|
|
102
|
+
"h": "h",
|
|
103
|
+
"i": "i",
|
|
104
|
+
"j": "j",
|
|
105
|
+
"k": "k",
|
|
106
|
+
"l": "l",
|
|
107
|
+
"m": "m",
|
|
108
|
+
"n": "n",
|
|
109
|
+
"o": "o",
|
|
110
|
+
"p": "p",
|
|
111
|
+
"q": "q",
|
|
112
|
+
"r": "r",
|
|
113
|
+
"s": "s",
|
|
114
|
+
"t": "t",
|
|
115
|
+
"u": "u",
|
|
116
|
+
"v": "v",
|
|
117
|
+
"w": "w",
|
|
118
|
+
"x": "x",
|
|
119
|
+
"y": "y",
|
|
120
|
+
"z": "z",
|
|
121
|
+
"0": "0",
|
|
122
|
+
"1": "1",
|
|
123
|
+
"2": "2",
|
|
124
|
+
"3": "3",
|
|
125
|
+
"4": "4",
|
|
126
|
+
"5": "5",
|
|
127
|
+
"6": "6",
|
|
128
|
+
"7": "7",
|
|
129
|
+
"8": "8",
|
|
130
|
+
"9": "9",
|
|
131
|
+
" ": " ",
|
|
132
|
+
")": ")",
|
|
133
|
+
"(": "(",
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Regex also created only once
|
|
137
|
+
const fullWidthCharRegex = new RegExp(`[${Object.keys(fullWidthCharMap).join("")}]`, "g");
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Convert full-width characters to half-width characters
|
|
141
|
+
*
|
|
142
|
+
* Conversion targets:
|
|
143
|
+
* - Full-width uppercase letters (A-Z → A-Z)
|
|
144
|
+
* - Full-width lowercase letters (a-z → a-z)
|
|
145
|
+
* - Full-width digits (0-9 → 0-9)
|
|
146
|
+
* - Full-width space ( → regular space)
|
|
147
|
+
* - Full-width parentheses (() → ())
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* strReplaceFullWidth("A123") // "A123"
|
|
151
|
+
* strReplaceFullWidth("(株)") // "(株)"
|
|
152
|
+
*/
|
|
153
|
+
export function strReplaceFullWidth(str: string): string {
|
|
154
|
+
return str.replace(fullWidthCharRegex, (char) => fullWidthCharMap[char] ?? char);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
|
|
159
|
+
//#region Case conversion
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convert to PascalCase
|
|
163
|
+
* @example "hello-world" → "HelloWorld"
|
|
164
|
+
* @example "hello_world" → "HelloWorld"
|
|
165
|
+
* @example "hello.world" → "HelloWorld"
|
|
166
|
+
*/
|
|
167
|
+
export function strToPascalCase(str: string): string {
|
|
168
|
+
return str
|
|
169
|
+
.replace(/[-._][a-z]/g, (m) => m[1].toUpperCase())
|
|
170
|
+
.replace(/^[a-z]/, (m) => m.toUpperCase());
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Convert to camelCase
|
|
175
|
+
* @example "hello-world" → "helloWorld"
|
|
176
|
+
* @example "hello_world" → "helloWorld"
|
|
177
|
+
* @example "HelloWorld" → "helloWorld"
|
|
178
|
+
*/
|
|
179
|
+
export function strToCamelCase(str: string): string {
|
|
180
|
+
return str
|
|
181
|
+
.replace(/[-._][a-z]/g, (m) => m[1].toUpperCase())
|
|
182
|
+
.replace(/^[A-Z]/, (m) => m.toLowerCase());
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Convert to kebab-case
|
|
187
|
+
*
|
|
188
|
+
* @example "HelloWorld" → "hello-world"
|
|
189
|
+
* @example "helloWorld" → "hello-world"
|
|
190
|
+
* @example "hello_world" → "hello_world" (no conversion if only lowercase)
|
|
191
|
+
* @example "Hello_World" → "hello-_world" (existing separators are preserved)
|
|
192
|
+
* @example "Hello-World" → "hello--world" (existing separators are preserved)
|
|
193
|
+
* @example "XMLParser" → "x-m-l-parser" (consecutive uppercase letters are separated)
|
|
194
|
+
*/
|
|
195
|
+
export function strToKebabCase(str: string): string {
|
|
196
|
+
return toCaseWithSeparator(str, "-");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Convert to snake_case
|
|
201
|
+
*
|
|
202
|
+
* @example "HelloWorld" → "hello_world"
|
|
203
|
+
* @example "helloWorld" → "hello_world"
|
|
204
|
+
* @example "hello-world" → "hello-world" (no conversion if only lowercase)
|
|
205
|
+
* @example "Hello-World" → "hello_-world" (existing separators are preserved)
|
|
206
|
+
* @example "Hello_World" → "hello__world" (existing separators are preserved)
|
|
207
|
+
* @example "XMLParser" → "x_m_l_parser" (consecutive uppercase letters are separated)
|
|
208
|
+
*/
|
|
209
|
+
export function strToSnakeCase(str: string): string {
|
|
210
|
+
return toCaseWithSeparator(str, "_");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function toCaseWithSeparator(str: string, separator: string): string {
|
|
214
|
+
return str
|
|
215
|
+
.replace(/^[A-Z]/, (m) => m.toLowerCase())
|
|
216
|
+
.replace(/[-_]?[A-Z]/g, (m) => separator + m.toLowerCase());
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
|
|
221
|
+
//#region Other
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Check if string is undefined or empty (type guard)
|
|
225
|
+
*
|
|
226
|
+
* @param str The string to check
|
|
227
|
+
* @returns true if undefined, null, or empty string
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* const name: string | undefined = getValue();
|
|
231
|
+
* if (strIsNullOrEmpty(name)) {
|
|
232
|
+
* // name: "" | undefined
|
|
233
|
+
* console.log("Name is empty");
|
|
234
|
+
* } else {
|
|
235
|
+
* // name: string (non-empty string)
|
|
236
|
+
* console.log(`Name: ${name}`);
|
|
237
|
+
* }
|
|
238
|
+
*/
|
|
239
|
+
export function strIsNullOrEmpty(str: string | undefined): str is "" | undefined {
|
|
240
|
+
return str == null || str === "";
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Insert a string at a specific position
|
|
245
|
+
*
|
|
246
|
+
* @param str The original string
|
|
247
|
+
* @param index The position to insert at (0-based)
|
|
248
|
+
* @param insertString The string to insert
|
|
249
|
+
* @returns A new string with the insertion applied
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* strInsert("Hello World", 5, ","); // "Hello, World"
|
|
253
|
+
* strInsert("abc", 0, "X"); // "Xabc"
|
|
254
|
+
* strInsert("abc", 3, "X"); // "abcX"
|
|
255
|
+
*/
|
|
256
|
+
export function strInsert(str: string, index: number, insertString: string): string {
|
|
257
|
+
return str.substring(0, index) + insertString + str.substring(index);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
//#endregion
|