UNPKG

796 BJavaScriptView Raw
1// @flow strict-local
2
3export function unique<T>(array: Array<T>): Array<T> {
4 return [...new Set(array)];
5}
6
7export function objectSortedEntries(obj: {
8 +[string]: mixed
9}): Array<[string, mixed]> {
10 return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
11}
12
13export function objectSortedEntriesDeep(object: {
14 +[string]: mixed
15}): Array<[string, mixed]> {
16 let sortedEntries = objectSortedEntries(object);
17 for (let i = 0; i < sortedEntries.length; i++) {
18 sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
19 }
20 return sortedEntries;
21}
22
23function sortEntry(entry: mixed) {
24 if (Array.isArray(entry)) {
25 return entry.map(sortEntry);
26 }
27
28 if (typeof entry === 'object' && entry != null) {
29 return objectSortedEntriesDeep(entry);
30 }
31
32 return entry;
33}