@bemoje/iter 1.0.1 → 1.1.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.
Files changed (3) hide show
  1. package/index.d.ts +474 -59
  2. package/index.mjs +219 -151
  3. package/package.json +8 -2
package/index.d.ts CHANGED
@@ -1,43 +1,170 @@
1
1
  /**
2
- * Filter a `Map` (or `ReadonlyMap`). Predicate receives `(value, key)`. Returns a new `Map`.
2
+ * Filter an array based on a predicate function.
3
+ *
4
+ * @param array - The array to filter
5
+ * @param predicate - A function that tests each element. Returns `true` to keep the element, `false` otherwise.
6
+ * @returns A new array containing only the elements that satisfy the predicate
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const numbers = [1, 2, 3, 4, 5]
11
+ * const evens = filterArray(numbers, (n) => n % 2 === 0)
12
+ * // evens: [2, 4]
13
+ * ```
3
14
  */
4
- declare function filter<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V, key: K) => boolean): Map<K, V>;
5
- /** Filter a `Set` (or `ReadonlySet`). Predicate receives `(value)`. Returns a new `Set`. */
6
- declare function filter<V>(set: ReadonlySet<V>, predicate: (value: V) => boolean): Set<V>;
7
- /** Filter an array. Predicate receives `(value, index)`. Returns a new array. */
8
- declare function filter<V>(array: readonly V[], predicate: (value: V, index: number) => boolean): V[];
9
- /** Filter a plain object. Predicate receives `(value, key)`. Returns a new partial object. */
10
- declare function filter<K extends string, V>(object: Record<K, V>, predicate: (value: V, key: K) => boolean): Partial<Record<K, V>>;
11
- /** Filter any `Iterable`. Predicate receives `(value)`. Returns a new array. */
12
- declare function filter<V>(iterable: Iterable<V>, predicate: (value: V) => boolean): V[];
15
+ declare function filterArray<V, S extends V>(array: readonly V[], predicate: (value: V, index: number) => value is S): S[];
16
+ declare function filterArray<V>(array: readonly V[], predicate: (value: V, index: number) => boolean): V[];
13
17
 
14
18
  /**
15
- * Transform values of an iterable.
19
+ * Filter an iterable based on a predicate function.
20
+ *
21
+ * @param collection - The iterable to filter
22
+ * @param predicate - A function that tests each element. Returns `true` to keep the element, `false` otherwise.
23
+ * @returns An iterable containing only the elements that satisfy the predicate
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const numbers = [1, 2, 3, 4, 5]
28
+ * const evens = filterIterable(numbers, (n) => n % 2 === 0)
29
+ * // evens: Iterable of [2, 4]
30
+ * ```
16
31
  */
17
- declare function filterIterable<T>(set: Iterable<T>, predicate: (value: T) => boolean): Iterable<T>;
32
+ declare function filterIterable<T, S extends T>(collection: Iterable<T>, predicate: (value: T) => value is S): Iterable<S>;
33
+ declare function filterIterable<T>(collection: Iterable<T>, predicate: (value: T) => boolean): Iterable<T>;
18
34
 
19
35
  /**
20
- * Filter map entries based on a predicate function.
36
+ * Filter an iterable of key-value entries based on a predicate function.
37
+ *
38
+ * @param mapLike - An iterable of key-value tuples to filter
39
+ * @param predicate - A function that tests each entry. Returns `true` to keep the entry, `false` otherwise.
40
+ * @returns An iterable containing only the entries that satisfy the predicate
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
45
+ * const filtered = filterIterableEntries(entries, ([key, value]) => value > 1)
46
+ * // filtered: Iterable of [['b', 2], ['c', 3]]
47
+ * ```
21
48
  */
22
- declare function filterIterableEntries<K, V>(mapLike: Iterable<[K, V]>, predicate: (value: V, key: K) => boolean): Iterable<[K, V]>;
49
+ declare function filterIterableEntries<K, V, K2 extends K, V2 extends V>(mapLike: Iterable<[K, V]>, predicate: (entry: [K, V]) => entry is [K2, V2]): Iterable<[K2, V2]>;
50
+ declare function filterIterableEntries<K, V>(mapLike: Iterable<[K, V]>, predicate: (entry: [K, V]) => boolean): Iterable<[K, V]>;
23
51
 
24
52
  /**
25
- * Iterate a `Map` (or `ReadonlyMap`). Callback receives `(value, key)`.
53
+ * Filter an iterable of key-value entries based on a predicate function that tests keys.
54
+ *
55
+ * @param mapLike - An iterable of key-value tuples to filter
56
+ * @param predicate - A function that tests each key-value pair. Returns `true` to keep the entry, `false` otherwise.
57
+ * @returns An iterable containing only the entries whose keys satisfy the predicate
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const entries: [string, number][] = [['apple', 1], ['banana', 2], ['apricot', 3]]
62
+ * const filtered = filterIterableKeys(entries, (key) => key.startsWith('a'))
63
+ * // filtered: Iterable of [['apple', 1], ['apricot', 3]]
64
+ * ```
26
65
  */
27
- declare function forEach<K, V>(map: ReadonlyMap<K, V>, callback: (value: V, key: K) => void): void;
28
- /** Iterate a `Set` (or `ReadonlySet`). Callback receives `(value)`. */
29
- declare function forEach<V>(set: ReadonlySet<V>, callback: (value: V) => void): void;
30
- /** Iterate an array. Callback receives `(value, index)`. */
31
- declare function forEach<V>(array: readonly V[], callback: (value: V, index: number) => void): void;
32
- /** Iterate a plain object. Callback receives `(value, key)`. */
33
- declare function forEach<K extends string, V>(object: Record<K, V>, callback: (value: V, key: K) => void): void;
34
- /** Iterate any `Iterable`. Callback receives `(value)`. */
35
- declare function forEach<V>(iterable: Iterable<V>, callback: (value: V) => void): void;
66
+ declare function filterIterableKeys<K, V, K2 extends K>(mapLike: Iterable<[K, V]>, predicate: (key: K, value: V) => key is K2): Iterable<[K2, V]>;
67
+ declare function filterIterableKeys<K, V>(mapLike: Iterable<[K, V]>, predicate: (key: K, value: V) => boolean): Iterable<[K, V]>;
36
68
 
