Mercurial > code > home > repos > streamed-graph
comparison src/layout/algorithm.ts @ 122:2e8fa3fec0c8
support joining subjects into wider rows
author | drewp@bigasterisk.com |
---|---|
date | Sun, 20 Mar 2022 14:12:03 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
121:3584f24becf4 | 122:2e8fa3fec0c8 |
---|---|
1 import Immutable from "immutable"; | |
2 | |
3 export function addToValues<K, V>( | |
4 imap: Immutable.Map<K, V[]>, | |
5 key: K, | |
6 newValue: V): Immutable.Map<K, V[]> { | |
7 let cur = imap.get(key, undefined); | |
8 let ret = imap; | |
9 if (cur === undefined) { | |
10 cur = []; | |
11 ret = imap.set(key, cur); | |
12 } | |
13 cur.push(newValue); | |
14 return ret; | |
15 } | |
16 | |
17 export function multiColumnSort<T>( | |
18 elems: T[], | |
19 makeSortCols: (elem: T, index: number) => Array<number | string>): T[] { | |
20 const tagged = elems.map((p, i) => { | |
21 return { | |
22 sort: makeSortCols(p, i), | |
23 val: p | |
24 }; | |
25 }); | |
26 tagged.sort((e1, e2) => { | |
27 let index = 0; | |
28 for (let k1 of e1.sort) { | |
29 const k2 = e2.sort[index]; | |
30 if (!Immutable.is(k1, k2)) { | |
31 if (typeof k1 === "number") { | |
32 if (typeof k2 === "number") { | |
33 return k1 - k2; | |
34 } else { | |
35 throw new Error(`${k1} vs ${k2}`); | |
36 } | |
37 } else { | |
38 return k1.localeCompare(k2 as string); | |
39 } | |
40 } | |
41 index++; | |
42 } | |
43 return 0; | |
44 }); | |
45 return tagged.map((e) => e.val); | |
46 } |