@simplysm/core-common 13.0.96 → 13.0.98
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 +117 -70
- package/dist/env.d.ts +5 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +6 -3
- package/dist/env.js.map +1 -1
- package/docs/errors.md +119 -0
- package/docs/extensions.md +387 -0
- package/docs/features.md +95 -295
- package/docs/types.md +202 -256
- package/docs/utils.md +708 -0
- package/package.json +2 -2
- package/src/env.ts +13 -3
- package/docs/utilities.md +0 -359
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
# Prototype Extensions (side-effect)
|
|
2
|
+
|
|
3
|
+
Prototype extensions for `Array`, `Map`, and `Set`. These are applied as side effects when importing `@simplysm/core-common`.
|
|
4
|
+
|
|
5
|
+
**Important:** Importing the package entry point automatically patches these prototypes. If you import only specific sub-modules, you must also import the extension modules or the entry point to get these methods.
|
|
6
|
+
|
|
7
|
+
Source: `src/extensions/*.ts`
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Array Extensions
|
|
12
|
+
|
|
13
|
+
### Readonly methods (return new array or value, do not mutate)
|
|
14
|
+
|
|
15
|
+
#### `single`
|
|
16
|
+
|
|
17
|
+
Return single element matching condition. Throws `ArgumentError` if 2+ elements match.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
single(predicate?: (item: T, index: number) => boolean): T | undefined;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
#### `first`
|
|
24
|
+
|
|
25
|
+
Return first element.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
first(predicate?: (item: T, index: number) => boolean): T | undefined;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### `last`
|
|
32
|
+
|
|
33
|
+
Return last element.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
last(predicate?: (item: T, index: number) => boolean): T | undefined;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### `filterAsync`
|
|
40
|
+
|
|
41
|
+
Async filter (sequential execution).
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
filterAsync(predicate: (item: T, index: number) => Promise<boolean>): Promise<T[]>;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### `filterExists`
|
|
48
|
+
|
|
49
|
+
Remove `null` and `undefined` values.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
filterExists(): NonNullable<T>[];
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### `ofType`
|
|
56
|
+
|
|
57
|
+
Filter only elements of specific type (`PrimitiveTypeStr` or constructor type).
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
ofType<TKey extends PrimitiveTypeStr>(type: TKey): Extract<T, PrimitiveTypeMap[TKey]>[];
|
|
61
|
+
ofType<TNarrow extends T>(type: Type<TNarrow>): TNarrow[];
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
#### `mapAsync`
|
|
65
|
+
|
|
66
|
+
Async mapping (sequential execution).
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
mapAsync<R>(selector: (item: T, index: number) => Promise<R>): Promise<R[]>;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### `mapMany`
|
|
73
|
+
|
|
74
|
+
Flatten nested array, or map then flatten.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
mapMany(): T extends readonly (infer U)[] ? U[] : T;
|
|
78
|
+
mapMany<R>(selector: (item: T, index: number) => R[]): R[];
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### `mapManyAsync`
|
|
82
|
+
|
|
83
|
+
Async mapping and then flatten (sequential execution).
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
mapManyAsync<R>(selector: (item: T, index: number) => Promise<R[]>): Promise<R[]>;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### `parallelAsync`
|
|
90
|
+
|
|
91
|
+
Async parallel processing using `Promise.all`.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
parallelAsync<R>(fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### `groupBy`
|
|
98
|
+
|
|
99
|
+
Group by key. O(n) for primitive keys, O(n^2) for object keys (deep comparison).
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
groupBy<K>(keySelector: (item: T, index: number) => K): { key: K; values: T[] }[];
|
|
103
|
+
groupBy<K, V>(
|
|
104
|
+
keySelector: (item: T, index: number) => K,
|
|
105
|
+
valueSelector: (item: T, index: number) => V,
|
|
106
|
+
): { key: K; values: V[] }[];
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### `toMap`
|
|
110
|
+
|
|
111
|
+
Convert to `Map`. Throws `ArgumentError` on duplicate keys.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
toMap<K>(keySelector: (item: T, index: number) => K): Map<K, T>;
|
|
115
|
+
toMap<K, V>(keySelector: (item: T, index: number) => K, valueSelector: (item: T, index: number) => V): Map<K, V>;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `toMapAsync`
|
|
119
|
+
|
|
120
|
+
Async version of `toMap`.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
toMapAsync<K>(keySelector: (item: T, index: number) => Promise<K>): Promise<Map<K, T>>;
|
|
124
|
+
toMapAsync<K, V>(
|
|
125
|
+
keySelector: (item: T, index: number) => Promise<K> | K,
|
|
126
|
+
valueSelector: (item: T, index: number) => Promise<V> | V,
|
|
127
|
+
): Promise<Map<K, V>>;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### `toArrayMap`
|
|
131
|
+
|
|
132
|
+
Convert to `Map<K, T[]>` (groups values by key).
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
toArrayMap<K>(keySelector: (item: T, index: number) => K): Map<K, T[]>;
|
|
136
|
+
toArrayMap<K, V>(keySelector: (item: T, index: number) => K, valueSelector: (item: T, index: number) => V): Map<K, V[]>;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### `toSetMap`
|
|
140
|
+
|
|
141
|
+
Convert to `Map<K, Set<T>>`.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
toSetMap<K>(keySelector: (item: T, index: number) => K): Map<K, Set<T>>;
|
|
145
|
+
toSetMap<K, V>(keySelector: (item: T, index: number) => K, valueSelector: (item: T, index: number) => V): Map<K, Set<V>>;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### `toMapValues`
|
|
149
|
+
|
|
150
|
+
Group by key, then reduce each group's values.
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
toMapValues<K, V>(
|
|
154
|
+
keySelector: (item: T, index: number) => K,
|
|
155
|
+
valueSelector: (items: T[]) => V,
|
|
156
|
+
): Map<K, V>;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### `toObject`
|
|
160
|
+
|
|
161
|
+
Convert to plain object. Throws `ArgumentError` on duplicate keys.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
toObject(keySelector: (item: T, index: number) => string): Record<string, T>;
|
|
165
|
+
toObject<V>(keySelector: (item: T, index: number) => string, valueSelector: (item: T, index: number) => V): Record<string, V>;
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### `toTree`
|
|
169
|
+
|
|
170
|
+
Convert flat array to tree structure. Items with `null`/`undefined` parent key become roots. Uses `toArrayMap` for O(n) complexity.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
toTree<K extends keyof T, P extends keyof T>(keyProp: K, parentKey: P): TreeArray<T>[];
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### `distinct`
|
|
177
|
+
|
|
178
|
+
Remove duplicates. Options: `matchAddress` for reference comparison, `keyFn` for custom key (O(n)). Without `keyFn` on objects: O(n^2).
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
distinct(options?: boolean | { matchAddress?: boolean; keyFn?: (item: T) => string | number }): T[];
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `orderBy` / `orderByDesc`
|
|
185
|
+
|
|
186
|
+
Sort in ascending or descending order. Supports `string`, `number`, `DateTime`, `DateOnly`, `Time`.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
orderBy(selector?: (item: T) => string | number | DateTime | DateOnly | Time | undefined): T[];
|
|
190
|
+
orderByDesc(selector?: (item: T) => string | number | DateTime | DateOnly | Time | undefined): T[];
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### `diffs`
|
|
194
|
+
|
|
195
|
+
Compare two arrays and return INSERT / DELETE / UPDATE results.
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
diffs<P>(target: P[]): ArrayDiffsResult<T, P>[];
|
|
199
|
+
diffs<P>(target: P[], options: { keys: string[]; excludes?: string[] }): ArrayDiffsResult<T, P>[];
|
|
200
|
+
diffs<P>(target: P[], options: { excludes: string[] }): ArrayDiffsResult<T, P>[];
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### `oneWayDiffs`
|
|
204
|
+
|
|
205
|
+
One-way diff against original items. Returns `"create"`, `"update"`, or `"same"` for each item.
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
oneWayDiffs<K extends keyof T>(
|
|
209
|
+
orgItems: T[] | Map<T[K], T>,
|
|
210
|
+
keyPropNameOrGetValFn: K | ((item: T) => string | number | undefined),
|
|
211
|
+
options?: { includeSame?: boolean; excludes?: string[]; includes?: string[] },
|
|
212
|
+
): ArrayOneWayDiffResult<T>[];
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### `merge`
|
|
216
|
+
|
|
217
|
+
Merge source and target arrays based on diff results.
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
merge<P>(target: P[]): (T | P | (T & P))[];
|
|
221
|
+
merge<P>(target: P[], options: { keys: string[]; excludes?: string[] }): (T | P | (T & P))[];
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### `sum`
|
|
225
|
+
|
|
226
|
+
Return sum of elements. Returns `0` for empty arrays.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
sum(selector?: (item: T, index: number) => number): number;
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `min` / `max`
|
|
233
|
+
|
|
234
|
+
Return minimum or maximum value.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
min(): T extends number | string ? T | undefined : never;
|
|
238
|
+
min<P extends number | string>(selector?: (item: T, index: number) => P): P | undefined;
|
|
239
|
+
max(): T extends number | string ? T | undefined : never;
|
|
240
|
+
max<P extends number | string>(selector?: (item: T, index: number) => P): P | undefined;
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `shuffle`
|
|
244
|
+
|
|
245
|
+
Return a shuffled copy (Fisher-Yates algorithm).
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
shuffle(): T[];
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
### Mutable methods (modify original array, marked `@mutates`)
|
|
254
|
+
|
|
255
|
+
#### `distinctThis`
|
|
256
|
+
|
|
257
|
+
Remove duplicates from original array.
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
distinctThis(options?: boolean | { matchAddress?: boolean; keyFn?: (item: T) => string | number }): T[];
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### `orderByThis` / `orderByDescThis`
|
|
264
|
+
|
|
265
|
+
Sort original array in ascending or descending order.
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
orderByThis(selector?: (item: T) => string | number | DateTime | DateOnly | Time | undefined): T[];
|
|
269
|
+
orderByDescThis(selector?: (item: T) => string | number | DateTime | DateOnly | Time | undefined): T[];
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
#### `insert`
|
|
273
|
+
|
|
274
|
+
Insert items at index.
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
insert(index: number, ...items: T[]): this;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
#### `remove`
|
|
281
|
+
|
|
282
|
+
Remove item or items matching condition.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
remove(item: T): this;
|
|
286
|
+
remove(selector: (item: T, index: number) => boolean): this;
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
#### `toggle`
|
|
290
|
+
|
|
291
|
+
Toggle item in array (remove if exists, add if not).
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
toggle(item: T): this;
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
#### `clear`
|
|
298
|
+
|
|
299
|
+
Clear all items from array.
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
clear(): this;
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## Exported Types
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
export type ArrayDiffsResult<TOriginal, TOther> =
|
|
311
|
+
| { source: undefined; target: TOther } // INSERT
|
|
312
|
+
| { source: TOriginal; target: undefined } // DELETE
|
|
313
|
+
| { source: TOriginal; target: TOther }; // UPDATE
|
|
314
|
+
|
|
315
|
+
export type ArrayOneWayDiffResult<TItem> =
|
|
316
|
+
| { type: "create"; item: TItem; orgItem: undefined }
|
|
317
|
+
| { type: "update"; item: TItem; orgItem: TItem }
|
|
318
|
+
| { type: "same"; item: TItem; orgItem: TItem };
|
|
319
|
+
|
|
320
|
+
export type TreeArray<TNode> = TNode & { children: TreeArray<TNode>[] };
|
|
321
|
+
|
|
322
|
+
/** Type that can be sorted/compared */
|
|
323
|
+
export type ComparableType = string | number | boolean | DateTime | DateOnly | Time | undefined;
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
## Map Extensions
|
|
329
|
+
|
|
330
|
+
#### `getOrCreate`
|
|
331
|
+
|
|
332
|
+
If no value exists for key, set new value and return it. If the second argument is a function, it is called as a factory.
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
getOrCreate(key: K, newValue: V): V;
|
|
336
|
+
getOrCreate(key: K, newValueFn: () => V): V;
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
**Caution:** If `V` is a function type, passing the function directly will be recognized as a factory and called. Wrap it in a factory to store the function itself.
|
|
340
|
+
|
|
341
|
+
#### `update`
|
|
342
|
+
|
|
343
|
+
Update value for key using function. Called even if key does not exist.
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
update(key: K, updateFn: (v: V | undefined) => V): void;
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
**Example:**
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
const countMap = new Map<string, number>();
|
|
353
|
+
countMap.update("key", (v) => (v ?? 0) + 1);
|
|
354
|
+
|
|
355
|
+
map.getOrCreate("users", []).push(newUser);
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## Set Extensions
|
|
361
|
+
|
|
362
|
+
#### `adds`
|
|
363
|
+
|
|
364
|
+
Add multiple values at once.
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
adds(...values: T[]): this;
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
#### `toggle`
|
|
371
|
+
|
|
372
|
+
Toggle value (remove if exists, add if not). Optional `addOrDel` parameter to force add or remove.
|
|
373
|
+
|
|
374
|
+
```typescript
|
|
375
|
+
toggle(value: T, addOrDel?: "add" | "del"): this;
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
**Example:**
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
const set = new Set<number>([1, 2, 3]);
|
|
382
|
+
set.toggle(2); // 2 exists, so remove -> {1, 3}
|
|
383
|
+
set.toggle(4); // 4 doesn't exist, so add -> {1, 3, 4}
|
|
384
|
+
|
|
385
|
+
const isAdmin = true;
|
|
386
|
+
set.toggle(5, isAdmin ? "add" : "del"); // Force add
|
|
387
|
+
```
|