@helpers4/map 3.0.2-alpha.0

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/llms.txt ADDED
@@ -0,0 +1,449 @@
1
+ # @helpers4/map
2
+
3
+ > Tree-shakable TypeScript utility functions for the `map` domain.
4
+ > Package: `@helpers4/map` — Version: 3.0.2
5
+ > License: LGPL-3.0-or-later
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @helpers4/map
11
+ # or
12
+ pnpm add @helpers4/map
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { countBy, every, filter, ... } from '@helpers4/map';
19
+ ```
20
+
21
+ ## Functions
22
+
23
+ | Function | Description |
24
+ |---|---|
25
+ | `countBy` | Groups the entries of a Map by a derived key and counts how many fall into each group. |
26
+ | `every` | Checks if every entry of a Map satisfies the predicate. Short-circuits on the first mismatch. |
27
+ | `filter` | Creates a new Map containing only the entries for which the predicate returns true. |
28
+ | `findKey` | Returns the first key of a Map whose entry satisfies the predicate, in insertion order. |
29
+ | `findValue` | Returns the first value of a Map whose entry satisfies the predicate, in insertion order. |
30
+ | `hasValue` | Checks whether a value exists anywhere in a Map (`Map.prototype.has` checks keys, not values). Uses |
31
+ | `mapKeys` | Creates a new Map with the same values but with each key transformed by a function. If two entries d |
32
+ | `mapValues` | Creates a new Map with the same keys but with each value transformed by a function. |
33
+ | `reduce` | Reduces a Map to a single value by applying a function to each entry, in insertion order. |
34
+ | `some` | Checks if at least one entry of a Map satisfies the predicate. Short-circuits on the first match. |
35
+ | `toMapByKey` | Builds a Map from an iterable of items, keyed by a derived key. When two items derive the same key, |
36
+
37
+ ---
38
+
39
+ ## API Reference
40
+
41
+ ### `countBy`
42
+
43
+ Groups the entries of a Map by a derived key and counts how many fall into each group.
44
+
45
+ ```typescript
46
+ import { countBy } from '@helpers4/map';
47
+
48
+ countBy<K, V, R>(map: ReadonlyMap<K, V>, fn: function): Map<R, number>
49
+ ```
50
+
51
+ **Parameters:**
52
+
53
+ - `map: ReadonlyMap<K, V>` — The Map to summarize
54
+ - `fn: function` — Function deriving the grouping key from (value, key)
55
+
56
+ **Returns:** `Map<R, number>` — A Map from derived key to the number of entries in that group
57
+
58
+ **Examples:**
59
+
60
+ *Count entries by parity*
61
+
62
+ Groups values by a derived key and counts how many fall into each group.
63
+
64
+ ```typescript
65
+ countBy(new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4]]), v => v % 2 === 0 ? 'even' : 'odd')
66
+ // => Map(2) { 'odd' => 2, 'even' => 2 }
67
+ ```
68
+
69
+ ---
70
+
71
+ ### `every`
72
+
73
+ Checks if every entry of a Map satisfies the predicate. Short-circuits on the first mismatch.
74
+
75
+ ```typescript
76
+ import { every } from '@helpers4/map';
77
+
78
+ every<K, V>(map: ReadonlyMap<K, V>, predicate: function): boolean
79
+ ```
80
+
81
+ **Parameters:**
82
+
83
+ - `map: ReadonlyMap<K, V>` — The Map to check
84
+ - `predicate: function` — Function invoked with (value, key)
85
+
86
+ **Returns:** `boolean` — `true` if all entries match (vacuously `true` for an empty map), `false` otherwise
87
+
88
+ **Examples:**
89
+
90
+ *Check that all values are positive*
91
+
92
+ Returns false as soon as one entry fails the predicate.
93
+
94
+ ```typescript
95
+ every(new Map([['a', 1], ['b', 2]]), value => value > 0)
96
+ // => true
97
+ ```
98
+
99
+ *Empty map*
100
+
101
+ Vacuously true for an empty map.
102
+
103
+ ```typescript
104
+ every(new Map(), () => false)
105
+ // => true
106
+ ```
107
+
108
+ ---
109
+
110
+ ### `filter`
111
+
112
+ Creates a new Map containing only the entries for which the predicate returns true.
113
+
114
+ ```typescript
115
+ import { filter } from '@helpers4/map';
116
+
117
+ filter<K, V>(map: ReadonlyMap<K, V>, predicate: function): Map<K, V>
118
+ ```
119
+
120
+ **Parameters:**
121
+
122
+ - `map: ReadonlyMap<K, V>` — The Map to filter
123
+ - `predicate: function` — Function invoked with (value, key) — return true to keep the entry
124
+
125
+ **Returns:** `Map<K, V>` — A new Map with only the matching entries
126
+
127
+ **Examples:**
128
+
129
+ *Keep only even values*
130
+
131
+ Creates a new Map with only the entries whose value satisfies the predicate.
132
+
133
+ ```typescript
134
+ filter(new Map([['a', 1], ['b', 2], ['c', 3]]), value => value % 2 === 0)
135
+ // => Map(1) { 'b' => 2 }
136
+ ```
137
+
138
+ *No matches*
139
+
140
+ Returns an empty Map when nothing matches.
141
+
142
+ ```typescript
143
+ filter(new Map([['a', 1]]), () => false)
144
+ // => Map(0) {}
145
+ ```
146
+
147
+ ---
148
+
149
+ ### `findKey`
150
+
151
+ Returns the first key of a Map whose entry satisfies the predicate, in insertion order.
152
+
153
+ ```typescript
154
+ import { findKey } from '@helpers4/map';
155
+
156
+ findKey<K, V>(map: ReadonlyMap<K, V>, predicate: function): K | undefined
157
+ ```
158
+
159
+ **Parameters:**
160
+
161
+ - `map: ReadonlyMap<K, V>` — The Map to search
162
+ - `predicate: function` — Function invoked with (value, key)
163
+
164
+ **Returns:** `K | undefined` — The matching key, or `undefined` if none matches
165
+
166
+ **Examples:**
167
+
168
+ *Find the key of the first matching entry*
169
+
170
+ Searches in insertion order and returns the key of the first match.
171
+
172
+ ```typescript
173
+ findKey(new Map([['a', 1], ['b', 2]]), value => value > 1)
174
+ // => 'b'
175
+ ```
176
+
177
+ *No match*
178
+
179
+ Returns undefined when nothing satisfies the predicate.
180
+
181
+ ```typescript
182
+ findKey(new Map([['a', 1]]), value => value > 10)
183
+ // => undefined
184
+ ```
185
+
186
+ ---
187
+
188
+ ### `findValue`
189
+
190
+ Returns the first value of a Map whose entry satisfies the predicate, in insertion order.
191
+
192
+ ```typescript
193
+ import { findValue } from '@helpers4/map';
194
+
195
+ findValue<K, V>(map: ReadonlyMap<K, V>, predicate: function): V | undefined
196
+ ```
197
+
198
+ **Parameters:**
199
+
200
+ - `map: ReadonlyMap<K, V>` — The Map to search
201
+ - `predicate: function` — Function invoked with (value, key)
202
+
203
+ **Returns:** `V | undefined` — The matching value, or `undefined` if none matches
204
+
205
+ **Examples:**
206
+
207
+ *Find the first matching value*
208
+
209
+ Searches in insertion order and returns the value of the first match.
210
+
211
+ ```typescript
212
+ findValue(new Map([['a', 1], ['b', 2]]), value => value > 1)
213
+ // => 2
214
+ ```
215
+
216
+ *No match*
217
+
218
+ Returns undefined when nothing satisfies the predicate.
219
+
220
+ ```typescript
221
+ findValue(new Map([['a', 1]]), value => value > 10)
222
+ // => undefined
223
+ ```
224
+
225
+ ---
226
+
227
+ ### `hasValue`
228
+
229
+ Checks whether a value exists anywhere in a Map (`Map.prototype.has` checks keys, not values).
230
+ Uses `Object.is`-style equality (via `===`, with `NaN` matching `NaN`).
231
+
232
+ ```typescript
233
+ import { hasValue } from '@helpers4/map';
234
+
235
+ hasValue<K, V>(map: ReadonlyMap<K, V>, value: V): boolean
236
+ ```
237
+
238
+ **Parameters:**
239
+
240
+ - `map: ReadonlyMap<K, V>` — The Map to search
241
+ - `value: V` — The value to look for
242
+
243
+ **Returns:** `boolean` — `true` if the value is found in at least one entry
244
+
245
+ **Examples:**
246
+
247
+ *Check whether a value exists in a Map*
248
+
249
+ Unlike Map.prototype.has (which checks keys), this checks values.
250
+
251
+ ```typescript
252
+ hasValue(new Map([['a', 1], ['b', 2]]), 2)
253
+ // => true
254
+ ```
255
+
256
+ *Value absent*
257
+
258
+ Returns false when no entry has that value.
259
+
260
+ ```typescript
261
+ hasValue(new Map([['a', 1]]), 99)
262
+ // => false
263
+ ```
264
+
265
+ ---
266
+
267
+ ### `mapKeys`
268
+
269
+ Creates a new Map with the same values but with each key transformed by a function.
270
+ If two entries derive the same new key, the later one (in insertion order) wins.
271
+
272
+ ```typescript
273
+ import { mapKeys } from '@helpers4/map';
274
+
275
+ mapKeys<K, V, R>(map: ReadonlyMap<K, V>, fn: function): Map<R, V>
276
+ ```
277
+
278
+ **Parameters:**
279
+
280
+ - `map: ReadonlyMap<K, V>` — The Map to transform
281
+ - `fn: function` — Function invoked with (key, value) that returns the new key
282
+
283
+ **Returns:** `Map<R, V>` — A new Map with transformed keys
284
+
285
+ **Examples:**
286
+
287
+ *Uppercase every key*
288
+
289
+ Creates a new Map with transformed keys and unchanged values.
290
+
291
+ ```typescript
292
+ mapKeys(new Map([['a', 1], ['b', 2]]), key => key.toUpperCase())
293
+ // => Map(2) { 'A' => 1, 'B' => 2 }
294
+ ```
295
+
296
+ ---
297
+
298
+ ### `mapValues`
299
+
300
+ Creates a new Map with the same keys but with each value transformed by a function.
301
+
302
+ ```typescript
303
+ import { mapValues } from '@helpers4/map';
304
+
305
+ mapValues<K, V, R>(map: ReadonlyMap<K, V>, fn: function): Map<K, R>
306
+ ```
307
+
308
+ **Parameters:**
309
+
310
+ - `map: ReadonlyMap<K, V>` — The Map to transform
311
+ - `fn: function` — Function invoked with (value, key) that returns the new value
312
+
313
+ **Returns:** `Map<K, R>` — A new Map with transformed values
314
+
315
+ **Examples:**
316
+
317
+ *Scale every value*
318
+
319
+ Creates a new Map with transformed values and unchanged keys.
320
+
321
+ ```typescript
322
+ mapValues(new Map([['a', 1], ['b', 2]]), value => value * 10)
323
+ // => Map(2) { 'a' => 10, 'b' => 20 }
324
+ ```
325
+
326
+ ---
327
+
328
+ ### `reduce`
329
+
330
+ Reduces a Map to a single value by applying a function to each entry, in insertion order.
331
+
332
+ ```typescript
333
+ import { reduce } from '@helpers4/map';
334
+
335
+ reduce<K, V, R>(map: ReadonlyMap<K, V>, fn: function, initial: R): R
336
+ ```
337
+
338
+ **Parameters:**
339
+
340
+ - `map: ReadonlyMap<K, V>` — The Map to reduce
341
+ - `fn: function` — Function invoked with (accumulator, value, key)
342
+ - `initial: R` — Initial accumulator value
343
+
344
+ **Returns:** `R` — The final accumulator value
345
+
346
+ **Examples:**
347
+
348
+ *Sum all values*
349
+
350
+ Reduces a Map of numbers to their total.
351
+
352
+ ```typescript
353
+ reduce(new Map([['a', 1], ['b', 2], ['c', 3]]), (acc, value) => acc + value, 0)
354
+ // => 6
355
+ ```
356
+
357
+ *Build an array of keys*
358
+
359
+ Reduce can produce a completely different shape than the map values.
360
+
361
+ ```typescript
362
+ reduce(new Map([['a', 1], ['b', 2]]), (acc: string[], _v, key) => [...acc, key], [])
363
+ // => ['a', 'b']
364
+ ```
365
+
366
+ ---
367
+
368
+ ### `some`
369
+
370
+ Checks if at least one entry of a Map satisfies the predicate. Short-circuits on the first match.
371
+
372
+ ```typescript
373
+ import { some } from '@helpers4/map';
374
+
375
+ some<K, V>(map: ReadonlyMap<K, V>, predicate: function): boolean
376
+ ```
377
+
378
+ **Parameters:**
379
+
380
+ - `map: ReadonlyMap<K, V>` — The Map to check
381
+ - `predicate: function` — Function invoked with (value, key)
382
+
383
+ **Returns:** `boolean` — `true` if at least one entry matches, `false` otherwise (including for an empty map)
384
+
385
+ **Examples:**
386
+
387
+ *Check if any value exceeds a threshold*
388
+
389
+ Returns true as soon as one entry satisfies the predicate.
390
+
391
+ ```typescript
392
+ some(new Map([['a', 1], ['b', 2]]), value => value > 1)
393
+ // => true
394
+ ```
395
+
396
+ *Empty map*
397
+
398
+ Always returns false for an empty map.
399
+
400
+ ```typescript
401
+ some(new Map(), () => true)
402
+ // => false
403
+ ```
404
+
405
+ ---
406
+
407
+ ### `toMapByKey`
408
+
409
+ Builds a Map from an iterable of items, keyed by a derived key. When two items derive the
410
+ same key, the later item wins (last-write-wins, matching `Map.prototype.set` semantics).
411
+
412
+ Named `toMapByKey`, not `keyBy` (lodash's name for the same idea) — lodash's `_.keyBy`
413
+ returns a plain object, not a `Map`; the `to<Type>` prefix (matching `toSorted`/`toReversed`)
414
+ makes the actual return type unambiguous.
415
+
416
+ ```typescript
417
+ import { toMapByKey } from '@helpers4/map';
418
+
419
+ toMapByKey<T, K>(items: Iterable<T>, fn: function): Map<K, T>
420
+ ```
421
+
422
+ **Parameters:**
423
+
424
+ - `items: Iterable<T>` — The items to index
425
+ - `fn: function` — Function deriving the key for each item
426
+
427
+ **Returns:** `Map<K, T>` — A Map from derived key to item
428
+
429
+ **Examples:**
430
+
431
+ *Index an array of records by id*
432
+
433
+ Builds a Map keyed by a derived value, for O(1) lookup by that key.
434
+
435
+ ```typescript
436
+ toMapByKey([{ id: 'a', n: 1 }, { id: 'b', n: 2 }], item => item.id)
437
+ // => Map(2) { 'a' => {...}, 'b' => {...} }
438
+ ```
439
+
440
+ *Last item wins on collision*
441
+
442
+ When two items derive the same key, the later one overwrites the earlier one.
443
+
444
+ ```typescript
445
+ toMapByKey([{ id: 'a', n: 1 }, { id: 'a', n: 2 }], item => item.id).get('a')
446
+ // => { id: 'a', n: 2 }
447
+ ```
448
+
449
+ ---