@simplysm/core-common 13.0.100 → 14.0.4

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.
Files changed (184) hide show
  1. package/README.md +86 -92
  2. package/dist/common.types.d.ts +14 -14
  3. package/dist/common.types.js +2 -1
  4. package/dist/common.types.js.map +1 -6
  5. package/dist/env.d.ts +8 -1
  6. package/dist/env.d.ts.map +1 -1
  7. package/dist/env.js +13 -9
  8. package/dist/env.js.map +1 -6
  9. package/dist/errors/argument-error.d.ts +10 -10
  10. package/dist/errors/argument-error.d.ts.map +1 -1
  11. package/dist/errors/argument-error.js +31 -14
  12. package/dist/errors/argument-error.js.map +1 -6
  13. package/dist/errors/not-implemented-error.d.ts +8 -8
  14. package/dist/errors/not-implemented-error.js +30 -12
  15. package/dist/errors/not-implemented-error.js.map +1 -6
  16. package/dist/errors/sd-error.d.ts +10 -10
  17. package/dist/errors/sd-error.d.ts.map +1 -1
  18. package/dist/errors/sd-error.js +45 -24
  19. package/dist/errors/sd-error.js.map +1 -6
  20. package/dist/errors/timeout-error.d.ts +10 -10
  21. package/dist/errors/timeout-error.js +34 -15
  22. package/dist/errors/timeout-error.js.map +1 -6
  23. package/dist/extensions/arr-ext.d.ts +2 -2
  24. package/dist/extensions/arr-ext.helpers.d.ts +10 -10
  25. package/dist/extensions/arr-ext.helpers.js +112 -89
  26. package/dist/extensions/arr-ext.helpers.js.map +1 -6
  27. package/dist/extensions/arr-ext.js +458 -422
  28. package/dist/extensions/arr-ext.js.map +1 -6
  29. package/dist/extensions/arr-ext.types.d.ts +57 -57
  30. package/dist/extensions/arr-ext.types.d.ts.map +1 -1
  31. package/dist/extensions/arr-ext.types.js +6 -1
  32. package/dist/extensions/arr-ext.types.js.map +1 -6
  33. package/dist/extensions/map-ext.d.ts +16 -16
  34. package/dist/extensions/map-ext.js +27 -22
  35. package/dist/extensions/map-ext.js.map +1 -6
  36. package/dist/extensions/set-ext.d.ts +11 -11
  37. package/dist/extensions/set-ext.js +32 -25
  38. package/dist/extensions/set-ext.js.map +1 -6
  39. package/dist/features/debounce-queue.d.ts +17 -17
  40. package/dist/features/debounce-queue.js +98 -70
  41. package/dist/features/debounce-queue.js.map +1 -6
  42. package/dist/features/event-emitter.d.ts +20 -20
  43. package/dist/features/event-emitter.js +101 -78
  44. package/dist/features/event-emitter.js.map +1 -6
  45. package/dist/features/serial-queue.d.ts +11 -11
  46. package/dist/features/serial-queue.js +78 -57
  47. package/dist/features/serial-queue.js.map +1 -6
  48. package/dist/globals.d.ts +4 -4
  49. package/dist/globals.js +9 -1
  50. package/dist/globals.js.map +1 -6
  51. package/dist/index.js +28 -27
  52. package/dist/index.js.map +1 -6
  53. package/dist/types/date-only.d.ts +64 -64
  54. package/dist/types/date-only.d.ts.map +1 -1
  55. package/dist/types/date-only.js +263 -252
  56. package/dist/types/date-only.js.map +1 -6
  57. package/dist/types/date-time.d.ts +36 -36
  58. package/dist/types/date-time.d.ts.map +1 -1
  59. package/dist/types/date-time.js +196 -288
  60. package/dist/types/date-time.js.map +1 -6
  61. package/dist/types/lazy-gc-map.d.ts +26 -26
  62. package/dist/types/lazy-gc-map.d.ts.map +1 -1
  63. package/dist/types/lazy-gc-map.js +202 -159
  64. package/dist/types/lazy-gc-map.js.map +1 -6
  65. package/dist/types/time.d.ts +23 -23
  66. package/dist/types/time.d.ts.map +1 -1
  67. package/dist/types/time.js +169 -158
  68. package/dist/types/time.js.map +1 -6
  69. package/dist/types/uuid.d.ts +11 -11
  70. package/dist/types/uuid.d.ts.map +1 -1
  71. package/dist/types/uuid.js +95 -70
  72. package/dist/types/uuid.js.map +1 -6
  73. package/dist/utils/bytes.d.ts +17 -17
  74. package/dist/utils/bytes.js +137 -81
  75. package/dist/utils/bytes.js.map +1 -6
  76. package/dist/utils/date-format.d.ts +40 -40
  77. package/dist/utils/date-format.js +187 -101
  78. package/dist/utils/date-format.js.map +1 -6
  79. package/dist/utils/error.d.ts +4 -4
  80. package/dist/utils/error.js +11 -6
  81. package/dist/utils/error.js.map +1 -6
  82. package/dist/utils/json.d.ts +19 -19
  83. package/dist/utils/json.js +187 -135
  84. package/dist/utils/json.js.map +1 -6
  85. package/dist/utils/num.d.ts +20 -20
  86. package/dist/utils/num.js +76 -34
  87. package/dist/utils/num.js.map +1 -6
  88. package/dist/utils/obj.d.ts +111 -111
  89. package/dist/utils/obj.d.ts.map +1 -1
  90. package/dist/utils/obj.js +706 -496
  91. package/dist/utils/obj.js.map +1 -6
  92. package/dist/utils/path.d.ts +10 -10
  93. package/dist/utils/path.js +35 -18
  94. package/dist/utils/path.js.map +1 -6
  95. package/dist/utils/primitive.d.ts +5 -5
  96. package/dist/utils/primitive.js +34 -14
  97. package/dist/utils/primitive.js.map +1 -6
  98. package/dist/utils/str.d.ts +38 -38
  99. package/dist/utils/str.js +217 -113
  100. package/dist/utils/str.js.map +1 -6
  101. package/dist/utils/template-strings.d.ts +26 -26
  102. package/dist/utils/template-strings.js +113 -40
  103. package/dist/utils/template-strings.js.map +1 -6
  104. package/dist/utils/transferable.d.ts +18 -18
  105. package/dist/utils/transferable.js +218 -151
  106. package/dist/utils/transferable.js.map +1 -6
  107. package/dist/utils/wait.d.ts +9 -9
  108. package/dist/utils/wait.js +30 -15
  109. package/dist/utils/wait.js.map +1 -6
  110. package/dist/utils/xml.d.ts +13 -13
  111. package/dist/utils/xml.js +84 -46
  112. package/dist/utils/xml.js.map +1 -6
  113. package/dist/utils/zip.d.ts +22 -22
  114. package/dist/utils/zip.js +172 -148
  115. package/dist/utils/zip.js.map +1 -6
  116. package/docs/array-extensions.md +430 -0
  117. package/docs/env.md +52 -0
  118. package/docs/errors.md +41 -56
  119. package/docs/features.md +82 -97
  120. package/docs/type-utilities.md +91 -0
  121. package/docs/types.md +221 -201
  122. package/docs/utils.md +319 -435
  123. package/package.json +7 -5
  124. package/src/common.types.ts +14 -14
  125. package/src/env.ts +12 -3
  126. package/src/errors/argument-error.ts +15 -15
  127. package/src/errors/not-implemented-error.ts +9 -9
  128. package/src/errors/sd-error.ts +12 -12
  129. package/src/errors/timeout-error.ts +12 -12
  130. package/src/extensions/arr-ext.helpers.ts +16 -16
  131. package/src/extensions/arr-ext.ts +35 -35
  132. package/src/extensions/arr-ext.types.ts +57 -57
  133. package/src/extensions/map-ext.ts +16 -16
  134. package/src/extensions/set-ext.ts +11 -11
  135. package/src/features/debounce-queue.ts +23 -23
  136. package/src/features/event-emitter.ts +25 -25
  137. package/src/features/serial-queue.ts +13 -13
  138. package/src/globals.ts +4 -4
  139. package/src/index.ts +5 -5
  140. package/src/types/date-only.ts +84 -83
  141. package/src/types/date-time.ts +43 -42
  142. package/src/types/lazy-gc-map.ts +44 -44
  143. package/src/types/time.ts +29 -29
  144. package/src/types/uuid.ts +15 -15
  145. package/src/utils/bytes.ts +35 -35
  146. package/src/utils/date-format.ts +59 -59
  147. package/src/utils/error.ts +4 -4
  148. package/src/utils/json.ts +41 -41
  149. package/src/utils/num.ts +20 -20
  150. package/src/utils/obj.ts +138 -138
  151. package/src/utils/path.ts +10 -10
  152. package/src/utils/primitive.ts +6 -6
  153. package/src/utils/str.ts +48 -48
  154. package/src/utils/template-strings.ts +29 -29
  155. package/src/utils/transferable.ts +38 -38
  156. package/src/utils/wait.ts +10 -10
  157. package/src/utils/xml.ts +19 -19
  158. package/src/utils/zip.ts +25 -25
  159. package/docs/extensions.md +0 -387
  160. package/tests/errors/errors.spec.ts +0 -80
  161. package/tests/extensions/array-extension.spec.ts +0 -654
  162. package/tests/extensions/map-extension.spec.ts +0 -117
  163. package/tests/extensions/set-extension.spec.ts +0 -67
  164. package/tests/types/date-only.spec.ts +0 -533
  165. package/tests/types/date-time.spec.ts +0 -246
  166. package/tests/types/lazy-gc-map.spec.ts +0 -606
  167. package/tests/types/time.spec.ts +0 -428
  168. package/tests/types/uuid.spec.ts +0 -74
  169. package/tests/utils/bytes-utils.spec.ts +0 -197
  170. package/tests/utils/date-format.spec.ts +0 -350
  171. package/tests/utils/debounce-queue.spec.ts +0 -226
  172. package/tests/utils/json.spec.ts +0 -400
  173. package/tests/utils/number.spec.ts +0 -136
  174. package/tests/utils/object.spec.ts +0 -810
  175. package/tests/utils/path.spec.ts +0 -70
  176. package/tests/utils/primitive.spec.ts +0 -43
  177. package/tests/utils/sd-event-emitter.spec.ts +0 -189
  178. package/tests/utils/serial-queue.spec.ts +0 -305
  179. package/tests/utils/string.spec.ts +0 -265
  180. package/tests/utils/template-strings.spec.ts +0 -48
  181. package/tests/utils/transferable.spec.ts +0 -639
  182. package/tests/utils/wait.spec.ts +0 -123
  183. package/tests/utils/xml.spec.ts +0 -146
  184. package/tests/utils/zip.spec.ts +0 -221
