-
Notifications
You must be signed in to change notification settings - Fork 0
/
extender.ts
87 lines (77 loc) · 3 KB
/
extender.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
// type TypedConstructor<T> = [T] extends [number] ?
// | Uint8ArrayConstructor
// | Uint16ArrayConstructor
// | Uint32ArrayConstructor
// | Uint8ClampedArrayConstructor
// | Int8ArrayConstructor
// | Int16ArrayConstructor
// | Int32ArrayConstructor
// : never;
// type BigTypedConstructor<T> = [T] extends [bigint] ?
// | BigUint64ArrayConstructor
// | BigInt64ArrayConstructor
// : never;
type IndependentConstructors =
| StringConstructor
| ArrayConstructor
| SetConstructor;
export type Constructor<T> = [T] extends [[unknown, unknown]]
? MapConstructor | IndependentConstructors
: IndependentConstructors;
// type TypedCollection<Data, Constructor> = [Data] extends [number]
// ? [Constructor] extends [Uint8ArrayConstructor] ? Uint8Array
// : [Constructor] extends [Uint16ArrayConstructor] ? Uint16Array
// : [Constructor] extends [Uint32ArrayConstructor] ? Uint32Array
// : [Constructor] extends [Uint8ClampedArrayConstructor] ? Uint32Array
// : [Constructor] extends [Int8ArrayConstructor] ? Int8Array
// : [Constructor] extends [Int16ArrayConstructor] ? Int16Array
// : [Constructor] extends [Int32ArrayConstructor] ? Int32Array
// : never
// : never;
// type BigTypedCollection<Data, Constructor> = [Data] extends [bigint]
// ? [Constructor] extends [BigUint64ArrayConstructor] ? BigUint64Array
// : [Constructor] extends [BigInt64ArrayConstructor] ? BigInt64Array
// : never
// : never;
type MapCollection<Data, Constructor> = [Data] extends [[infer K, infer V]]
? [Constructor] extends [MapConstructor] ? Map<K, V> : never
: never;
type StringCollection<Data, Constructor> = [Data] extends [string]
? [Constructor] extends [StringConstructor] ? string : never
: never;
export type Collection<Data, Constructor> = [Constructor] extends
[ArrayConstructor] ? Data[]
: [Constructor] extends [SetConstructor] ? Set<Data>
:
| StringCollection<Data, Constructor>
| MapCollection<Data, Constructor>;
type Extender<Data, Collection> = (c: Collection, d: Data) => Collection;
export default function <
T,
>(
constructor: Constructor<T>,
): [
Collection<T, typeof constructor>,
Extender<T, Collection<T, typeof constructor>>,
] {
type Collect = Collection<T, typeof constructor>;
// deno-lint-ignore no-explicit-any
const collection = new (constructor as any)() as Collect;
let extender: Extender<T, Collect>;
if (collection instanceof Map) {
// deno-lint-ignore no-explicit-any
extender = (c, v) => ((c as any).set((v as any)[0], (v as any)[1]), c);
} else if (collection instanceof Set) {
// deno-lint-ignore no-explicit-any
extender = (c, v) => ((c as any).add(v), c);
} else if (collection instanceof String) {
// deno-lint-ignore no-explicit-any
extender = (c, v) => c as any + v as any;
} else if (Array.isArray(collection)) {
// deno-lint-ignore no-explicit-any
extender = (c, v) => ((c as any).push(v), c);
} else {
throw new TypeError("Iter: Invalid type to collect into");
}
return [collection, extender];
}