@simplysm/core-common 14.0.23 → 14.0.24

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/docs/utilities.md DELETED
@@ -1,723 +0,0 @@
1
- # Utility Namespaces
2
-
3
- All utility namespaces are exported as `export * as name` from `index.ts`. Access them as `obj.clone(...)`, `str.toPascalCase(...)`, etc.
4
-
5
- ---
6
-
7
- ## `obj` -- Object Utilities
8
-
9
- ### `clone`
10
-
11
- Deep clone with circular reference support and custom type handling (DateTime, DateOnly, Time, Uuid, Uint8Array, RegExp, Error, Map, Set). Prototype chains are preserved. Functions and Symbols are kept by reference.
12
-
13
- ```typescript
14
- function clone<TObj>(source: TObj): TObj;
15
- ```
16
-
17
- ### `equal`
18
-
19
- Deep equality comparison with extensive options.
20
-
21
- ```typescript
22
- function equal(source: unknown, target: unknown, options?: EqualOptions): boolean;
23
- ```
24
-
25
- ```typescript
26
- interface EqualOptions {
27
- topLevelIncludes?: string[];
28
- topLevelExcludes?: string[];
29
- ignoreArrayIndex?: boolean;
30
- shallow?: boolean;
31
- }
32
- ```
33
-
34
- | Option | Description |
35
- |--------|-------------|
36
- | `topLevelIncludes` | Only compare these keys (top-level object properties only) |
37
- | `topLevelExcludes` | Exclude these keys from comparison (top-level only) |
38
- | `ignoreArrayIndex` | Treat arrays as unordered sets (O(n^2)) |
39
- | `shallow` | Only compare one level deep (reference comparison for nested values) |
40
-
41
- Supports: Date, DateTime, DateOnly, Time, Uuid, RegExp, Array, Map, Set, plain objects.
42
-
43
- ### `merge`
44
-
45
- Deep merge. Returns a new object (immutable). Source is the base, target values override.
46
-
47
- ```typescript
48
- function merge<TSource, TMergeTarget>(
49
- source: TSource,
50
- target: TMergeTarget,
51
- opt?: MergeOptions,
52
- ): TSource & TMergeTarget;
53
- ```
54
-
55
- ```typescript
56
- interface MergeOptions {
57
- arrayProcess?: "replace" | "concat";
58
- useDelTargetNull?: boolean;
59
- }
60
- ```
61
-
62
- | Option | Description |
63
- |--------|-------------|
64
- | `arrayProcess` | `"replace"` (default): target array replaces source. `"concat"`: merge arrays with Set-based dedup. |
65
- | `useDelTargetNull` | If `true`, `null` in target deletes the key from the result. |
66
-
67
- ### `merge3`
68
-
69
- Three-way merge for conflict detection. Compares source, origin (base), and target.
70
-
71
- ```typescript
72
- function merge3<
73
- S extends Record<string, unknown>,
74
- O extends Record<string, unknown>,
75
- T extends Record<string, unknown>,
76
- >(
77
- source: S,
78
- origin: O,
79
- target: T,
80
- optionsObj?: Record<string, Merge3KeyOptions>,
81
- ): { conflict: boolean; result: O & S & T };
82
- ```
83
-
84
- ```typescript
85
- interface Merge3KeyOptions {
86
- keys?: string[];
87
- excludes?: string[];
88
- ignoreArrayIndex?: boolean;
89
- }
90
- ```
91
-
92
- Rules:
93
- - source == origin, target differs -> use target
94
- - target == origin, source differs -> use source
95
- - source == target -> use that value
96
- - all three differ -> conflict (origin value kept)
97
-
98
- ### `omit`
99
-
100
- Create a new object with specified keys removed.
101
-
102
- ```typescript
103
- function omit<T extends Record<string, unknown>, K extends keyof T>(
104
- item: T,
105
- omitKeys: K[],
106
- ): Omit<T, K>;
107
- ```
108
-
109
- ### `omitByFilter`
110
-
111
- Create a new object with keys matching a predicate removed.
112
-
113
- ```typescript
114
- function omitByFilter<T extends Record<string, unknown>>(
115
- item: T,
116
- omitKeyFn: (key: keyof T) => boolean,
117
- ): T;
118
- ```
119
-
120
- ### `pick`
121
-
122
- Create a new object with only the specified keys.
123
-
124
- ```typescript
125
- function pick<T extends Record<string, unknown>, K extends keyof T>(
126
- item: T,
127
- pickKeys: K[],
128
- ): Pick<T, K>;
129
- ```
130
-
131
- ### `getChainValue`
132
-
133
- Get a nested value using a dot/bracket path string.
134
-
135
- ```typescript
136
- function getChainValue(obj: unknown, chain: string): unknown;
137
- function getChainValue(obj: unknown, chain: string, optional: true): unknown | undefined;
138
- ```
139
-
140
- Path syntax: `"a.b[0].c"`, `"a['key'].b"`
141
-
142
- ### `getChainValueByDepth`
143
-
144
- Descend into a nested object by repeatedly accessing the same key.
145
-
146
- ```typescript
147
- function getChainValueByDepth<TObject, TKey extends keyof TObject>(
148
- obj: TObject,
149
- key: TKey,
150
- depth: number,
151
- ): TObject[TKey];
152
- function getChainValueByDepth<TObject, TKey extends keyof TObject>(
153
- obj: TObject,
154
- key: TKey,
155
- depth: number,
156
- optional: true,
157
- ): TObject[TKey] | undefined;
158
- ```
159
-
160
- ### `setChainValue`
161
-
162
- Set a nested value using a dot/bracket path string. Creates intermediate objects as needed.
163
-
164
- ```typescript
165
- function setChainValue(obj: unknown, chain: string, value: unknown): void;
166
- ```
167
-
168
- ### `deleteChainValue`
169
-
170
- Delete a nested value using a dot/bracket path string. Silently returns if intermediate path does not exist.
171
-
172
- ```typescript
173
- function deleteChainValue(obj: unknown, chain: string): void;
174
- ```
175
-
176
- ### `clearUndefined`
177
-
178
- Remove all keys with `undefined` values from an object. **Mutates** the original.
179
-
180
- ```typescript
181
- function clearUndefined<T extends object>(obj: T): T;
182
- ```
183
-
184
- ### `clear`
185
-
186
- Remove all keys from an object. **Mutates** the original.
187
-
188
- ```typescript
189
- function clear<T extends Record<string, unknown>>(obj: T): Record<string, never>;
190
- ```
191
-
192
- ### `nullToUndefined`
193
-
194
- Recursively convert all `null` values to `undefined`. **Mutates** arrays and objects in place. Handles circular references.
195
-
196
- ```typescript
197
- function nullToUndefined<TObject>(obj: TObject): TObject | undefined;
198
- ```
199
-
200
- ### `unflatten`
201
-
202
- Convert a flat object with dot-separated keys into a nested object.
203
-
204
- ```typescript
205
- function unflatten(flatObj: Record<string, unknown>): Record<string, unknown>;
206
- ```
207
-
208
- ### `keys`
209
-
210
- Type-safe `Object.keys`.
211
-
212
- ```typescript
213
- function keys<T extends object>(obj: T): (keyof T)[];
214
- ```
215
-
216
- ### `entries`
217
-
218
- Type-safe `Object.entries`.
219
-
220
- ```typescript
221
- function entries<T extends object>(obj: T): { [K in keyof T]: [K, T[K]] }[keyof T][];
222
- ```
223
-
224
- ### `fromEntries`
225
-
226
- Type-safe `Object.fromEntries`.
227
-
228
- ```typescript
229
- function fromEntries<T extends [string, unknown]>(entryPairs: T[]): { [K in T[0]]: T[1] };
230
- ```
231
-
232
- ### `map`
233
-
234
- Transform each entry of an object. Return `[null, newValue]` to keep the original key.
235
-
236
- ```typescript
237
- function map<TSource extends object, TNewKey extends string, TNewValue>(
238
- obj: TSource,
239
- fn: (key: keyof TSource, value: TSource[keyof TSource]) => [TNewKey | null, TNewValue],
240
- ): Record<TNewKey | Extract<keyof TSource, string>, TNewValue>;
241
- ```
242
-
243
- ### Type Utilities (from obj namespace)
244
-
245
- #### `UndefToOptional<T>`
246
-
247
- Convert properties that include `undefined` in their type to optional properties.
248
-
249
- ```typescript
250
- type UndefToOptional<TObject> = {
251
- [K in keyof TObject as undefined extends TObject[K] ? K : never]?: TObject[K];
252
- } & { [K in keyof TObject as undefined extends TObject[K] ? never : K]: TObject[K] };
253
- ```
254
-
255
- #### `OptionalToUndef<T>`
256
-
257
- Convert optional properties to required properties with `| undefined`.
258
-
259
- ```typescript
260
- type OptionalToUndef<TObject> = {
261
- [K in keyof TObject]-?: {} extends Pick<TObject, K> ? TObject[K] | undefined : TObject[K];
262
- };
263
- ```
264
-
265
- #### `EqualOptions`
266
-
267
- See `equal` above.
268
-
269
- #### `MergeOptions`
270
-
271
- See `merge` above.
272
-
273
- #### `Merge3KeyOptions`
274
-
275
- See `merge3` above.
276
-
277
- ---
278
-
279
- ## `str` -- String Utilities
280
-
281
- ### `getKoreanSuffix`
282
-
283
- Return the correct Korean particle (suffix) based on the final consonant of the preceding text.
284
-
285
- ```typescript
286
- function getKoreanSuffix(
287
- text: string,
288
- type: "을" | "은" | "이" | "와" | "랑" | "로" | "라",
289
- ): string;
290
- ```
291
-
292
- | Type | With final consonant | Without |
293
- |------|---------------------|---------|
294
- | `"을"` | 을 | 를 |
295
- | `"은"` | 은 | 는 |
296
- | `"이"` | 이 | 가 |
297
- | `"와"` | 과 | 와 |
298
- | `"랑"` | 이랑 | 랑 |
299
- | `"로"` | 으로 | 로 |
300
- | `"라"` | 이라 | 라 |
301
-
302
- ### `replaceFullWidth`
303
-
304
- Convert full-width characters to half-width. Covers A-Z, a-z, 0-9, space, and parentheses.
305
-
306
- ```typescript
307
- function replaceFullWidth(str: string): string;
308
- ```
309
-
310
- ### `toPascalCase`
311
-
312
- Convert to PascalCase. Recognizes `-`, `_`, `.` as separators.
313
-
314
- ```typescript
315
- function toPascalCase(str: string): string;
316
- ```
317
-
318
- ### `toCamelCase`
319
-
320
- Convert to camelCase. Recognizes `-`, `_`, `.` as separators.
321
-
322
- ```typescript
323
- function toCamelCase(str: string): string;
324
- ```
325
-
326
- ### `toKebabCase`
327
-
328
- Convert to kebab-case. Inserts `-` before uppercase letters.
329
-
330
- ```typescript
331
- function toKebabCase(str: string): string;
332
- ```
333
-
334
- ### `toSnakeCase`
335
-
336
- Convert to snake_case. Inserts `_` before uppercase letters.
337
-
338
- ```typescript
339
- function toSnakeCase(str: string): string;
340
- ```
341
-
342
- ### `isNullOrEmpty`
343
-
344
- Type guard: returns `true` if the string is `undefined`, `null`, or `""`.
345
-
346
- ```typescript
347
- function isNullOrEmpty(str: string | undefined): str is "" | undefined;
348
- ```
349
-
350
- ### `insert`
351
-
352
- Insert a string at a specific index.
353
-
354
- ```typescript
355
- function insert(str: string, index: number, insertString: string): string;
356
- ```
357
-
358
- ---
359
-
360
- ## `num` -- Number Utilities
361
-
362
- ### `parseInt`
363
-
364
- Parse a value to integer. Non-numeric characters (except `0-9`, `-`, `.`) are stripped first. Returns `undefined` if unparseable.
365
-
366
- ```typescript
367
- function parseInt(text: unknown): number | undefined;
368
- ```
369
-
370
- ### `parseFloat`
371
-
372
- Parse a value to float. Non-numeric characters are stripped first.
373
-
374
- ```typescript
375
- function parseFloat(text: unknown): number | undefined;
376
- ```
377
-
378
- ### `parseRoundedInt`
379
-
380
- Parse to float then round to nearest integer.
381
-
382
- ```typescript
383
- function parseRoundedInt(text: unknown): number | undefined;
384
- ```
385
-
386
- ### `isNullOrEmpty`
387
-
388
- Type guard: returns `true` if the value is `undefined`, `null`, or `0`.
389
-
390
- ```typescript
391
- function isNullOrEmpty(val: number | undefined): val is 0 | undefined;
392
- ```
393
-
394
- ### `format`
395
-
396
- Format a number with thousand separators and optional decimal precision.
397
-
398
- ```typescript
399
- function format(val: number, digit?: { max?: number; min?: number }): string;
400
- function format(val: number | undefined, digit?: { max?: number; min?: number }): string | undefined;
401
- ```
402
-
403
- | Option | Description |
404
- |--------|-------------|
405
- | `digit.max` | Maximum decimal places |
406
- | `digit.min` | Minimum decimal places (pads with zeros) |
407
-
408
- ---
409
-
410
- ## `bytes` -- Uint8Array Utilities
411
-
412
- ### `concat`
413
-
414
- Concatenate multiple `Uint8Array` into one.
415
-
416
- ```typescript
417
- function concat(arrays: Bytes[]): Bytes;
418
- ```
419
-
420
- ### `toHex`
421
-
422
- Convert `Uint8Array` to lowercase hex string.
423
-
424
- ```typescript
425
- function toHex(bytes: Bytes): string;
426
- ```
427
-
428
- ### `fromHex`
429
-
430
- Convert hex string to `Uint8Array`. Throws `ArgumentError` on odd length or invalid hex characters.
431
-
432
- ```typescript
433
- function fromHex(hex: string): Bytes;
434
- ```
435
-
436
- ### `toBase64`
437
-
438
- Convert `Uint8Array` to base64 string.
439
-
440
- ```typescript
441
- function toBase64(bytes: Bytes): string;
442
- ```
443
-
444
- ### `fromBase64`
445
-
446
- Convert base64 string to `Uint8Array`. Whitespace is stripped, padding is optional. Throws `ArgumentError` on invalid characters or length.
447
-
448
- ```typescript
449
- function fromBase64(base64: string): Bytes;
450
- ```
451
-
452
- ---
453
-
454
- ## `path` -- Path Utilities
455
-
456
- POSIX-style path utilities for browser environments. Only forward slashes (`/`) are supported. No backslash support.
457
-
458
- ### `join`
459
-
460
- Join path segments with `/`.
461
-
462
- ```typescript
463
- function join(...segments: string[]): string;
464
- ```
465
-
466
- ### `basename`
467
-
468
- Extract the file name from a path. Optionally strip an extension.
469
-
470
- ```typescript
471
- function basename(filePath: string, ext?: string): string;
472
- ```
473
-
474
- ### `extname`
475
-
476
- Extract the file extension (including the dot). Hidden files (e.g., `.gitignore`) return `""`.
477
-
478
- ```typescript
479
- function extname(filePath: string): string;
480
- ```
481
-
482
- ---
483
-
484
- ## `json` -- JSON Utilities
485
-
486
- Custom JSON serialization/deserialization that preserves Simplysm types.
487
-
488
- ### `stringify`
489
-
490
- Serialize an object to JSON string. Custom types are encoded as `{ __type__: "...", data: ... }`.
491
-
492
- Supported types: Date, DateTime, DateOnly, Time, Uuid, Set, Map, Error, Uint8Array.
493
-
494
- ```typescript
495
- function stringify(
496
- obj: unknown,
497
- options?: {
498
- space?: string | number;
499
- replacer?: (key: string | undefined, value: unknown) => unknown;
500
- redactBytes?: boolean;
501
- },
502
- ): string;
503
- ```
504
-
505
- | Option | Description |
506
- |--------|-------------|
507
- | `space` | JSON indentation (number of spaces or string) |
508
- | `replacer` | Custom replacer called before built-in type conversion |
509
- | `redactBytes` | If `true`, Uint8Array contents are replaced with `"__hidden__"` (for logging). Cannot be parsed back. |
510
-
511
- Circular references throw `TypeError`.
512
-
513
- ### `parse`
514
-
515
- Deserialize a JSON string, restoring custom types. All JSON `null` values are converted to `undefined` (Simplysm null-free convention).
516
-
517
- ```typescript
518
- function parse<TResult = unknown>(json: string): TResult;
519
- ```
520
-
521
- In dev mode (`env.DEV`), parse errors include the full JSON string. In production, only the JSON length is included.
522
-
523
- ---
524
-
525
- ## `xml` -- XML Utilities
526
-
527
- Built on `fast-xml-parser`.
528
-
529
- ### `parse`
530
-
531
- Parse XML string to object.
532
-
533
- ```typescript
534
- function parse(str: string, options?: { stripTagPrefix?: boolean }): unknown;
535
- ```
536
-
537
- Output conventions:
538
- - Attributes are grouped under `$`
539
- - Text nodes are stored under `_`
540
- - Child elements are wrapped in arrays (except root)
541
-
542
- | Option | Description |
543
- |--------|-------------|
544
- | `stripTagPrefix` | Remove namespace prefixes from tag names (attributes are kept) |
545
-
546
- ### `stringify`
547
-
548
- Serialize object to XML string.
549
-
550
- ```typescript
551
- function stringify(obj: unknown, options?: XmlBuilderOptions): string;
552
- ```
553
-
554
- ---
555
-
556
- ## `wait` -- Async Wait Utilities
557
-
558
- ### `until`
559
-
560
- Poll a condition function until it returns `true`.
561
-
562
- ```typescript
563
- function until(
564
- forwarder: () => boolean | Promise<boolean>,
565
- milliseconds?: number,
566
- maxCount?: number,
567
- ): Promise<void>;
568
- ```
569
-
570
- | Parameter | Type | Default | Description |
571
- |-----------|------|---------|-------------|
572
- | `forwarder` | `() => boolean \| Promise<boolean>` | -- | Condition to check |
573
- | `milliseconds` | `number` | `100` | Polling interval in ms |
574
- | `maxCount` | `number \| undefined` | unlimited | Max attempts. Throws `TimeoutError` when exceeded. |
575
-
576
- ### `time`
577
-
578
- Wait for a specified duration.
579
-
580
- ```typescript
581
- function time(millisecond: number): Promise<void>;
582
- ```
583
-
584
- ---
585
-
586
- ## `transfer` -- Transferable Utilities
587
-
588
- Serialize/deserialize objects for Worker `postMessage` with transferable support. Handles custom types that `structuredClone` does not support.
589
-
590
- ### `encode`
591
-
592
- Convert an object to a Worker-transferable form. Returns the converted object and a list of `ArrayBuffer` transferables.
593
-
594
- ```typescript
595
- function encode(obj: unknown): {
596
- result: unknown;
597
- transferList: ArrayBuffer[];
598
- };
599
- ```
600
-
601
- Supported types: Date, DateTime, DateOnly, Time, Uuid, RegExp, Error (with cause/code/detail), Uint8Array (zero-copy via transfer), Array, Map, Set, plain objects.
602
-
603
- Throws `TypeError` on circular references (with path info).
604
-
605
- ### `decode`
606
-
607
- Restore an object from its transferable form.
608
-
609
- ```typescript
610
- function decode(obj: unknown): unknown;
611
- ```
612
-
613
- ---
614
-
615
- ## `err` -- Error Utilities
616
-
617
- ### `message`
618
-
619
- Extract a message string from an unknown error value. Returns `err.message` for Error instances, `String(err)` otherwise.
620
-
621
- ```typescript
622
- function message(err: unknown): string;
623
- ```
624
-
625
- ---
626
-
627
- ## `dt` -- Date Format Utilities
628
-
629
- ### `normalizeMonth`
630
-
631
- Normalize year/month/day when month is outside 1-12 range. Adjusts year and clamps day to the target month's last day.
632
-
633
- ```typescript
634
- function normalizeMonth(year: number, month: number, day: number): DtNormalizedMonth;
635
- ```
636
-
637
- ```typescript
638
- interface DtNormalizedMonth {
639
- year: number;
640
- month: number;
641
- day: number;
642
- }
643
- ```
644
-
645
- ### `convert12To24`
646
-
647
- Convert 12-hour format to 24-hour format.
648
-
649
- ```typescript
650
- function convert12To24(rawHour: number, isPM: boolean): number;
651
- ```
652
-
653
- ### `format`
654
-
655
- Format date/time components using a C#-style format string.
656
-
657
- ```typescript
658
- function format(
659
- formatString: string,
660
- args: {
661
- year?: number;
662
- month?: number;
663
- day?: number;
664
- hour?: number;
665
- minute?: number;
666
- second?: number;
667
- millisecond?: number;
668
- timezoneOffsetMinutes?: number;
669
- },
670
- ): string;
671
- ```
672
-
673
- Supported format patterns:
674
-
675
- | Pattern | Description | Example |
676
- |---------|-------------|---------|
677
- | `yyyy` | 4-digit year | 2024 |
678
- | `yy` | 2-digit year | 24 |
679
- | `MM` | Zero-padded month | 01-12 |
680
- | `M` | Month | 1-12 |
681
- | `ddd` | Day of week (Korean) | 일, 월, 화, 수, 목, 금, 토 |
682
- | `dd` | Zero-padded day | 01-31 |
683
- | `d` | Day | 1-31 |
684
- | `tt` | AM/PM | AM, PM |
685
- | `hh` | Zero-padded 12-hour | 01-12 |
686
- | `h` | 12-hour | 1-12 |
687
- | `HH` | Zero-padded 24-hour | 00-23 |
688
- | `H` | 24-hour | 0-23 |
689
- | `mm` | Zero-padded minute | 00-59 |
690
- | `m` | Minute | 0-59 |
691
- | `ss` | Zero-padded second | 00-59 |
692
- | `s` | Second | 0-59 |
693
- | `fff` | Millisecond (3 digits) | 000-999 |
694
- | `ff` | Millisecond (2 digits) | 00-99 |
695
- | `f` | Millisecond (1 digit) | 0-9 |
696
- | `zzz` | Timezone offset (HH:mm) | +09:00 |
697
- | `zz` | Timezone offset (HH) | +09 |
698
- | `z` | Timezone offset (H) | +9 |
699
-
700
- ---
701
-
702
- ## `primitive` -- Primitive Type Utilities
703
-
704
- ### `typeStr`
705
-
706
- Get the `PrimitiveTypeStr` for a runtime value.
707
-
708
- ```typescript
709
- function typeStr(value: PrimitiveTypeMap[PrimitiveTypeStr]): PrimitiveTypeStr;
710
- ```
711
-
712
- | Input Type | Returns |
713
- |------------|---------|
714
- | `string` | `"string"` |
715
- | `number` | `"number"` |
716
- | `boolean` | `"boolean"` |
717
- | `DateTime` | `"DateTime"` |
718
- | `DateOnly` | `"DateOnly"` |
719
- | `Time` | `"Time"` |
720
- | `Uuid` | `"Uuid"` |
721
- | `Uint8Array` | `"Bytes"` |
722
-
723
- Throws `ArgumentError` for unsupported types.