package/docs/features.md CHANGED
@@ -1,143 +1,128 @@
1
1
  # Features
2
2
 
3
- Feature classes for async control flow and event handling.
3
+ Async queue and event emitter utilities. All support `using` syntax via `Symbol.dispose`.
4
4
 
5
- Source: `src/features/*.ts`
6
-
7
- ---
8
-
9
- ## `EventEmitter`
5
+ ## `DebounceQueue`
10
6
 
11
- Type-safe event emitter that can be used in both browsers and Node.js. Internally implemented using `EventTarget`. Supports the `using` statement (`Symbol.dispose`).
7
+ Async debounce queue. When called multiple times in rapid succession, only the last enqueued function executes after the delay. Extends `EventEmitter<{ error: SdError }>`.
12
8
 
13
9
  ```typescript
14
- export class EventEmitter<
15
- TEvents extends { [K in keyof TEvents]: unknown } = Record<string, unknown>,
16
- > {
17
- /**
18
- * Register an event listener
19
- * @note Duplicate registration of the same listener to the same event is ignored
20
- */
21
- on<TEventName extends keyof TEvents & string>(
22
- type: TEventName,
23
- listener: (data: TEvents[TEventName]) => void,
24
- ): void;
25
-
26
- /** Remove an event listener */
27
- off<TEventName extends keyof TEvents & string>(
28
- type: TEventName,
29
- listener: (data: TEvents[TEventName]) => void,
30
- ): void;
31
-
32
- /**
33
- * Emit an event
34
- * @param args Event data (omitted if void type)
35
- */
36
- emit<TEventName extends keyof TEvents & string>(
37
- type: TEventName,
38
- ...args: TEvents[TEventName] extends void ? [] : [data: TEvents[TEventName]]
39
- ): void;
40
-
41
- /** Return the number of listeners for a specific event */
42
- listenerCount<TEventName extends keyof TEvents & string>(type: TEventName): number;
10
+ class DebounceQueue extends EventEmitter<{ error: SdError }> {
11
+ constructor(delay?: number);
43
12
 
44
- /** Remove all event listeners */
13
+ run(fn: () => void | Promise<void>): void;
45
14
  dispose(): void;
46
-
47
15
  [Symbol.dispose](): void;
48
16
  }
49
17
  ```
