@byloth/core 1.3.0-rc.2 → 1.3.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.
@@ -1,384 +0,0 @@
1
- export function aggregate<V>(elements: Iterable<V>)
2
- {
3
- type Iteratee<K> = (element: V, index: number) => K;
4
-
5
- function byKey<K extends string | number | symbol>(iteratee: Iteratee<K>)
6
- {
7
- function countToMap()
8
- {
9
- const counts = new Map<K, number>();
10
-
11
- let index = 0;
12
- for (const element of elements)
13
- {
14
- const key = iteratee(element, index);
15
-
16
- if (counts.has(key))
17
- {
18
- counts.set(key, counts.get(key)! + 1);
19
- }
20
- else
21
- {
22
- counts.set(key, 1);
23
- }
24
-
25
- index += 1;
26
- }
27
-
28
- return counts;
29
- }
30
- function countToDict()
31
- {
32
- const mapCounts = countToMap();
33
- const dictCounts = { } as Record<K, number>;
34
-
35
- for (const [key, _count] of mapCounts.entries())
36
- {
37
- dictCounts[key] = _count;
38
- }
39
-
40
- return dictCounts;
41
- }
42
- function count()
43
- {
44
- return Array.from(countToMap().values());
45
- }
46
-
47
- function groupToMap()
48
- {
49
- const groups = new Map<K, V[]>();
50
-
51
- let index = 0;
52
- for (const element of elements)
53
- {
54
- const key = iteratee(element, index);
55
-
56
- if (groups.has(key))
57
- {
58
- groups.get(key)!
59
- .push(element);
60
- }
61
- else
62
- {
63
- groups.set(key, [element]);
64
- }
65
-
66
- index += 1;
67
- }
68
-
69
- return groups;
70
- }
71
- function groupToDict()
72
- {
73
- const mapGroups = groupToMap();
74
- const dictGroup = { } as Record<K, V[]>;
75
-
76
- for (const [key, _group] of mapGroups.entries())
77
- {
78
- dictGroup[key] = _group;
79
- }
80
-
81
- return dictGroup;
82
- }
83
- function group()
84
- {
85
- return Array.from(groupToMap().values());
86
- }
87
-
88
- type Reducer<R = V> = (key: K, group: R | undefined, element: V, index: number) => R;
89
-
90
- function reduceToMap<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
91
- {
92
- const counts = new Map<K, number>();
93
- const groups = new Map<K, R>();
94
-
95
- let index = 0;
96
- for (const element of elements)
97
- {
98
- let _count = 0;
99
- let _group = initialValue;
100
-
101
- const key = iteratee(element, index);
102
- if (groups.has(key))
103
- {
104
- _count = counts.get(key)!;
105
- _group = groups.get(key)!;
106
- }
107
-
108
- _group = reducer(key, _group, element, _count);
109
-
110
- counts.set(key, _count + 1);
111
- groups.set(key, _group);
112
-
113
- index += 1;
114
- }
115
-
116
- return groups;
117
- }
118
- function reduceToDict<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
119
- {
120
- const mapGroups = reduceToMap(reducer, initialValue);
121
- const dictGroup = { } as Record<K, R>;
122
-
123
- for (const [key, _group] of mapGroups.entries())
124
- {
125
- dictGroup[key] = _group;
126
- }
127
-
128
- return dictGroup;
129
- }
130
- function reduce<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
131
- {
132
- return Array.from(reduceToMap(reducer, initialValue).values());
133
- }
134
-
135
- async function asyncReduceToMap<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
136
- {
137
- const awaitedGroups = new Map<K, R>();
138
- const asyncGroups = reduceToMap(reducer, initialValue);
139
-
140
- for (const [key, asyncGroup] of asyncGroups.entries())
141
- {
142
- awaitedGroups.set(key, await asyncGroup);
143
- }
144
-
145
- return awaitedGroups;
146
- }
147
- async function asyncReduceToDict<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
148
- {
149
- const mapGroups = reduceToMap(reducer, initialValue);
150
- const dictGroup = { } as Record<K, R>;
151
-
152
- for (const [key, _group] of mapGroups.entries())
153
- {
154
- dictGroup[key] = await _group;
155
- }
156
-
157
- return dictGroup;
158
- }
159
- function asyncReduce<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
160
- {
161
- return Promise.all(reduce(reducer, initialValue));
162
- }
163
-
164
- return {
165
- count,
166
- countToMap,
167
- countToDict,
168
- group,
169
- groupToMap,
170
- groupToDict,
171
- reduce,
172
- reduceToMap,
173
- reduceToDict,
174
- asyncReduce,
175
- asyncReduceToMap,
176
- asyncReduceToDict
177
- };
178
- }
179
- function byAsyncKey<K extends string | number | symbol>(iteratee: Iteratee<Promise<K>>)
180
- {
181
- async function countToMap()
182
- {
183
- const counts = new Map<K, number>();
184
-
185
- let index = 0;
186
- for (const element of elements)
187
- {
188
- const key = await iteratee(element, index);
189
-
190
- if (counts.has(key))
191
- {
192
- counts.set(key, counts.get(key)! + 1);
193
- }
194
- else
195
- {
196
- counts.set(key, 1);
197
- }
198
-
199
- index += 1;
200
- }
201
-
202
- return counts;
203
- }
204
- async function countToDict()
205
- {
206
- const mapCounts = await countToMap();
207
- const dictCounts = { } as Record<K, number>;
208
-
209
- for (const [key, _count] of mapCounts.entries())
210
- {
211
- dictCounts[key] = _count;
212
- }
213
-
214
- return dictCounts;
215
- }
216
- async function count()
217
- {
218
- const counts = await countToMap();
219
-
220
- return Array.from(counts.values());
221
- }
222
-
223
- async function groupToMap()
224
- {
225
- const groups = new Map<K, V[]>();
226
-
227
- let index = 0;
228
- for (const element of elements)
229
- {
230
- const key = await iteratee(element, index);
231
-
232
- if (groups.has(key))
233
- {
234
- groups.get(key)!
235
- .push(element);
236
- }
237
- else
238
- {
239
- groups.set(key, [element]);
240
- }
241
-
242
- index += 1;
243
- }
244
-
245
- return groups;
246
- }
247
- async function groupToDict()
248
- {
249
- const mapGroups = await groupToMap();
250
- const dictGroup = { } as Record<K, V[]>;
251
-
252
- for (const [key, _group] of mapGroups.entries())
253
- {
254
- dictGroup[key] = _group;
255
- }
256
-
257
- return dictGroup;
258
- }
259
- async function group()
260
- {
261
- const groups = await groupToMap();
262
-
263
- return Array.from(groups.values());
264
- }
265
-
266
- type Reducer<R = V> = (key: K, group: R | undefined, element: V, index: number) => R;
267
-
268
- async function reduceToMap<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
269
- {
270
- const counts = new Map<K, number>();
271
- const groups = new Map<K, R>();
272
-
273
- let index = 0;
274
- for (const element of elements)
275
- {
276
- let _count = 0;
277
- let _group = initialValue;
278
-
279
- const key = await iteratee(element, index);
280
- if (groups.has(key))
281
- {
282
- _count = counts.get(key)!;
283
- _group = groups.get(key)!;
284
- }
285
-
286
- _group = reducer(key, _group, element, _count);
287
-
288
- counts.set(key, _count + 1);
289
- groups.set(key, _group);
290
-
291
- index += 1;
292
- }
293
-
294
- return groups;
295
- }
296
- async function reduceToDict<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
297
- {
298
- const mapGroups = await reduceToMap(reducer, initialValue);
299
- const dictGroup = { } as Record<K, R>;
300
-
301
- for (const [key, _group] of mapGroups.entries())
302
- {
303
- dictGroup[key] = _group;
304
- }
305
-
306
- return dictGroup;
307
- }
308
- async function reduce<R = V>(reducer: Reducer<R>, initialValue?: R | undefined)
309
- {
310
- const groups = await reduceToMap(reducer, initialValue);
311
-
312
- return Array.from(groups.values());
313
- }
314
-
315
- async function asyncReduceToMap<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
316
- {
317
- const awaitedGroups = new Map<K, R>();
318
- const asyncGroups = await reduceToMap(reducer, initialValue);
319
-
320
- for (const [key, asyncGroup] of asyncGroups.entries())
321
- {
322
- awaitedGroups.set(key, await asyncGroup);
323
- }
324
-
325
- return awaitedGroups;
326
- }
327
- async function asyncReduceToDict<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
328
- {
329
- const mapGroups = await reduceToMap(reducer, initialValue);
330
- const dictGroup = { } as Record<K, R>;
331
-
332
- for (const [key, _group] of mapGroups.entries())
333
- {
334
- dictGroup[key] = await _group;
335
- }
336
-
337
- return dictGroup;
338
- }
339
- async function asyncReduce<R = V>(reducer: Reducer<Promise<R>>, initialValue?: Promise<R> | undefined)
340
- {
341
- const groups = await reduce(reducer, initialValue);
342
-
343
- return Promise.all(groups);
344
- }
345
-
346
- return {
347
- count,
348
- countToMap,
349
- countToDict,
350
- group,
351
- groupToMap,
352
- groupToDict,
353
- reduce,
354
- reduceToMap,
355
- reduceToDict,
356
- asyncReduce,
357
- asyncReduceToMap,
358
- asyncReduceToDict
359
- };
360
- }
361
-
362
- type Mapper<R> = (element: V, index: number) => R;
363
-
364
- function map<R>(mapper: Mapper<R>)
365
- {
366
- const mapping: R[] = [];
367
-
368
- let index = 0;
369
- for (const element of elements)
370
- {
371
- mapping.push(mapper(element, index));
372
-
373
- index += 1;
374
- }
375
-
376
- return mapping;
377
- }
378
- async function asyncMap<R>(mapper: Mapper<Promise<R>>)
379
- {
380
- return await Promise.all(map(mapper));
381
- }
382
-
383
- return { byKey, byAsyncKey, map, asyncMap };
384
- }