forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.ts
More file actions
58 lines (53 loc) · 1.4 KB
/
arrays.ts
File metadata and controls
58 lines (53 loc) · 1.4 KB
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
import {assert} from './asserts.ts';
/**
* Returns `arr` as is if none of the elements are `undefined`.
* Otherwise returns a new array with only defined elements in `arr`.
*/
export function defined<T>(arr: (T | undefined)[]): T[] {
// avoid an array copy if possible
let i = arr.findIndex(x => x === undefined);
if (i < 0) {
return arr as T[];
}
const defined: T[] = arr.slice(0, i) as T[];
for (i++; i < arr.length; i++) {
const x = arr[i];
if (x !== undefined) {
defined.push(x);
}
}
return defined;
}
export function areEqual<T>(arr1: readonly T[], arr2: readonly T[]): boolean {
return arr1.length === arr2.length && arr1.every((e, i) => e === arr2[i]);
}
export function zip<T1, T2>(a1: readonly T1[], a2: readonly T2[]): [T1, T2][] {
assert(a1.length === a2.length);
const result: [T1, T2][] = [];
for (let i = 0; i < a1.length; i++) {
result.push([a1[i], a2[i]]);
}
return result;
}
export function last<T>(arr: T[]): T | undefined {
if (arr.length === 0) {
return undefined;
}
return arr[arr.length - 1];
}
export function groupBy<T, K>(
arr: readonly T[],
keyFn: (el: T) => K,
): Map<K, T[]> {
const groups = new Map<K, T[]>();
for (const el of arr) {
const key = keyFn(el);
let group = groups.get(key);
if (group === undefined) {
group = [];
groups.set(key, group);
}
group.push(el);
}
return groups;
}