50
18
 
51
- **Example:**
19
+ ### Constructor
52
20
 
53
- ```typescript
54
- interface MyEvents {
55
- data: string;
56
- error: Error;
57
- done: void;
58
- }
21
+ | Parameter | Type | Default | Description |
22
+ |-----------|------|---------|-------------|
23
+ | `delay` | `number \| undefined` | `undefined` (next event loop) | Debounce delay in milliseconds |
59
24
 
60
- class MyEmitter extends EventEmitter<MyEvents> {}
25
+ ### Methods
61
26
 
62
- const emitter = new MyEmitter();
63
- emitter.on("data", (data) => console.log(data)); // data: string
64
- emitter.emit("data", "hello");
65
- emitter.emit("done"); // void type is called without arguments
66
- ```
27
+ | Method | Description |
28
+ |--------|-------------|
29
+ | `run(fn)` | Enqueue a function. Replaces any previously pending function. After the delay, executes the last enqueued function |
30
+ | `dispose()` | Cancels pending timer and clears state |
67
31
 
68
- ---
32
+ ### Behavior
69
33
 
70
- ## `DebounceQueue`
34
+ - If a new `run()` call arrives while a previous function is executing, the new function runs immediately after the current one completes (no debounce delay).
35
+ - Errors are emitted as `"error"` events if listeners exist; otherwise logged via consola.
71
36
 