37
69
  /**
38
- * Iterate over values of an iterable, executing a callback for each.
70
+ * Filter an iterable of key-value entries based on a predicate function that tests values.
71
+ *
72
+ * @param mapLike - An iterable of key-value tuples to filter
73
+ * @param predicate - A function that tests each value. Returns `true` to keep the entry, `false` otherwise.
74
+ * @returns An iterable containing only the entries whose values satisfy the predicate
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
79
+ * const filtered = filterIterableValues(entries, (value) => value > 1)
80
+ * // filtered: Iterable of [['b', 2], ['c', 3]]
81
+ * ```
39
82
  */
40
- declare function forEachIterable<T>(set: Iterable<T>, callback: (value: T) => unknown): void;
83
+ declare function filterIterableValues<K, V, V2 extends V>(mapLike: Iterable<[K, V]>, predicate: (value: V, key: K) => value is V2): Iterable<[K, V2]>;
84
+ declare function filterIterableValues<K, V>(mapLike: Iterable<[K, V]>, predicate: (value: V, key: K) => boolean): Iterable<[K, V]>;
85
+
86
+ /**
87
+ * Filter a Map based on a predicate function.
88
+ *
89
+ * @param map - The Map to filter
90
+ * @param predicate - A function that tests each value-key pair. Returns `true` to keep the entry, `false` otherwise.
91
+ * @returns A new Map containing only the entries that satisfy the predicate
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const map = new Map([['a', 1], ['b', 2], ['c', 3]])
96
+ * const filtered = filterMap(map, (value) => value > 1)
97
+ * // filtered: Map { 'b' => 2, 'c' => 3 }
98
+ * ```
99
+ */
100
+ declare function filterMap<K, V, V2 extends V>(map: ReadonlyMap<K, V>, predicate: (value: V, key: K) => value is V2): Map<K, V2>;
101
+ declare function filterMap<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V, key: K) => boolean): Map<K, V>;
102
+
103
+ /**
104
+ * Filter an object's properties based on a predicate function.
105
+ *
106
+ * @param object - The object to filter
107
+ * @param predicate - A function that tests each value-key pair. Returns `true` to keep the property, `false` otherwise.
108
+ * @returns A new object containing only the properties that satisfy the predicate
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * const obj = { a: 1, b: 2, c: 3 }
113
+ * const filtered = filterObject(obj, (value) => value > 1)
114
+ * // filtered: { b: 2, c: 3 }
115
+ * ```
116
+ */
117
+ declare function filterObject<T extends Record<string, unknown>, V2 extends T[keyof T]>(object: T, predicate: (value: T[keyof T], key: keyof T) => value is V2): Partial<Record<keyof T, V2>>;
118
+ declare function filterObject<T extends Record<string, unknown>>(object: T, predicate: (value: T[keyof T], key: keyof T) => boolean): Partial<T>;
119
+
120
+ /**
121
+ * Filter a Set based on a predicate function.
122
+ *
123
+ * @param set - The Set to filter
124
+ * @param predicate - A function that tests each value. Returns `true` to keep the value, `false` otherwise.
125
+ * @returns A new Set containing only the values that satisfy the predicate
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const set = new Set([1, 2, 3, 4, 5])
130
+ * const evens = filterSet(set, (n) => n % 2 === 0)
131
+ * // evens: Set { 2, 4 }
132
+ * ```
133
+ */
134
+ declare function filterSet<V, S extends V>(set: ReadonlySet<V>, predicate: (value: V) => value is S): Set<S>;
135
+ declare function filterSet<V>(set: ReadonlySet<V>, predicate: (value: V) => boolean): Set<V>;
136
+
137
+ /**
138
+ * Execute a callback function for each element in an array.
139
+ *
140
+ * @param array - The array to iterate over
141
+ * @param callback - A function to execute for each element
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * const numbers = [1, 2, 3, 4, 5]
146
+ * forEachArray(numbers, (n, index) => {
147
+ * console.log(`Item ${index}: ${n}`)
148
+ * })
149
+ * ```
150
+ */
151
+ declare function forEachArray<V>(array: readonly V[], callback: (value: V, index: number) => void): void;
152
+
153
+ /**
154
+ * Execute a callback function for each element in an iterable.
155
+ *
156
+ * @param collection - The iterable to iterate over
157
+ * @param callback - A function to execute for each element
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * const numbers = [1, 2, 3, 4, 5]
162
+ * forEachIterable(numbers, (n) => {
163
+ * console.log(n)
164
+ * })
165
+ * ```
166
+ */
167
+ declare function forEachIterable<T>(collection: Iterable<T>, callback: (value: T) => void): void;
41
168
 
