Mercurial > code > home > repos > streamed-graph
comparison src/suffixLabels.ts @ 15:7ca4ff2088c3
managed to use a newer ts or something, so this includes a bunch of type fixes too
author | drewp@bigasterisk.com |
---|---|
date | Sun, 08 Dec 2019 23:32:12 -0800 |
parents | 1988ad250036 |
children | c751380b70c5 |
comparison
equal
deleted
inserted
replaced
14:497d50f8fe5b | 15:7ca4ff2088c3 |
---|---|
1 import { Term } from 'n3'; | 1 import { Term } from 'n3'; |
2 | 2 |
3 type SuffixesNode = { usedBy: null | string, children: Map<string, SuffixesNode> }; | 3 type SuffixesNode = { usedBy?: string, children: Map<string, SuffixesNode> }; |
4 type DisplayNode = { label: string | null, link?: string }; | 4 type DisplayNode = { label?: string, link?: string }; |
5 class SuffixLabels { | 5 class SuffixLabels { |
6 displayNodes: Map<string, DisplayNode>; | 6 displayNodes: Map<string, DisplayNode>; |
7 usedSuffixes: SuffixesNode; | 7 usedSuffixes: SuffixesNode; |
8 constructor() { | 8 constructor() { |
9 this.displayNodes = new Map(); | 9 this.displayNodes = new Map(); |
10 this.usedSuffixes = { usedBy: null, children: new Map() }; | 10 this.usedSuffixes = { usedBy: undefined, children: new Map() }; |
11 } | 11 } |
12 | 12 |
13 planDisplayForNode(node: Term) { | 13 planDisplayForNode(node: Term) { |
14 const uri = node.value; | 14 const uri = node.value; |
15 this._planDisplayForUri(uri); | 15 this._planDisplayForUri(uri); |
20 return; | 20 return; |
21 } | 21 } |
22 | 22 |
23 const segments = uri.split('/'); | 23 const segments = uri.split('/'); |
24 let curs = this.usedSuffixes; | 24 let curs = this.usedSuffixes; |
25 let label = null; | 25 let label: string | undefined = undefined; |
26 | 26 |
27 for (let i = segments.length - 1; i >= 0; i--) { | 27 for (let i = segments.length - 1; i >= 0; i--) { |
28 const seg = segments[i]; | 28 const seg = segments[i]; |
29 if (curs.usedBy && curs.usedBy != uri) { | 29 if (curs.usedBy && curs.usedBy != uri) { |
30 this._prependClashingUri(curs); | 30 this._prependClashingUri(curs); |
31 } | 31 } |
32 | 32 |
33 if (!curs.children.has(seg)) { | 33 if (!curs.children.has(seg)) { |
34 const child = { usedBy: null, children: new Map() }; | 34 const child: SuffixesNode = { usedBy: undefined, children: new Map() }; |
35 curs.children.set(seg, child); | 35 curs.children.set(seg, child); |
36 | 36 |
37 if (label === null) { | 37 if (label === undefined) { |
38 label = SuffixLabels._tailSegments(uri, segments.length - i); | 38 label = SuffixLabels._tailSegments(uri, segments.length - i); |
39 child.usedBy = uri; | 39 child.usedBy = uri; |
40 } | 40 } |
41 } | 41 } |
42 curs = curs.children.get(seg); | 42 curs = curs.children.get(seg)!; |
43 } | 43 } |
44 this.displayNodes.set(uri, { label: label }); | 44 this.displayNodes.set(uri, { label: label }); |
45 } | 45 } |
46 | 46 |
47 _prependClashingUri(curs: SuffixesNode) { | 47 _prependClashingUri(curs: SuffixesNode) { |
48 // Claim: When a clash is discovered, only 1 uri needs to | 48 // Claim: When a clash is discovered, only 1 uri needs to |
49 // change its length, and there will be only one child node to | 49 // change its length, and there will be only one child node to |
50 // follow, and the clashing uri can be changed to prepend that | 50 // follow, and the clashing uri can be changed to prepend that |
51 // one child (since we'll see it again if that one wasn't | 51 // one child (since we'll see it again if that one wasn't |
52 // enough). | 52 // enough). |
53 const clashNode: DisplayNode = this.displayNodes.get(curs.usedBy); | 53 const clashNode: DisplayNode = this.displayNodes.get(curs.usedBy!)!; |
54 const nextLeftSeg = curs.children.entries().next().value; | 54 const nextLeftSeg = curs.children.entries().next().value; |
55 if (nextLeftSeg[1].usedBy) { | 55 if (nextLeftSeg[1].usedBy) { |
56 throw new Error("unexpected"); | 56 throw new Error("unexpected"); |
57 } | 57 } |
58 | 58 |
59 clashNode.label = nextLeftSeg[0] + '/' + clashNode.label; | 59 clashNode.label = nextLeftSeg[0] + '/' + clashNode.label; |
60 nextLeftSeg[1].usedBy = curs.usedBy; | 60 nextLeftSeg[1].usedBy = curs.usedBy; |
61 curs.usedBy = null; | 61 curs.usedBy = undefined; |
62 | 62 |
63 } | 63 } |
64 | 64 |
65 getLabelForNode(node: string) { | 65 getLabelForNode(node: string) { |
66 return this.displayNodes.get(node).label; | 66 return this.displayNodes.get(node)!.label; |
67 } | 67 } |
68 | 68 |
69 static _tailSegments(uri: string, n: number) { | 69 static _tailSegments(uri: string, n: number) { |
70 let i = uri.length; | 70 let i = uri.length; |
71 for (let rep = 0; rep < n; rep++) { | 71 for (let rep = 0; rep < n; rep++) { |