72
- Asynchronous debounce queue. When called multiple times within a short time, only the last request is executed and previous requests are ignored. Extends `EventEmitter<{ error: SdError }>`. Supports the `using` statement.
37
+ ```typescript
38
+ const queue = new DebounceQueue(300);
39
+ queue.on("error", (err) => console.error(err));
73
40
 
74
- Requests added during execution are processed immediately after the current execution completes without debounce delay.
41
+ queue.run(() => console.log("1")); // cancelled
42
+ queue.run(() => console.log("2")); // cancelled
43
+ queue.run(() => console.log("3")); // executes after 300ms
44
+ ```
75
45
 
76
- ```typescript
77
- export class DebounceQueue extends EventEmitter<{ error: SdError }> {
78
- /**
79
- * @param _delay Debounce delay time (milliseconds). If omitted, executes immediately (next event loop)
80
- */
81
- constructor(delay?: number);
46
+ ## `SerialQueue`
82
47
 
83
- /** Clean up pending tasks and timers */
84
- dispose(): void;
48
+ Async serial queue. Functions are executed one at a time in the order they are enqueued. Errors in one function do not prevent subsequent functions from running. Extends `EventEmitter<{ error: SdError }>`.
85
49
 
86
- [Symbol.dispose](): void;
50
+ ```typescript
51
+ class SerialQueue extends EventEmitter<{ error: SdError }> {
52
+ constructor(gap?: number);
87
53
 
88
- /**
89
- * Add a function to the queue
90
- * If there was a previously added function, it will be replaced
91
- */
92
54
  run(fn: () => void | Promise<void>): void;
55
+ dispose(): void;
56
+ [Symbol.dispose](): void;
93
57
  }
