@bemoje/iter 1.0.0 → 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.
- package/README.md +96 -0
- package/index.d.ts +502 -115
- package/index.mjs +239 -277
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @bemoje/iter
|
|
2
|
+
|
|
3
|
+
Utility functions for working with iterables, especially map-like key-value pair structures.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/iter
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Transform Iterables
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { mapIterable, mapIterableKeys, mapIterableValues } from '@bemoje/iter'
|
|
20
|
+
|
|
21
|
+
const entries: Iterable<[string, number]> = new Map([
|
|
22
|
+
['a', 1],
|
|
23
|
+
['b', 2],
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
// Transform both keys and values
|
|
27
|
+
const mapped = mapIterable(entries, (value, key) => [key.toUpperCase(), value * 10])
|
|
28
|
+
// [['A', 10], ['B', 20]]
|
|
29
|
+
|
|
30
|
+
// Transform only keys
|
|
31
|
+
const newKeys = mapIterableKeys(entries, (key) => key.toUpperCase())
|
|
32
|
+
// [['A', 1], ['B', 2]]
|
|
33
|
+
|
|
34
|
+
// Transform only values
|
|
35
|
+
const doubled = mapIterableValues(entries, (value) => value * 2)
|
|
36
|
+
// [['a', 2], ['b', 4]]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Filter and Reduce
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { filterIterable, reduceIterable, forEachIterable } from '@bemoje/iter'
|
|
43
|
+
|
|
44
|
+
const entries: Iterable<[string, number]> = new Map([
|
|
45
|
+
['a', 1],
|
|
46
|
+
['b', 2],
|
|
47
|
+
['c', 3],
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
// Filter entries by value
|
|
51
|
+
const filtered = filterIterable(entries, (value) => value > 1)
|
|
52
|
+
// [['b', 2], ['c', 3]]
|
|
53
|
+
|
|
54
|
+
// Reduce to a single value
|
|
55
|
+
const sum = reduceIterable(entries, (acc, value) => acc + value, 0)
|
|
56
|
+
// 6
|
|
57
|
+
|
|
58
|
+
// Side effects
|
|
59
|
+
forEachIterable(entries, (value, key) => console.log(`${key}: ${value}`))
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Count Unique Values
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { countUniques } from '@bemoje/iter'
|
|
66
|
+
|
|
67
|
+
const words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
|
|
68
|
+
const counts = countUniques(words)
|
|
69
|
+
// ExtMap { 'apple' => 3, 'banana' => 2, 'cherry' => 1 } (sorted by count desc)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Convert to Object
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { toObjectIterable } from '@bemoje/iter'
|
|
76
|
+
|
|
77
|
+
const entries: Iterable<[string, number]> = [
|
|
78
|
+
['a', 1],
|
|
79
|
+
['b', 2],
|
|
80
|
+
]
|
|
81
|
+
const obj = toObjectIterable(entries)
|
|
82
|
+
// { a: 1, b: 2 }
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API Reference
|
|
86
|
+
|
|
87
|
+
| Export | Description |
|
|
88
|
+
| ------------------- | ------------------------------------------------------- |
|
|
89
|
+
| `mapIterable` | Transform both keys and values of map-like entries |
|
|
90
|
+
| `mapIterableKeys` | Transform keys while preserving values |
|
|
91
|
+
| `mapIterableValues` | Transform values while preserving keys |
|
|
92
|
+
| `filterIterable` | Filter map entries by predicate |
|
|
93
|
+
| `reduceIterable` | Reduce a map-like iterable to a single value |
|
|
94
|
+
| `forEachIterable` | Execute a callback for each entry |
|
|
95
|
+
| `countUniques` | Count unique values in an iterable, sorted by frequency |
|
|
96
|
+
| `toObjectIterable` | Convert key-value iterable to a plain object |
|
package/index.d.ts
CHANGED
|
@@ -1,137 +1,524 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* const
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
*
|
|
99
|
-
*/
|
|
100
|
-
declare function
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
1
|
+
/**
|
|
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
|
+
* ```
|
|
14
|
+
*/
|
|
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[];
|
|
17
|
+
|
|
18
|
+
/**
|
|
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
|
+
* ```
|
|
31
|
+
*/
|
|
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>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
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
|
+
* ```
|
|
48
|
+
*/
|
|
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]>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
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
|
+
* ```
|
|
65
|
+
*/
|
|
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]>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
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
|
+
* ```
|
|
82
|
+
*/
|
|
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;
|
|
106
168
|
|
|
107
169
|
/**
|
|
108
170
|
* Execute a callback function for each entry in a map-like iterable.
|
|
109
171
|
*/
|
|
110
|
-
declare function
|
|
172
|
+
declare function forEachIterableEntries<K, V>(mapLike: Iterable<[K, V]>, callback: (value: V, key: K) => void): void;
|
|
173
|
+
|
|
174
|
+
/**
|
|
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>;
|
|
111
365
|
|
|
112
366
|
/**
|
|
113
|
-
* Transform
|
|
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
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
declare function reduceArray<V, R>(array: readonly V[], reducer: (accumulator: R, value: V, index: number) => R, initial: R): R;
|
|
398
|
+
|
|
399
|
+
/**
|
|
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
|
+
* ```
|
|
114
413
|
*/
|
|
115
|
-
declare function
|
|
414
|
+
declare function reduceIterable<T, R>(collection: Iterable<T>, reducer: (accumulator: R, value: T) => R, initial: R): R;
|
|
116
415
|
|
|
117
416
|
/**
|
|
118
|
-
*
|
|
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
|
+
* ```
|
|
119
430
|
*/
|
|
120
|
-
declare function
|
|
431
|
+
declare function reduceIterableEntries<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, entry: [K, V]) => R, initial: R): R;
|
|
121
432
|
|
|
122
433
|
/**
|
|
123
|
-
*
|
|
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
|
+
* ```
|
|
124
447
|
*/
|
|
125
|
-
declare function
|
|
448
|
+
declare function reduceIterableKeys<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, key: K, value: V) => R, initial: R): R;
|
|
126
449
|
|
|
127
450
|
/**
|
|
128
|
-
* Reduce
|
|
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
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
declare function reduceIterableValues<K, V, R>(mapLike: Iterable<[K, V]>, reducer: (accumulator: R, value: V, key: K) => R, initial: R): R;
|
|
466
|
+
|
|
467
|
+
/**
|
|
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
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
declare function reduceMap<K, V, R>(map: ReadonlyMap<K, V>, reducer: (accumulator: R, value: V, key: K) => R, initial: R): R;
|
|
483
|
+
|
|
484
|
+
/**
|
|
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
|
+
* ```
|
|
498
|
+
*/
|
|
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;
|
|
500
|
+
|
|
501
|
+
/**
|
|
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
|
+
* ```
|
|
129
515
|
*/
|
|
130
|
-
declare function
|
|
516
|
+
declare function reduceSet<V, R>(set: ReadonlySet<V>, reducer: (accumulator: R, value: V) => R, initial: R): R;
|
|
131
517
|
|
|
132
518
|
/**
|
|
133
519
|
* Convert a map-like iterable to a regular object.
|
|
134
520
|
*/
|
|
135
521
|
declare function toObjectIterable<K extends string, V>(mapLike: Iterable<[K, V]>): Record<K, V>;
|
|
522
|
+
declare function toObjectIterable<K, V>(mapLike: Iterable<[K, V]>): Record<Extract<K, string> | string, V>;
|
|
136
523
|
|
|
137
|
-
export {
|
|
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,333 +1,295 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { MultiSet } from 'mnemonist';
|
|
1
|
+
import { filter, forEach, map } from 'iter-tools';
|
|
2
|
+
import { entriesOf } from '@bemoje/object';
|
|
4
3
|
|
|
5
4
|
var __defProp = Object.defineProperty;
|
|
6
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
6
|
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
get [Symbol.toStringTag]() {
|
|
19
|
-
return this.constructor.name;
|
|
20
|
-
}
|
|
21
|
-
get target() {
|
|
22
|
-
return this[TARGET_SYMBOL];
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// ../composition/src/inheritProxifiedPrototypeProperty.ts
|
|
27
|
-
function inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, key) {
|
|
28
|
-
const des = Reflect.getOwnPropertyDescriptor(TargetClass.prototype, key);
|
|
29
|
-
if (!des) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
if (des.value && typeof des.value === "function") {
|
|
33
|
-
return Object.defineProperty(ViewerClass.prototype, key, {
|
|
34
|
-
value(...args) {
|
|
35
|
-
return this.target[key](...args);
|
|
36
|
-
},
|
|
37
|
-
writable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
enumerable: false
|
|
40
|
-
});
|
|
41
|
-
} else if (des && !des.get && !des.set) {
|
|
42
|
-
return Object.defineProperty(ViewerClass.prototype, key, {
|
|
43
|
-
get() {
|
|
44
|
-
return this.target[key];
|
|
45
|
-
},
|
|
46
|
-
set: des.writable ? /* @__PURE__ */ __name(function set(value) {
|
|
47
|
-
this.target[key] = value;
|
|
48
|
-
}, "set") : void 0,
|
|
49
|
-
configurable: true,
|
|
50
|
-
enumerable: false
|
|
51
|
-
});
|
|
52
|
-
} else {
|
|
53
|
-
Object.defineProperty(ViewerClass.prototype, key, {
|
|
54
|
-
get: des.get ? function() {
|
|
55
|
-
return this.target[key];
|
|
56
|
-
} : void 0,
|
|
57
|
-
set: des.set ? function(value) {
|
|
58
|
-
this.target[key] = value;
|
|
59
|
-
} : void 0,
|
|
60
|
-
configurable: true,
|
|
61
|
-
enumerable: false
|
|
62
|
-
});
|
|
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);
|
|
14
|
+
}
|
|
15
|
+
index++;
|
|
63
16
|
}
|
|
17
|
+
return result;
|
|
64
18
|
}
|
|
65
|
-
__name(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return k !== "constructor" && k !== Symbol.toStringTag;
|
|
71
|
-
}).filter((k) => {
|
|
72
|
-
return omitKeys ? !omitKeys.includes(k) : true;
|
|
73
|
-
}).forEach((k) => {
|
|
74
|
-
return inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, k);
|
|
75
|
-
});
|
|
76
|
-
return ViewerClass;
|
|
77
|
-
}
|
|
78
|
-
__name(inheritProxifiedPrototype, "inheritProxifiedPrototype");
|
|
79
|
-
|
|
80
|
-
// ../object/src/entriesOf.ts
|
|
81
|
-
function entriesOf(obj) {
|
|
82
|
-
return Object.entries(obj);
|
|
19
|
+
__name(filterArray, "filterArray");
|
|
20
|
+
function filterIterable(collection, predicate) {
|
|
21
|
+
return filter((value) => {
|
|
22
|
+
return predicate(value);
|
|
23
|
+
}, collection);
|
|
83
24
|
}
|
|
84
|
-
__name(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
function defineMethod(obj, key, value, des = {}) {
|
|
88
|
-
return Object.defineProperty(obj, key, {
|
|
89
|
-
value,
|
|
90
|
-
configurable: true,
|
|
91
|
-
writable: true,
|
|
92
|
-
enumerable: false,
|
|
93
|
-
...des
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
__name(defineMethod, "defineMethod");
|
|
97
|
-
|
|
98
|
-
// ../map/src/entriesArray.ts
|
|
99
|
-
function entriesArray(ins) {
|
|
100
|
-
return Array.from(ins);
|
|
25
|
+
__name(filterIterable, "filterIterable");
|
|
26
|
+
function filterIterableEntries(mapLike, predicate) {
|
|
27
|
+
return filter(predicate, mapLike);
|
|
101
28
|
}
|
|
102
|
-
__name(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
29
|
+
__name(filterIterableEntries, "filterIterableEntries");
|
|
30
|
+
function filterIterableKeys(mapLike, predicate) {
|
|
31
|
+
return filter(([k, v]) => {
|
|
32
|
+
return predicate(k, v);
|
|
33
|
+
}, mapLike);
|
|
107
34
|
}
|
|
108
|
-
__name(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (value !== void 0 || map4.has(key)) {
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
value = factory(key, map4);
|
|
117
|
-
map4.set(key, value);
|
|
118
|
-
return value;
|
|
35
|
+
__name(filterIterableKeys, "filterIterableKeys");
|
|
36
|
+
function filterIterableValues(mapLike, predicate) {
|
|
37
|
+
return filter(([k, v]) => {
|
|
38
|
+
return predicate(v, k);
|
|
39
|
+
}, mapLike);
|
|
119
40
|
}
|
|
120
|
-
__name(
|
|
41
|
+
__name(filterIterableValues, "filterIterableValues");
|
|
121
42
|
|
|
122
|
-
//
|
|
123
|
-
function
|
|
124
|
-
|
|
125
|
-
|
|
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);
|
|
49
|
+
}
|
|
126
50
|
}
|
|
127
|
-
return
|
|
128
|
-
}
|
|
129
|
-
__name(mapLoad, "mapLoad");
|
|
130
|
-
|
|
131
|
-
// ../map/src/mapReverse.ts
|
|
132
|
-
function mapReverse(ins) {
|
|
133
|
-
const entries = Array.from(ins).reverse();
|
|
134
|
-
ins.clear();
|
|
135
|
-
mapLoad(ins, entries);
|
|
136
|
-
return ins;
|
|
137
|
-
}
|
|
138
|
-
__name(mapReverse, "mapReverse");
|
|
139
|
-
|
|
140
|
-
// ../map/src/mapUpdate.ts
|
|
141
|
-
function mapUpdate(ins, key, update) {
|
|
142
|
-
const newValue = update(ins.get(key), key, ins);
|
|
143
|
-
ins.set(key, newValue);
|
|
144
|
-
return ins;
|
|
51
|
+
return result;
|
|
145
52
|
}
|
|
146
|
-
__name(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
ins.set(k, v);
|
|
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;
|
|
59
|
+
}
|
|
154
60
|
}
|
|
155
|
-
return
|
|
61
|
+
return result;
|
|
156
62
|
}
|
|
157
|
-
__name(
|
|
63
|
+
__name(filterObject, "filterObject");
|
|
158
64
|
|
|
159
|
-
//
|
|
160
|
-
function
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
ins.set(k, v);
|
|
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);
|
|
71
|
+
}
|
|
167
72
|
}
|
|
168
|
-
return
|
|
73
|
+
return result;
|
|
169
74
|
}
|
|
170
|
-
__name(
|
|
75
|
+
__name(filterSet, "filterSet");
|
|
171
76
|
|
|
172
|
-
//
|
|
173
|
-
function
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
for (const [k, v] of entries) {
|
|
179
|
-
ins.set(k, v);
|
|
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++;
|
|
180
83
|
}
|
|
181
|
-
return ins;
|
|
182
84
|
}
|
|
183
|
-
__name(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
85
|
+
__name(forEachArray, "forEachArray");
|
|
86
|
+
function forEachIterable(collection, callback) {
|
|
87
|
+
forEach((value) => {
|
|
88
|
+
callback(value);
|
|
89
|
+
}, collection);
|
|
188
90
|
}
|
|
189
|
-
__name(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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);
|
|
194
108
|
}
|
|
195
|
-
__name(
|
|
109
|
+
__name(forEachIterableValues, "forEachIterableValues");
|
|
196
110
|
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
111
|
+
// src/forEachMap.ts
|
|
112
|
+
function forEachMap(map5, callback) {
|
|
113
|
+
for (const [key, value] of map5) {
|
|
114
|
+
callback(value, key);
|
|
201
115
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
static fromIterable(entries) {
|
|
209
|
-
return new _ExtMap(entries);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Creates a shallow copy of the map.
|
|
213
|
-
*
|
|
214
|
-
* @returns A new ExtMap instance with the same entries
|
|
215
|
-
*/
|
|
216
|
-
clone() {
|
|
217
|
-
return new _ExtMap(this);
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Converts the map to a JSON-serializable format.
|
|
221
|
-
*
|
|
222
|
-
* @returns Array of key-value pairs suitable for JSON serialization
|
|
223
|
-
*/
|
|
224
|
-
toJSON() {
|
|
225
|
-
return [
|
|
226
|
-
...this.entries()
|
|
227
|
-
];
|
|
116
|
+
}
|
|
117
|
+
__name(forEachMap, "forEachMap");
|
|
118
|
+
function forEachObject(object, callback) {
|
|
119
|
+
for (const [key, value] of entriesOf(object)) {
|
|
120
|
+
callback(value, key);
|
|
228
121
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
[inspect.custom](_depth, _options) {
|
|
237
|
-
return inspect(this.target, {
|
|
238
|
-
breakLength: 50
|
|
239
|
-
});
|
|
122
|
+
}
|
|
123
|
+
__name(forEachObject, "forEachObject");
|
|
124
|
+
|
|
125
|
+
// src/forEachSet.ts
|
|
126
|
+
function forEachSet(set, callback) {
|
|
127
|
+
for (const value of set) {
|
|
128
|
+
callback(value);
|
|
240
129
|
}
|
|
241
|
-
};
|
|
242
|
-
function thisify(fn) {
|
|
243
|
-
return function(...args) {
|
|
244
|
-
return fn(this, ...args);
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
__name(thisify, "thisify");
|
|
248
|
-
function transformReturnValue(fn, transform) {
|
|
249
|
-
return function(...args) {
|
|
250
|
-
const result = fn.apply(this, args);
|
|
251
|
-
return transform(result);
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
__name(transformReturnValue, "transformReturnValue");
|
|
255
|
-
inheritProxifiedPrototype(ExtMap, Map, []);
|
|
256
|
-
defineMethod(ExtMap.prototype, "load", thisify(mapLoad));
|
|
257
|
-
defineMethod(ExtMap.prototype, "sort", thisify(sort));
|
|
258
|
-
defineMethod(ExtMap.prototype, "sortByKeys", thisify(sortByKeys));
|
|
259
|
-
defineMethod(ExtMap.prototype, "sortByValues", thisify(sortByValues));
|
|
260
|
-
defineMethod(ExtMap.prototype, "reverse", thisify(mapReverse));
|
|
261
|
-
defineMethod(ExtMap.prototype, "update", thisify(mapUpdate));
|
|
262
|
-
defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
|
|
263
|
-
defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
|
|
264
|
-
defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
|
|
265
|
-
defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
|
|
266
|
-
defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
|
|
267
|
-
defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
|
|
268
|
-
defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterable));
|
|
269
|
-
defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterable, ExtMap.fromIterable)));
|
|
270
|
-
defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
|
|
271
|
-
defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
|
|
272
|
-
defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterable, ExtMap.fromIterable)));
|
|
273
|
-
function countUniques(input) {
|
|
274
|
-
return new ExtMap(reduce(new MultiSet(), (acc, imp) => {
|
|
275
|
-
return acc.add(imp);
|
|
276
|
-
}, input).multiplicities()).sortByValues((a, b) => {
|
|
277
|
-
return b - a;
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
__name(countUniques, "countUniques");
|
|
281
|
-
function filterIterable(mapLike, predicate) {
|
|
282
|
-
return filter(([k, v]) => {
|
|
283
|
-
return predicate(v, k);
|
|
284
|
-
}, mapLike);
|
|
285
130
|
}
|
|
286
|
-
__name(
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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++;
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
291
142
|
}
|
|
292
|
-
__name(
|
|
293
|
-
function mapIterable(
|
|
294
|
-
return map((
|
|
295
|
-
return
|
|
296
|
-
},
|
|
143
|
+
__name(mapArray, "mapArray");
|
|
144
|
+
function mapIterable(collection, mapper) {
|
|
145
|
+
return map((value) => {
|
|
146
|
+
return mapper(value);
|
|
147
|
+
}, collection);
|
|
297
148
|
}
|
|
298
149
|
__name(mapIterable, "mapIterable");
|
|
299
|
-
function
|
|
150
|
+
function mapIterableEntries(mapLike, mapper) {
|
|
151
|
+
return map(mapper, mapLike);
|
|
152
|
+
}
|
|
153
|
+
__name(mapIterableEntries, "mapIterableEntries");
|
|
154
|
+
function mapIterableKeys(mapLike, mapper) {
|
|
300
155
|
return map(([k, v]) => {
|
|
301
156
|
return [
|
|
302
|
-
|
|
157
|
+
mapper(k, v),
|
|
303
158
|
v
|
|
304
159
|
];
|
|
305
160
|
}, mapLike);
|
|
306
161
|
}
|
|
307
162
|
__name(mapIterableKeys, "mapIterableKeys");
|
|
308
|
-
function mapIterableValues(mapLike,
|
|
163
|
+
function mapIterableValues(mapLike, mapper) {
|
|
309
164
|
return map(([k, v]) => {
|
|
310
165
|
return [
|
|
311
166
|
k,
|
|
312
|
-
|
|
167
|
+
mapper(v, k)
|
|
313
168
|
];
|
|
314
169
|
}, mapLike);
|
|
315
170
|
}
|
|
316
171
|
__name(mapIterableValues, "mapIterableValues");
|
|
317
172
|
|
|
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));
|
|
196
|
+
}
|
|
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++;
|
|
208
|
+
}
|
|
209
|
+
return accumulator;
|
|
210
|
+
}
|
|
211
|
+
__name(reduceArray, "reduceArray");
|
|
212
|
+
|
|
318
213
|
// src/reduceIterable.ts
|
|
319
|
-
function reduceIterable(
|
|
320
|
-
|
|
321
|
-
|
|
214
|
+
function reduceIterable(collection, reducer, initial) {
|
|
215
|
+
let accumulator = initial;
|
|
216
|
+
for (const value of collection) {
|
|
217
|
+
accumulator = reducer(accumulator, value);
|
|
322
218
|
}
|
|
323
|
-
return
|
|
219
|
+
return accumulator;
|
|
324
220
|
}
|
|
325
221
|
__name(reduceIterable, "reduceIterable");
|
|
326
222
|
|
|
223
|
+
// src/reduceIterableEntries.ts
|
|
224
|
+
function reduceIterableEntries(mapLike, reducer, initial) {
|
|
225
|
+
let accumulator = initial;
|
|
226
|
+
for (const entry of mapLike) {
|
|
227
|
+
accumulator = reducer(accumulator, entry);
|
|
228
|
+
}
|
|
229
|
+
return accumulator;
|
|
230
|
+
}
|
|
231
|
+
__name(reduceIterableEntries, "reduceIterableEntries");
|
|
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
|
+
|
|
327
281
|
// src/toObjectIterable.ts
|
|
328
282
|
function toObjectIterable(mapLike) {
|
|
329
|
-
|
|
283
|
+
const object = {};
|
|
284
|
+
for (const [key, value] of mapLike) {
|
|
285
|
+
if (typeof key !== "string" && typeof key !== "number" && typeof key !== "symbol") {
|
|
286
|
+
object[String(key)] = value;
|
|
287
|
+
} else {
|
|
288
|
+
object[key] = value;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return object;
|
|
330
292
|
}
|
|
331
293
|
__name(toObjectIterable, "toObjectIterable");
|
|
332
294
|
|
|
333
|
-
export {
|
|
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.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "iter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -12,11 +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
23
|
"iter-tools": "^7.5.4",
|
|
19
|
-
"
|
|
24
|
+
"@bemoje/object": "^2.1.0"
|
|
20
25
|
},
|
|
21
26
|
"publishConfig": {
|
|
22
27
|
"access": "public"
|