42
169
  /**
43
170
  * Execute a callback function for each entry in a map-like iterable.
@@ -45,60 +172,348 @@ declare function forEachIterable<T>(set: Iterable<T>, callback: (value: T) => un
45
172
  declare function forEachIterableEntries<K, V>(mapLike: Iterable<[K, V]>, callback: (value: V, key: K) => void): void;
46
173
 
47
174
  /**
48
- * Map a `Map` (or `ReadonlyMap`). Callback receives `(value, key)`. Returns a new `Map`.
175
+ * Execute a callback function for each key in an iterable of key-value pairs.
176
+ *
177
+ * @param mapLike - An iterable of key-value tuples to iterate over
178
+ * @param callback - A function to execute for each key-value pair
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * const entries: [string, number][] = [['apple', 1], ['banana', 2], ['apricot', 3]]
183
+ * forEachIterableKeys(entries, (key, value) => {
184
+ * console.log(`Key: ${key}, Value: ${value}`)
185
+ * })
186
+ * ```
187
+ */
188
+ declare function forEachIterableKeys<K, V>(mapLike: Iterable<[K, V]>, callback: (key: K, value: V) => void): void;
189
+
190
+ /**
191
+ * Execute a callback function for each value in an iterable of key-value pairs.
192
+ *
193
+ * @param mapLike - An iterable of key-value tuples to iterate over
194
+ * @param callback - A function to execute for each value
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
199
+ * forEachIterableValues(entries, (value, key) => {
200
+ * console.log(`Value: ${value}, Key: ${key}`)
201
+ * })
202
+ * ```
203
+ */
204
+ declare function forEachIterableValues<K, V>(mapLike: Iterable<[K, V]>, callback: (value: V, key: K) => void): void;
205
+
206
+ /**
207
+ * Execute a callback function for each entry in a Map.
208
+ *
209
+ * @param map - The Map to iterate over
210
+ * @param callback - A function to execute for each value-key pair
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * const map = new Map([['a', 1], ['b', 2], ['c', 3]])
215
+ * forEachMap(map, (value, key) => {
216
+ * console.log(`${key}: ${value}`)
217
+ * })
218
+ * ```
219
+ */
220
+ declare function forEachMap<K, V>(map: ReadonlyMap<K, V>, callback: (value: V, key: K) => void): void;
221
+
222
+ /**
223
+ * Execute a callback function for each property in an object.
224
+ *
225
+ * @param object - The object to iterate over
226
+ * @param callback - A function to execute for each value-key pair
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * const obj = { a: 1, b: 2, c: 3 }
231
+ * forEachObject(obj, (value, key) => {
232
+ * console.log(`${key}: ${value}`)
233
+ * })
234
+ * ```
235
+ */
236
+ declare function forEachObject<T extends Record<string, unknown>>(object: T, callback: (value: T[keyof T], key: keyof T) => void): void;
237
+
238
+ /**
239
+ * Execute a callback function for each value in a Set.
240
+ *
241
+ * @param set - The Set to iterate over
242
+ * @param callback - A function to execute for each value
243
+ *
244
+ * @example
245
+ * ```ts
246
+ * const set = new Set([1, 2, 3, 4, 5])
247
+ * forEachSet(set, (n) => {
248
+ * console.log(n)
249
+ * })
250
+ * ```
251
+ */
252
+ declare function forEachSet<V>(set: ReadonlySet<V>, callback: (value: V) => void): void;
253
+
254
+ /**
255
+ * Transform each element in an array using a mapper function.
256
+ *
257
+ * @param array - The array to transform
258
+ * @param mapper - A function that transforms each element
259
+ * @returns A new array containing the transformed elements
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * const numbers = [1, 2, 3, 4, 5]
264
+ * const doubled = mapArray(numbers, (n) => n * 2)
265
+ * // doubled: [2, 4, 6, 8, 10]
266
+ * ```
267
+ */
268
+ declare function mapArray<V, R>(array: readonly V[], mapper: (value: V, index: number) => R): R[];
269
+
270
+ /**
271
+ * Transform each element in an iterable using a mapper function.
272
+ *
273
+ * @param collection - The iterable to transform
274
+ * @param mapper - A function that transforms each element
275
+ * @returns An iterable containing the transformed elements
276
+ *
277
+ * @example
278
+ * ```ts
279
+ * const numbers = [1, 2, 3, 4, 5]
280
+ * const doubled = mapIterable(numbers, (n) => n * 2)
281
+ * // doubled: Iterable of [2, 4, 6, 8, 10]
282
+ * ```
283
+ */
284
+ declare function mapIterable<T, R>(collection: Iterable<T>, mapper: (value: T) => R): Iterable<R>;
285
+
286
+ /**
287
+ * Transform each entry in an iterable of key-value pairs using a mapper function.
288
+ *
289
+ * @param mapLike - An iterable of key-value tuples to transform
290
+ * @param mapper - A function that transforms each entry
291
+ * @returns An iterable containing the transformed entries
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
296
+ * const mapped = mapIterableEntries(entries, ([key, value]) => [key, value * 2])
297
+ * // mapped: Iterable of [['a', 2], ['b', 4], ['c', 6]]
298
+ * ```
299
+ */
300
+ declare function mapIterableEntries<K, V, K2, V2>(mapLike: Iterable<[K, V]>, mapper: (entry: [K, V]) => [K2, V2]): Iterable<[K2, V2]>;
301
+
302
+ /**
303
+ * Transform the keys in an iterable of key-value pairs using a mapper function.
304
+ *
305
+ * @param mapLike - An iterable of key-value tuples to transform
306
+ * @param mapper - A function that transforms each key
307
+ * @returns An iterable containing entries with transformed keys
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
312
+ * const mapped = mapIterableKeys(entries, (key) => key.toUpperCase())
313
+ * // mapped: Iterable of [['A', 1], ['B', 2], ['C', 3]]
314
+ * ```
315
+ */
316
+ declare function mapIterableKeys<K, V, K2>(mapLike: Iterable<[K, V]>, mapper: (key: K, value: V) => K2): Iterable<[K2, V]>;
317
+
318
+ /**
319
+ * Transform the values in an iterable of key-value pairs using a mapper function.
320
+ *
321
+ * @param mapLike - An iterable of key-value tuples to transform
322
+ * @param mapper - A function that transforms each value
323
+ * @returns An iterable containing entries with transformed values
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
328
+ * const mapped = mapIterableValues(entries, (value) => value * 2)
329
+ * // mapped: Iterable of [['a', 2], ['b', 4], ['c', 6]]
330
+ * ```
331
+ */
332
+ declare function mapIterableValues<K, V, V2>(mapLike: Iterable<[K, V]>, mapper: (value: V, key: K) => V2): Iterable<[K, V2]>;
333
+
334
+ /**
335
+ * Transform the values in a Map using a mapper function.
336
+ *
337
+ * @param map - The Map to transform
338
+ * @param mapper - A function that transforms each value
339
+ * @returns A new Map containing the transformed values
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * const map = new Map([['a', 1], ['b', 2], ['c', 3]])
344
+ * const doubled = mapMap(map, (value) => value * 2)
345
+ * // doubled: Map { 'a' => 2, 'b' => 4, 'c' => 6 }
346
+ * ```
347
+ */
348
+ declare function mapMap<K, V, V2>(map: ReadonlyMap<K, V>, mapper: (value: V, key: K) => V2): Map<K, V2>;
349
+
350
+ /**
351
+ * Transform the values in an object using a mapper function.
352
+ *
353
+ * @param object - The object to transform
354
+ * @param mapper - A function that transforms each value
355
+ * @returns A new object containing the transformed values
356
+ *
357
+ * @example
358
+ * ```ts
359
+ * const obj = { a: 1, b: 2, c: 3 }
360
+ * const doubled = mapObject(obj, (value) => value * 2)
361
+ * // doubled: { a: 2, b: 4, c: 6 }
362
+ * ```
363
+ */
364
+ declare function mapObject<T extends Record<string, unknown>, R>(object: T, mapper: (value: T[keyof T], key: keyof T) => R): Record<keyof T, R>;
365
+
366
+ /**
367
+ * Transform each value in a Set using a mapper function.
368
+ *
369
+ * @param set - The Set to transform
370
+ * @param mapper - A function that transforms each value
371
+ * @returns A new Set containing the transformed values
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * const set = new Set([1, 2, 3, 4, 5])
376
+ * const doubled = mapSet(set, (n) => n * 2)
377
+ * // doubled: Set { 2, 4, 6, 8, 10 }
378
+ * ```
379
+ */
380
+ declare function mapSet<V, R>(set: ReadonlySet<V>, mapper: (value: V) => R): Set<R>;
381
+
382
+ /**
383
+ * Reduce an array to a single value using a reducer function.
384
+ *
385
+ * @param array - The array to reduce
386
+ * @param reducer - A function that combines values
387
+ * @param initial - The initial value for the reduction
388
+ * @returns The reduced value
389
+ *
390
+ * @example
391
+ * ```ts
392
+ * const numbers = [1, 2, 3, 4, 5]
393
+ * const sum = reduceArray(numbers, (acc, n) => acc + n, 0)
394
+ * // sum: 15
395
+ * ```
49
396
  */
