forked from firebase/firebase-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.ts
148 lines (136 loc) · 4.66 KB
/
functional.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
import { LeafElems } from "./metaprogramming";
/**
* Flattens an object so that the return value's keys are the path
* to a value in the source object. E.g. flattenObject({the: {answer: 42}})
* returns {"the.answser": 42}
* @param obj An object to be flattened
* @return An array where values come from obj and keys are the path in obj to that value.
*/
export function* flattenObject<T extends object>(obj: T): Generator<[string, unknown]> {
function* helper<V extends object>(path: string[], obj: V): Generator<[string, unknown]> {
for (const [k, v] of Object.entries(obj)) {
if (typeof v !== "object" || v === null) {
yield [[...path, k].join("."), v];
} else {
// Object.entries loses type info, so we must cast
yield* helper([...path, k], v);
}
}
}
yield* helper([], obj);
}
/**
* Yields each non-array element recursively in arr.
* Useful for for-of loops.
* [...flatten([[[1]], [2], 3])] = [1, 2, 3]
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function* flattenArray<T extends unknown[]>(arr: T): Generator<LeafElems<T>> {
for (const val of arr) {
if (Array.isArray(val)) {
yield* flattenArray(val);
} else {
yield val as LeafElems<T>;
}
}
}
/** Shorthand for flattenObject. */
export function flatten<T extends object>(obj: T): Generator<[string, string]>;
/** Shorthand for flattenArray. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function flatten<T extends unknown[]>(arr: T): Generator<LeafElems<T>>;
/** Flattens an object or array. */
export function flatten<T extends unknown[] | object>(objOrArr: T): unknown {
if (Array.isArray(objOrArr)) {
return flattenArray(objOrArr);
} else {
return flattenObject(objOrArr);
}
}
/**
* Used with reduce to flatten in place.
* Due to the quirks of TypeScript, callers must pass [] as the
* second argument to reduce.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function reduceFlat<T = any>(accum: T[] | undefined, next: unknown): T[] {
return [...(accum || []), ...(flatten([next]) as Generator<T>)];
}
/**
* Yields each element from left and right in tandem
* [...zip([1, 2, 3], ['a', 'b', 'c'])] = [[1, 'a], [2, 'b'], [3, 'c']]
*/
export function* zip<T, V>(left: T[], right: V[]): Generator<[T, V]> {
if (left.length !== right.length) {
throw new Error("Cannot zip between two lists of differen lengths");
}
for (let i = 0; i < left.length; i++) {
yield [left[i], right[i]];
}
}
/**
* Utility to zip in another array from map.
* [1, 2].map(zipIn(['a', 'b'])) = [[1, 'a'], [2, 'b']]
*/
export const zipIn =
<T, V>(other: V[]) =>
(elem: T, ndx: number): [T, V] => {
return [elem, other[ndx]];
};
/** Used with type guards to guarantee that all cases have been covered. */
export function assertExhaustive(val: never, message?: string): never {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(message || `Never has a value (${val}).`);
}
/**
* Utility to partition an array into two based on predicate's truthiness for each element.
* Returns a Array containing two Array<T>. The first array contains all elements that returned true,
* the second contains all elements that returned false.
*/
export function partition<T>(arr: T[], predicate: (elem: T) => boolean): [T[], T[]] {
return arr.reduce<[T[], T[]]>(
(acc, elem) => {
acc[predicate(elem) ? 0 : 1].push(elem);
return acc;
},
[[], []],
);
}
/**
* Utility to partition a Record into two based on predicate's truthiness for each element.
* Returns a Array containing two Record<string, T>. The first array contains all elements that returned true,
* the second contains all elements that returned false.
*/
export function partitionRecord<T>(
rec: Record<string, T>,
predicate: (key: string, val: T) => boolean,
): [Record<string, T>, Record<string, T>] {
return Object.entries(rec).reduce<[Record<string, T>, Record<string, T>]>(
(acc, [key, val]) => {
acc[predicate(key, val) ? 0 : 1][key] = val;
return acc;
},
[{}, {}],
);
}
/**
* Create a map of transformed values for all keys.
*/
export function mapObject<T, V>(
input: Record<string, T>,
transform: (t: T) => V,
): Record<string, V> {
const result: Record<string, V> = {};
for (const [k, v] of Object.entries(input)) {
result[k] = transform(v);
}
return result;
}
export const nullsafeVisitor =
<First, Rest extends unknown[], Ret>(func: (first: First, ...rest: Rest) => Ret, ...rest: Rest) =>
(first: First | null): Ret | null => {
if (first === null) {
return null;
}
return func(first, ...rest);
};