94
58
  ```
95
59
 
96
- **Error handling:** If there are `"error"` event listeners, errors are emitted as events. Otherwise, errors are logged via `consola`.
60
+ ### Constructor
97
61
 
98
- **Example:**
62
+ | Parameter | Type | Default | Description |
63
+ |-----------|------|---------|-------------|
64
+ | `gap` | `number` | `0` | Delay between each task execution (ms) |
99
65
 
100
- ```typescript
101
- const queue = new DebounceQueue(300); // 300ms delay
102
- queue.run(() => console.log("1")); // ignored
103
- queue.run(() => console.log("2")); // ignored
104
- queue.run(() => console.log("3")); // executed after 300ms
66
+ ### Methods
67
+
68
+ | Method | Description |
69
+ |--------|-------------|
70
+ | `run(fn)` | Add a function to the queue. Starts processing if not already running |
71
+ | `dispose()` | Clears the pending queue. The currently running task still completes |
105
72
 
73
+ ```typescript
74
+ const queue = new SerialQueue();
106
75
  queue.on("error", (err) => console.error(err));
107
- ```
108
76
 
109
- ---
77
+ queue.run(async () => { await fetch("/api/1"); }); // runs first
78
+ queue.run(async () => { await fetch("/api/2"); }); // runs after 1 completes
79
+ queue.run(async () => { await fetch("/api/3"); }); // runs after 2 completes
80
+ ```
110
81
 
111
- ## `SerialQueue`
82
+ ## `EventEmitter<TEvents>`
112
83
 
113
- Asynchronous serial queue. Functions added to the queue are executed sequentially. The next task starts only after one task completes. Subsequent tasks continue to execute even if an error occurs. Extends `EventEmitter<{ error: SdError }>`. Supports the `using` statement.
84
+ Type-safe event emitter built on the `EventTarget` API. Works in both browser and Node.js.
114
85
 
115
86
  ```typescript