50
- declare function map<K, V, R>(map: ReadonlyMap<K, V>, transform: (value: V, key: K) => R): Map<K, R>;
51
- /** Map a `Set` (or `ReadonlySet`). Callback receives `(value)`. Returns a new `Set`. */
52
- declare function map<V, R>(set: ReadonlySet<V>, transform: (value: V) => R): Set<R>;
53
- /** Map an array. Callback receives `(value, index)`. Returns a new array. */
54
- declare function map<V, R>(array: readonly V[], transform: (value: V, index: number) => R): R[];
55
- /** Map a plain object. Callback receives `(value, key)`. Returns a new object. */
56
- declare function map<K extends string, V, R>(object: Record<K, V>, transform: (value: V, key: K) => R): Record<K, R>;
57
- /** Map any `Iterable`. Callback receives `(value)`. Returns a new array. */
58
- declare function map<V, R>(iterable: Iterable<V>, transform: (value: V) => R): R[];
397
+ declare function reduceArray<V, R>(array: readonly V[], reducer: (accumulator: R, value: V, index: number) => R, initial: R): R;
59
398
 
60
399
  /**
61
- * Transform values of an iterable.
400
+ * Reduce an iterable to a single value using a reducer function.
401
+ *
402
+ * @param collection - The iterable to reduce
403
+ * @param reducer - A function that combines values
404
+ * @param initial - The initial value for the reduction
405
+ * @returns The reduced value
406
+ *
407
+ * @example
408
+ * ```ts
409
+ * const numbers = [1, 2, 3, 4, 5]
410
+ * const sum = reduceIterable(numbers, (acc, n) => acc + n, 0)
411
+ * // sum: 15
412
+ * ```
62
413
  */
63
- declare function mapIterable<T, R>(set: Iterable<T>, transform: (value: T) => R): Iterable<R>;
414
+ declare function reduceIterable<T, R>(collection: Iterable<T>, reducer: (accumulator: R, value: T) => R, initial: R): R;
64
415
 
65
416
  /**
66
- * Transform both keys and values of map entries.
417
+ * Reduce an iterable of key-value entries to a single value using a reducer function.
418
+ *
419
+ * @param mapLike - An iterable of key-value tuples to reduce
420
+ * @param reducer - A function that combines entries
421
+ * @param initial - The initial value for the reduction
422
+ * @returns The reduced value
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
427
+ * const sum = reduceIterableEntries(entries, (acc, [key, value]) => acc + value, 0)
428
+ * // sum: 6
429
+ * ```
67
430
  */
68
- declare function mapIterableEntries<K, V, U, W>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => [U, W]): Iterable<[U, W]>;
431
+ declare function reduceIterableEntries<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, entry: [K, V]) => R, initial: R): R;
69
432
 
