annotate src/suffixLabels.ts @ 11:1988ad250036

suffixLabels test runswith 'inv test'
author drewp@localhost
date Fri, 06 Dec 2019 16:41:19 -0800
parents src/suffixLabels.js@a7ba8627a7b6
children 7ca4ff2088c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
1 import { Term } from 'n3';
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
2
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
3 type SuffixesNode = { usedBy: null | string, children: Map<string, SuffixesNode> };
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
4 type DisplayNode = { label: string | null, link?: string };
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
5 class SuffixLabels {
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
6 displayNodes: Map<string, DisplayNode>;
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
7 usedSuffixes: SuffixesNode;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
8 constructor() {
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
9 this.displayNodes = new Map();
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
10 this.usedSuffixes = { usedBy: null, children: new Map() };
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
11 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
12
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
13 planDisplayForNode(node: Term) {
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
14 const uri = node.value;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
15 this._planDisplayForUri(uri);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
16 };
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
17
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
18 _planDisplayForUri(uri: string) {
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
19 if (this.displayNodes.has(uri)) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
20 return;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
21 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
22
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
23 const segments = uri.split('/');
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
24 let curs = this.usedSuffixes;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
25 let label = null;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
26
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
27 for (let i = segments.length - 1; i >= 0; i--) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
28 const seg = segments[i];
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
29 if (curs.usedBy && curs.usedBy != uri) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
30 this._prependClashingUri(curs);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
31 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
32
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
33 if (!curs.children.has(seg)) {
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
34 const child = { usedBy: null, children: new Map() };
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
35 curs.children.set(seg, child);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
36
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
37 if (label === null) {
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
38 label = SuffixLabels._tailSegments(uri, segments.length - i);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
39 child.usedBy = uri;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
40 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
41 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
42 curs = curs.children.get(seg);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
43 }
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
44 this.displayNodes.set(uri, { label: label });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
45 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
46
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
47 _prependClashingUri(curs: SuffixesNode) {
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
48 // Claim: When a clash is discovered, only 1 uri needs to
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
49 // change its length, and there will be only one child node to
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
50 // follow, and the clashing uri can be changed to prepend that
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
51 // one child (since we'll see it again if that one wasn't
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
52 // enough).
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
53 const clashNode: DisplayNode = this.displayNodes.get(curs.usedBy);
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
54 const nextLeftSeg = curs.children.entries().next().value;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
55 if (nextLeftSeg[1].usedBy) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
56 throw new Error("unexpected");
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
57 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
58
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
59 clashNode.label = nextLeftSeg[0] + '/' + clashNode.label;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
60 nextLeftSeg[1].usedBy = curs.usedBy;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
61 curs.usedBy = null;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
62
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
63 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
64
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
65 getLabelForNode(node: string) {
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
66 return this.displayNodes.get(node).label;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
67 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
68
11
1988ad250036 suffixLabels test runswith 'inv test'
drewp@localhost
parents: 3
diff changeset
69 static _tailSegments(uri: string, n: number) {
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
70 let i = uri.length;
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
71 for (let rep = 0; rep < n; rep++) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
72 i = uri.lastIndexOf('/', i - 1);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
73 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
74 return uri.substr(i + 1);
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
75 }
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
76 };
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
77
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
78 export { SuffixLabels }