116
- export class SerialQueue extends EventEmitter<{ error: SdError }> {
117
- /**
118
- * @param gap Gap between each task (ms). Default: 0
119
- */
120
- constructor(gap?: number);
121
-
122
- /** Clear pending queue (currently executing task will complete) */
87
+ class EventEmitter<TEvents extends { [K in keyof TEvents]: unknown } = Record<string, unknown>> {
88
+ on<K extends keyof TEvents & string>(type: K, listener: (data: TEvents[K]) => void): void;
89
+ off<K extends keyof TEvents & string>(type: K, listener: (data: TEvents[K]) => void): void;
90
+ emit<K extends keyof TEvents & string>(
91
+ type: K,
92
+ ...args: TEvents[K] extends void ? [] : [data: TEvents[K]]
93
+ ): void;
94
+ listenerCount<K extends keyof TEvents & string>(type: K): number;
123
95
  dispose(): void;
124
-
125
96
  [Symbol.dispose](): void;
126
-
127
- /** Add a function to the queue and execute it */
128
- run(fn: () => void | Promise<void>): void;
129
97
  }
130
98
  ```
131
99
 
132
- **Error handling:** Same as `DebounceQueue` -- emitted as event if listeners exist, otherwise logged.
100
+ ### Methods
101
+
102
+ | Method | Description |
103
+ |--------|-------------|
104
+ | `on(type, listener)` | Register a listener. Duplicate registration for the same event+listener is ignored |
105
+ | `off(type, listener)` | Remove a listener |
106
+ | `emit(type, data?)` | Dispatch an event. For `void` event types, data argument is omitted |
107
+ | `listenerCount(type)` | Returns the number of registered listeners for an event type |
108
+ | `dispose()` | Removes all listeners from all event types |
133
109
 
134
- **Example:**
110
+ ### Type Parameter
111
+
112
+ `TEvents` is a map where keys are event names and values are event data types. Use `void` for events with no data.
135
113
 
136
114
  ```typescript
137
- const queue = new SerialQueue();
138
- queue.run(async () => { await fetch("/api/1"); });
139
- queue.run(async () => { await fetch("/api/2"); }); // executed after 1 completes
140
- queue.run(async () => { await fetch("/api/3"); }); // executed after 2 completes
115
+ interface MyEvents {
116
+ data: string;
117
+ error: Error;
118
+ done: void;
119
+ }
141
120
 
142
- queue.on("error", (err) => console.error(err));
121
+ class MyService extends EventEmitter<MyEvents> {}
122
+
123
+ const svc = new MyService();
124
+ svc.on("data", (data) => {}); // data: string
125
+ svc.on("done", () => {}); // no argument
126
+ svc.emit("data", "hello");
127
+ svc.emit("done"); // no argument needed
143
128
  ```
@@ -0,0 +1,91 @@
1
+ # Type Utilities
2
+
3
+ TypeScript type aliases and interfaces exported from `common.types.ts`.
4
+
5
+ ## `Bytes`
6
+
7
+ Type alias for `Uint8Array`. Used throughout the framework instead of `Buffer`.
8
+
9
+ ```typescript
10
+ type Bytes = Uint8Array;
11
+ ```
12
+
13
+ ## `PrimitiveTypeMap`
14
+
15
+ Maps primitive type string keys to their corresponding TypeScript types. Shared with `orm-common`.
16
+
17
+ ```typescript
18
+ type PrimitiveTypeMap = {
19
+ string: string;
20
+ number: number;
21
+ boolean: boolean;
22
+ DateTime: DateTime;
23
+ DateOnly: DateOnly;
24
+ Time: Time;
25
+ Uuid: Uuid;
26
+ Bytes: Bytes;
27
+ };
28
+ ```
29
+
30
+ ## `PrimitiveTypeStr`
31
+
32
+ Union of all keys in `PrimitiveTypeMap`.
33
+
34
+ ```typescript
35
+ type PrimitiveTypeStr = keyof PrimitiveTypeMap;
36
+ // "string" | "number" | "boolean" | "DateTime" | "DateOnly" | "Time" | "Uuid" | "Bytes"
37
+ ```
38
+
39
+ ## `PrimitiveType`
40
+
41
+ Union of all primitive type values (including `undefined`).
42
+
43
+ ```typescript
44
+ type PrimitiveType = PrimitiveTypeMap[PrimitiveTypeStr] | undefined;
45
+ // string | number | boolean | DateTime | DateOnly | Time | Uuid | Uint8Array | undefined
46
+ ```
47
+
48
+ ## `DeepPartial<TObject>`
49
+
50
+ Recursively makes all properties optional. Primitive types (`PrimitiveType`) are preserved as-is; only object/array types are recursively made partial.
51
+
52
+ ```typescript
53
+ type DeepPartial<TObject> = Partial<{
54
+ [K in keyof TObject]: TObject[K] extends PrimitiveType
55
+ ? TObject[K]
56
+ : DeepPartial<TObject[K]>;
57
+ }>;
58
+ ```
59
+
60
+ ```typescript
61
+ interface User {
62
+ name: string;
63
+ profile: {
64
+ age: number;
65
+ address: { city: string };
66
+ };
67
+ }
68
+
69
+ const partial: DeepPartial<User> = {
70
+ profile: { address: {} },
71
+ };
72
+ ```
73
+
74
+ ## `Type<TInstance>`
75
+
76
+ Constructor type interface. Represents a class constructor that creates instances of `TInstance`. Used for dependency injection, factory patterns, and `instanceof` checks.
77
+
78
+ ```typescript
79
+ interface Type<TInstance> extends Function {
80
+ new (...args: unknown[]): TInstance;
81
+ }
82
+ ```
83
+
84
+ ```typescript
85
+ function create<T>(ctor: Type<T>): T {
86
+ return new ctor();
87
+ }
88
+
89
+ class MyClass { name = "test"; }
90
+ const instance = create(MyClass); // MyClass instance
91
+ ```