70
433
  /**
71
- * Transform map keys while preserving values.
434
+ * Reduce an iterable of key-value entries based on keys using a reducer function.
435
+ *
436
+ * @param mapLike - An iterable of key-value tuples to reduce
437
+ * @param reducer - A function that combines keys
438
+ * @param initial - The initial value for the reduction
439
+ * @returns The reduced value
440
+ *
441
+ * @example
442
+ * ```ts
443
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
444
+ * const keys = reduceIterableKeys(entries, (acc, key, value) => acc + key, '')
445
+ * // keys: 'abc'
446
+ * ```
72
447
  */
73
- declare function mapIterableKeys<K, V, U>(mapLike: Iterable<[K, V]>, transform: (key: K, value: V) => U): Iterable<[U, V]>;
448
+ declare function reduceIterableKeys<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, key: K, value: V) => R, initial: R): R;
74
449
 
75
450
  /**
76
- * Transform map values while preserving keys.
451
+ * Reduce an iterable of key-value entries based on values using a reducer function.
452
+ *
453
+ * @param mapLike - An iterable of key-value tuples to reduce
454
+ * @param reducer - A function that combines values
455
+ * @param initial - The initial value for the reduction
456
+ * @returns The reduced value
457
+ *
458
+ * @example
459
+ * ```ts
460
+ * const entries: [string, number][] = [['a', 1], ['b', 2], ['c', 3]]
461
+ * const sum = reduceIterableValues(entries, (acc, value, key) => acc + value, 0)
462
+ * // sum: 6
463
+ * ```
77
464
  */
78
- declare function mapIterableValues<K, V, U>(mapLike: Iterable<[K, V]>, transform: (value: V, key: K) => U): Iterable<[K, U]>;
465
+ declare function reduceIterableValues<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, value: V, key: K) => R, initial: R): R;
79
466
 
80
467
  /**
81
- * Reduce a `Map` (or `ReadonlyMap`). Callback receives `(accumulator, value, key)`.
468
+ * Reduce a Map to a single value using a reducer function.
469
+ *
470
+ * @param map - The Map to reduce
471
+ * @param reducer - A function that combines values
472
+ * @param initial - The initial value for the reduction
473
+ * @returns The reduced value
474
+ *
475
+ * @example
476
+ * ```ts
477
+ * const map = new Map([['a', 1], ['b', 2], ['c', 3]])
478
+ * const sum = reduceMap(map, (acc, value, key) => acc + value, 0)
479
+ * // sum: 6
480
+ * ```
82
481
  */
