1 |
|
2 |
|
3 | export function unique<T>(array: Array<T>): Array<T> {
|
4 | return [...new Set(array)];
|
5 | }
|
6 |
|
7 | export function flatMap<T, U>(
|
8 | array: Array<T>,
|
9 | projectFn: (T, number, Array<T>) => Array<U>,
|
10 | ): Array<U> {
|
11 | let out = [];
|
12 |
|
13 | for (let arr of array.map(projectFn)) {
|
14 | out.push(...arr);
|
15 | }
|
16 | return out;
|
17 | }
|
18 |
|
19 | export function objectSortedEntries(obj: {
|
20 | +[string]: mixed,
|
21 | ...,
|
22 | }): Array<[string, mixed]> {
|
23 | return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
|
24 | }
|
25 |
|
26 | export function objectSortedEntriesDeep(object: {
|
27 | +[string]: mixed,
|
28 | ...,
|
29 | }): Array<[string, mixed]> {
|
30 | let sortedEntries = objectSortedEntries(object);
|
31 | for (let i = 0; i < sortedEntries.length; i++) {
|
32 | sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
|
33 | }
|
34 | return sortedEntries;
|
35 | }
|
36 |
|
37 | function sortEntry(entry: mixed) {
|
38 | if (Array.isArray(entry)) {
|
39 | return entry.map(sortEntry);
|
40 | }
|
41 |
|
42 | if (typeof entry === 'object' && entry != null) {
|
43 | return objectSortedEntriesDeep(entry);
|
44 | }
|
45 |
|
46 | return entry;
|
47 | }
|
48 |
|
49 | export function setDifference<T>(a: Set<T>, b: Set<T>): Set<T> {
|
50 | let difference = new Set();
|
51 | for (let e of a) {
|
52 | if (!b.has(e)) {
|
53 | difference.add(e);
|
54 | }
|
55 | }
|
56 | return difference;
|
57 | }
|