/
groupBy.ts
154 lines (140 loc) · 4.18 KB
/
groupBy.ts
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { AsyncFunctionException } from "./_internal/error";
import { isAsyncIterable, isIterable, isPromise } from "./_internal/utils";
import reduce from "./reduce";
import type Cast from "./types/Cast";
import type Equals from "./types/Equals";
import type { GetKeyOf } from "./types/GetKeyOf";
import type IterableInfer from "./types/IterableInfer";
import type iterableInfer from "./types/IterableInfer";
import type Key from "./types/Key";
import type ReturnValueType from "./types/ReturnValueType";
/**
* Splits Iterable/AsyncIterable into sets, grouped by the result of running each value through `f`.
*
* @example
* ```ts
* const given = [
* { category: "clothes", desc: "good" },
* { category: "pants", desc: "bad" },
* { category: "shoes", desc: "not bad" },
* { category: "shoes", desc: "great" },
* { category: "pants", desc: "good" },
* ];
*
* groupBy((a) => a.category, given);
* // {
* // clothes: [{ category: "clothes", desc: "good" }],
* // pants: [
* // { category: "pants", desc: "bad" },
* // { category: "pants", desc: "good" },
* // ],
* // shoes: [
* // { category: "shoes", desc: "not bad" },
* // { category: "shoes", desc: "great" },
* // ],
* // };
* ```
*
* {@link https://codesandbox.io/s/fxts-groupby-v8q3b | Try It}
*/
function groupBy<A extends Key>(
f: (a: A) => A,
iterable: Iterable<A>,
): { [K in A]: K[] };
function groupBy<A extends Key>(
f: (a: A) => A | Promise<A>,
iterable: AsyncIterable<A>,
): Promise<{ [K in A]: K[] }>;
function groupBy<A extends object, B extends Key & A[keyof A]>(
f: (a: A) => B,
iterable: Iterable<A>,
): {
[K in B]: (A & { [K2 in GetKeyOf<A, B>]: K })[];
};
function groupBy<A extends object, B extends Key & A[keyof A]>(
f: (a: A) => B | Promise<B>,
iterable: AsyncIterable<A>,
): Promise<{
[K in B]: (A & { [K2 in GetKeyOf<A, B>]: K })[];
}>;
function groupBy<
I extends Iterable<unknown> | AsyncIterable<unknown>,
F extends (a: IterableInfer<I>) => any,
>(
f: F,
): (iterable: I) => ReturnValueType<
I,
Equals<Awaited<ReturnType<F>>, IterableInfer<I>> extends 1
? {
[key1 in Awaited<ReturnType<F>>]: key1[];
}
: {
[key1 in Awaited<ReturnType<F>>]: (IterableInfer<I> & {
[key2 in GetKeyOf<Cast<IterableInfer<I>, object>, key1>]: key1;
})[];
}
>;
function groupBy<A extends Key, B extends Iterable<A> | AsyncIterable<A>>(
f: (a: A) => A | Promise<A>,
): (iterable: B) => ReturnValueType<B, { [K in A]: K[] }>;
function groupBy<A, B extends Key>(
f: (a: A) => B,
iterable: Iterable<A>,
): { [K in B]: A[] };
function groupBy<A, B extends Key>(
f: (a: A) => B | Promise<B>,
iterable: AsyncIterable<A>,
): Promise<{ [K in B]: A[] }>;
function groupBy<
A extends Iterable<unknown> | AsyncIterable<unknown>,
B extends Key,
>(
f: (a: IterableInfer<A>) => B | Promise<B>,
): (iterable: A) => ReturnValueType<A, { [K in B]: IterableInfer<A>[] }>;
function groupBy<
A extends Iterable<unknown> | AsyncIterable<unknown>,
B extends Key,
>(
f: (a: IterableInfer<A>) => B | Promise<B>,
iterable?: A,
):
| { [K in B]: IterableInfer<A>[] }
| Promise<{ [K in B]: IterableInfer<A>[] }>
| ((iterable: A) => ReturnValueType<A, { [K in B]: IterableInfer<A>[] }>) {
if (iterable === undefined) {
return (
iterable: A,
): ReturnValueType<A, { [K in B]: IterableInfer<A>[] }> => {
return groupBy(f, iterable as any) as ReturnValueType<
A,
{ [K in B]: IterableInfer<A>[] }
>;
};
}
const obj = {} as { [K in B]: IterableInfer<A>[] };
if (isIterable<IterableInfer<A>>(iterable)) {
return reduce(
(group, a) => {
const key = f(a);
if (isPromise(key)) {
throw new AsyncFunctionException();
}
return (group[key] || (group[key] = [])).push(a), group;
},
obj,
iterable,
);
}
if (isAsyncIterable<iterableInfer<A>>(iterable)) {
return reduce(
async (group, a) => {
const key = await f(a);
return (group[key] || (group[key] = [])).push(a), group;
},
obj,
iterable,
);
}
throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
}
export default groupBy;