83
- declare function reduce<K, V, T>(map: ReadonlyMap<K, V>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
84
- /** Reduce a `Set` (or `ReadonlySet`). Callback receives `(accumulator, value)`. */
85
- declare function reduce<V, T>(set: ReadonlySet<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
86
- /** Reduce an array. Callback receives `(accumulator, value, index)`. */
87
- declare function reduce<V, T>(array: readonly V[], reducer: (accumulator: T, value: V, index: number) => T, initialValue: T): T;
88
- /** Reduce a plain object. Callback receives `(accumulator, value, key)`. */
89
- declare function reduce<K extends string, V, T>(object: Record<K, V>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
90
- /** Reduce any `Iterable`. Callback receives `(accumulator, value)`. */
91
- declare function reduce<V, T>(iterable: Iterable<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
482
+ declare function reduceMap<K, V, R>(map: ReadonlyMap<K, V>, reducer: (accumulator: R, value: V, key: K) => R, initial: R): R;
92
483
 
93
484
  /**
94
- * Reduce any iterable to a single value.
485
+ * Reduce an object to a single value using a reducer function.
486
+ *
487
+ * @param object - The object to reduce
488
+ * @param reducer - A function that combines values
489
+ * @param initial - The initial value for the reduction
490
+ * @returns The reduced value
491
+ *
492
+ * @example
493
+ * ```ts
494
+ * const obj = { a: 1, b: 2, c: 3 }
495
+ * const sum = reduceObject(obj, (acc, value, key) => acc + value, 0)
496
+ * // sum: 6
497
+ * ```
95
498
  */
96
- declare function reduceIterable<V, T>(iterable: Iterable<V>, reducer: (accumulator: T, value: V) => T, initialValue: T): T;
499
+ declare function reduceObject<T extends Record<string, unknown>, R>(object: T, reducer: (accumulator: R, value: T[keyof T], key: keyof T) => R, initial: R): R;
97
500
 
98
501
  /**
99
- * Reduce a map-like iterable to a single value.
502
+ * Reduce a Set to a single value using a reducer function.
503
+ *
504
+ * @param set - The Set to reduce
505
+ * @param reducer - A function that combines values
506
+ * @param initial - The initial value for the reduction
507
+ * @returns The reduced value
508
+ *
509
+ * @example
510
+ * ```ts
511
+ * const set = new Set([1, 2, 3, 4, 5])
512
+ * const sum = reduceSet(set, (acc, n) => acc + n, 0)
513
+ * // sum: 15
514
+ * ```
100
515
  */
101
- declare function reduceIterableEntries<K, V, T>(mapLike: Iterable<[K, V]>, reducer: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
516
+ declare function reduceSet<V, R>(set: ReadonlySet<V>, reducer: (accumulator: R, value: V) => R, initial: R): R;
102
517
 
103
518
  /**
104
519
  * Convert a map-like iterable to a regular object.
@@ -106,4 +521,4 @@ declare function reduceIterableEntries<K, V, T>(mapLike: Iterable<[K, V]>, reduc
106
521
  declare function toObjectIterable<K extends string, V>(mapLike: Iterable<[K, V]>): Record<K, V>;
107
522
  declare function toObjectIterable<K, V>(mapLike: Iterable<[K, V]>): Record<Extract<K, string> | string, V>;
108
523
 
109
- export { filter, filterIterable, filterIterableEntries, forEach, forEachIterable, forEachIterableEntries, map, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, reduce, reduceIterable, reduceIterableEntries, toObjectIterable };
524
+ export { filterArray, filterIterable, filterIterableEntries, filterIterableKeys, filterIterableValues, filterMap, filterObject, filterSet, forEachArray, forEachIterable, forEachIterableEntries, forEachIterableKeys, forEachIterableValues, forEachMap, forEachObject, forEachSet, mapArray, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, mapMap, mapObject, mapSet, reduceArray, reduceIterable, reduceIterableEntries, reduceIterableKeys, reduceIterableValues, reduceMap, reduceObject, reduceSet, toObjectIterable };
package/index.mjs CHANGED
@@ -1,215 +1,283 @@
1
- import { filter as filter$1, forEach, map as map$1 } from 'iter-tools';
1
+ import { filter, forEach, map } from 'iter-tools';
2
+ import { entriesOf } from '@bemoje/object';
2
3
 
3
4
  var __defProp = Object.defineProperty;
4
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
6
 
6
- // src/filter.ts
7
- function filter(target, predicate) {
8
- if (Symbol.iterator in target) {
9
- if (target instanceof Map) {
10
- const result3 = /* @__PURE__ */ new Map();
11
- for (const [key, value] of target) {
12
- if (predicate(value, key)) {
13
- result3.set(key, value);
14
- }
15
- }
16
- return result3;
17
- }
18
- if (target instanceof Set) {
19
- const result3 = /* @__PURE__ */ new Set();
20
- for (const value of target) {
21
- if (predicate(value)) {
22
- result3.add(value);
23
- }
24
- }
25
- return result3;
26
- }
27
- if (Array.isArray(target)) {
28
- return target.filter(predicate);
29
- }
30
- const result2 = [];
31
- for (const value of target) {
32
- if (predicate(value)) {
33
- result2.push(value);
34
- }
35
- }
36
- return result2;
37
- }
38
- const result = {};
39
- for (const [key, value] of Object.entries(target)) {
40
- if (predicate(value, key)) {
41
- result[key] = value;
7
+ // src/filterArray.ts
8
+ function filterArray(array, predicate) {
9
+ let index = 0;
10
+ const result = [];
11
+ for (const value of array) {
12
+ if (predicate(value, index)) {
13
+ result.push(value);
42
14
  }
15
+ index++;
43
16
  }
44
17
  return result;
45
18
  }
46
- __name(filter, "filter");
47
- function filterIterable(set, predicate) {
48
- return filter$1((value) => {
19
+ __name(filterArray, "filterArray");
20
+ function filterIterable(collection, predicate) {
21
+ return filter((value) => {
49
22
  return predicate(value);
50
- }, set);
23
+ }, collection);
51
24
  }
52
25
  __name(filterIterable, "filterIterable");
53
26
  function filterIterableEntries(mapLike, predicate) {
54
- return filter$1(([k, v]) => {
55
- return predicate(v, k);
56
- }, mapLike);
27
+ return filter(predicate, mapLike);
57
28
  }
58
29
  __name(filterIterableEntries, "filterIterableEntries");
59
- function forEachIterableEntries(mapLike, callback) {
60
- return forEach(([k, v]) => {
61
- return callback(v, k);
30
+ function filterIterableKeys(mapLike, predicate) {
31
+ return filter(([k, v]) => {
32
+ return predicate(k, v);
62
33
  }, mapLike);
63
34
  }
64
- __name(forEachIterableEntries, "forEachIterableEntries");
35
+ __name(filterIterableKeys, "filterIterableKeys");
36
+ function filterIterableValues(mapLike, predicate) {
37
+ return filter(([k, v]) => {
38
+ return predicate(v, k);
39
+ }, mapLike);
40
+ }
41
+ __name(filterIterableValues, "filterIterableValues");
65
42
 
66
- // src/forEach.ts
67
- function forEach2(target, callback) {
68
- if (Symbol.iterator in target) {
69
- if (target instanceof Map) {
70
- return target.forEach(callback);
71
- }
72
- if (target instanceof Set) {
73
- return target.forEach(callback);
43
+ // src/filterMap.ts
44
+ function filterMap(map5, predicate) {
45
+ const result = /* @__PURE__ */ new Map();
46
+ for (const [key, value] of map5) {
47
+ if (predicate(value, key)) {
48
+ result.set(key, value);
74
49
  }
75
- if (Array.isArray(target)) {
76
- return target.forEach(callback);
50
+ }
51
+ return result;
52
+ }
53
+ __name(filterMap, "filterMap");
54
+ function filterObject(object, predicate) {
55
+ const result = {};
56
+ for (const [key, value] of entriesOf(object)) {
57
+ if (predicate(value, key)) {
58
+ result[key] = value;
77
59
  }
78
- for (const value of target) {
79
- callback(value);
60
+ }
61
+ return result;
62
+ }
63
+ __name(filterObject, "filterObject");
64
+
65
+ // src/filterSet.ts
66
+ function filterSet(set, predicate) {
67
+ const result = /* @__PURE__ */ new Set();
68
+ for (const value of set) {
69
+ if (predicate(value)) {
70
+ result.add(value);
80
71
  }
81
- return;
82
72
  }
83
- return forEachIterableEntries(Object.entries(target), callback);
73
+ return result;
84
74
  }
85
- __name(forEach2, "forEach");
86
- function forEachIterable(set, callback) {
87
- return forEach((value) => {
88
- return callback(value);
89
- }, set);
75
+ __name(filterSet, "filterSet");
76
+
77
+ // src/forEachArray.ts
78
+ function forEachArray(array, callback) {
79
+ let index = 0;
80
+ for (const value of array) {
81
+ callback(value, index);
82
+ index++;
83
+ }
84
+ }
85
+ __name(forEachArray, "forEachArray");
86
+ function forEachIterable(collection, callback) {
87
+ forEach((value) => {
88
+ callback(value);
89
+ }, collection);
90
90
  }
91
91
  __name(forEachIterable, "forEachIterable");
92
+ function forEachIterableEntries(mapLike, callback) {
93
+ return forEach(([k, v]) => {
94
+ return callback(v, k);
95
+ }, mapLike);
96
+ }
97
+ __name(forEachIterableEntries, "forEachIterableEntries");
98
+ function forEachIterableKeys(mapLike, callback) {
99
+ forEach(([k, v]) => {
100
+ callback(k, v);
101
+ }, mapLike);
102
+ }
103
+ __name(forEachIterableKeys, "forEachIterableKeys");
104
+ function forEachIterableValues(mapLike, callback) {
105
+ forEach(([k, v]) => {
106
+ callback(v, k);
107
+ }, mapLike);
108
+ }
109
+ __name(forEachIterableValues, "forEachIterableValues");
92
110
 
93
- // src/map.ts
94
- function map(target, transform) {
95
- if (target instanceof Map) {
96
- const result2 = /* @__PURE__ */ new Map();
97
- for (const [key, value] of target) {
98
- result2.set(key, transform(value, key));
99
- }
100
- return result2;
101
- }
102
- if (target instanceof Set) {
103
- const result2 = /* @__PURE__ */ new Set();
104
- for (const value of target) {
105
- result2.add(transform(value));
106
- }
107
- return result2;
111
+ // src/forEachMap.ts
112
+ function forEachMap(map5, callback) {
113
+ for (const [key, value] of map5) {
114
+ callback(value, key);
108
115
  }
109
- if (Array.isArray(target)) {
110
- return target.map(transform);
116
+ }
117
+ __name(forEachMap, "forEachMap");
118
+ function forEachObject(object, callback) {
119
+ for (const [key, value] of entriesOf(object)) {
120
+ callback(value, key);
111
121
  }
112
- if (Symbol.iterator in target) {
113
- const result2 = [];
114
- for (const value of target) {
115
- result2.push(transform(value));
116
- }
117
- return result2;
122
+ }
123
+ __name(forEachObject, "forEachObject");
124
+
125
+ // src/forEachSet.ts
126
+ function forEachSet(set, callback) {
127
+ for (const value of set) {
128
+ callback(value);
118
129
  }
119
- const result = {};
120
- for (const [key, value] of Object.entries(target)) {
121
- result[key] = transform(value, key);
130
+ }
131
+ __name(forEachSet, "forEachSet");
132
+
133
+ // src/mapArray.ts
134
+ function mapArray(array, mapper) {
135
+ let index = 0;
136
+ const result = [];
137
+ for (const value of array) {
138
+ result.push(mapper(value, index));
139
+ index++;
122
140
  }
123
141
  return result;
124
142
  }
125
- __name(map, "map");
126
- function mapIterable(set, transform) {
127
- return map$1((value) => {
128
- return transform(value);
129
- }, set);
143
+ __name(mapArray, "mapArray");
144
+ function mapIterable(collection, mapper) {
145
+ return map((value) => {
146
+ return mapper(value);
147
+ }, collection);
130
148
  }
131
149
  __name(mapIterable, "mapIterable");
132
- function mapIterableEntries(mapLike, transform) {
133
- return map$1(([k, v]) => {
134
- return transform(v, k);
135
- }, mapLike);
150
+ function mapIterableEntries(mapLike, mapper) {
151
+ return map(mapper, mapLike);
136
152
  }
137
153
  __name(mapIterableEntries, "mapIterableEntries");
138
- function mapIterableKeys(mapLike, transform) {
139
- return map$1(([k, v]) => {
154
+ function mapIterableKeys(mapLike, mapper) {
155
+ return map(([k, v]) => {
140
156
  return [
141
- transform(k, v),
157
+ mapper(k, v),
142
158
  v
143
159
  ];
144
160
  }, mapLike);
145
161
  }
146
162
  __name(mapIterableKeys, "mapIterableKeys");
147
- function mapIterableValues(mapLike, transform) {
148
- return map$1(([k, v]) => {
163
+ function mapIterableValues(mapLike, mapper) {
164
+ return map(([k, v]) => {
149
165
  return [
150
166
  k,
151
- transform(v, k)
167
+ mapper(v, k)
152
168
  ];
153
169
  }, mapLike);
154
170
  }
155
171
  __name(mapIterableValues, "mapIterableValues");
156
172
 
157
- // src/reduce.ts
158
- function reduce(target, reducer, initialValue) {
159
- if (Symbol.iterator in target) {
160
- if (target instanceof Map) {
161
- let acc3 = initialValue;
162
- for (const [key, value] of target.entries()) {
163
- acc3 = reducer(acc3, value, key);
164
- }
165
- return acc3;
166
- }
167
- if (target instanceof Set) {
168
- let acc3 = initialValue;
169
- for (const value of target.values()) {
170
- acc3 = reducer(acc3, value);
171
- }
172
- return acc3;
173
- }
174
- if (Array.isArray(target)) {
175
- let acc3 = initialValue;
176
- for (let i = 0; i < target.length; i++) {
177
- acc3 = reducer(acc3, target[i], i);
178
- }
179
- return acc3;
180
- }
181
- let acc2 = initialValue;
182
- for (const value of target) {
183
- acc2 = reducer(acc2, value);
184
- }
185
- return acc2;
173
+ // src/mapMap.ts
174
+ function mapMap(map5, mapper) {
175
+ const result = /* @__PURE__ */ new Map();
176
+ for (const [key, value] of map5) {
177
+ result.set(key, mapper(value, key));
178
+ }
179
+ return result;
180
+ }
181
+ __name(mapMap, "mapMap");
182
+ function mapObject(object, mapper) {
183
+ const result = {};
184
+ for (const [key, value] of entriesOf(object)) {
185
+ result[key] = mapper(value, key);
186
+ }
187
+ return result;
188
+ }
189
+ __name(mapObject, "mapObject");
190
+
191
+ // src/mapSet.ts
192
+ function mapSet(set, mapper) {
193
+ const result = /* @__PURE__ */ new Set();
194
+ for (const value of set) {
195
+ result.add(mapper(value));
186
196
  }
187
- let acc = initialValue;
188
- for (const [key, value] of Object.entries(target)) {
189
- acc = reducer(acc, value, key);
197
+ return result;
198
+ }
199
+ __name(mapSet, "mapSet");
200
+
201
+ // src/reduceArray.ts
202
+ function reduceArray(array, reducer, initial) {
203
+ let accumulator = initial;
204
+ let index = 0;
205
+ for (const value of array) {
206
+ accumulator = reducer(accumulator, value, index);
207
+ index++;
190
208
  }
191
- return acc;
209
+ return accumulator;
192
210
  }
193
- __name(reduce, "reduce");
211
+ __name(reduceArray, "reduceArray");
194
212
 
195
213
  // src/reduceIterable.ts
196
- function reduceIterable(iterable, reducer, initialValue) {
197
- for (const value of iterable) {
198
- initialValue = reducer(initialValue, value);
214
+ function reduceIterable(collection, reducer, initial) {
215
+ let accumulator = initial;
216
+ for (const value of collection) {
217
+ accumulator = reducer(accumulator, value);
199
218
  }
200
- return initialValue;
219
+ return accumulator;
201
220
  }
202
221
  __name(reduceIterable, "reduceIterable");
203
222
 
204
223
  // src/reduceIterableEntries.ts
205
- function reduceIterableEntries(mapLike, reducer, initialValue) {
206
- for (const [key, value] of mapLike) {
207
- initialValue = reducer(initialValue, value, key);
224
+ function reduceIterableEntries(mapLike, reducer, initial) {
225
+ let accumulator = initial;
226
+ for (const entry of mapLike) {
227
+ accumulator = reducer(accumulator, entry);
208
228
  }
209
- return initialValue;
229
+ return accumulator;
210
230
  }
211
231
  __name(reduceIterableEntries, "reduceIterableEntries");
212
232
 
233
+ // src/reduceIterableKeys.ts
234
+ function reduceIterableKeys(mapLike, reducer, initial) {
235
+ let accumulator = initial;
236
+ for (const [k, v] of mapLike) {
237
+ accumulator = reducer(accumulator, k, v);
238
+ }
239
+ return accumulator;
240
+ }
241
+ __name(reduceIterableKeys, "reduceIterableKeys");
242
+
243
+ // src/reduceIterableValues.ts
244
+ function reduceIterableValues(mapLike, reducer, initial) {
245
+ let accumulator = initial;
246
+ for (const [k, v] of mapLike) {
247
+ accumulator = reducer(accumulator, v, k);
248
+ }
249
+ return accumulator;
250
+ }
251
+ __name(reduceIterableValues, "reduceIterableValues");
252
+
253
+ // src/reduceMap.ts
254
+ function reduceMap(map5, reducer, initial) {
255
+ let accumulator = initial;
256
+ for (const [key, value] of map5) {
257
+ accumulator = reducer(accumulator, value, key);
258
+ }
259
+ return accumulator;
260
+ }
261
+ __name(reduceMap, "reduceMap");
262
+ function reduceObject(object, reducer, initial) {
263
+ let accumulator = initial;
264
+ for (const [key, value] of entriesOf(object)) {
265
+ accumulator = reducer(accumulator, value, key);
266
+ }
267
+ return accumulator;
268
+ }
269
+ __name(reduceObject, "reduceObject");
270
+
271
+ // src/reduceSet.ts
272
+ function reduceSet(set, reducer, initial) {
273
+ let accumulator = initial;
274
+ for (const value of set) {
275
+ accumulator = reducer(accumulator, value);
276
+ }
277
+ return accumulator;
278
+ }
279
+ __name(reduceSet, "reduceSet");
280
+
213
281
  // src/toObjectIterable.ts
214
282
  function toObjectIterable(mapLike) {
215
283
  const object = {};
@@ -224,4 +292,4 @@ function toObjectIterable(mapLike) {
224
292
  }
225
293
  __name(toObjectIterable, "toObjectIterable");
226
294
 
227
- export { filter, filterIterable, filterIterableEntries, forEach2 as forEach, forEachIterable, forEachIterableEntries, map, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, reduce, reduceIterable, reduceIterableEntries, toObjectIterable };
295
+ export { filterArray, filterIterable, filterIterableEntries, filterIterableKeys, filterIterableValues, filterMap, filterObject, filterSet, forEachArray, forEachIterable, forEachIterableEntries, forEachIterableKeys, forEachIterableValues, forEachMap, forEachObject, forEachSet, mapArray, mapIterable, mapIterableEntries, mapIterableKeys, mapIterableValues, mapMap, mapObject, mapSet, reduceArray, reduceIterable, reduceIterableEntries, reduceIterableKeys, reduceIterableValues, reduceMap, reduceObject, reduceSet, toObjectIterable };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/iter",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "iter",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -12,10 +12,16 @@
12
12
  "types": "./index.d.ts",
13
13
  "import": "./index.mjs",
14
14
  "default": "./index.mjs"
15
+ },
16
+ "./*": {
17
+ "types": "./index.d.ts",
18
+ "import": "./index.mjs",
19
+ "default": "./index.mjs"
15
20
  }
16
21
  },
17
22
  "dependencies": {
18
- "iter-tools": "^7.5.4"
23
+ "iter-tools": "^7.5.4",
24
+ "@bemoje/object": "^2.1.0"
19
25
  },
20
26
  "publishConfig": {
21
27
  "access": "public"