-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinks.ts
74 lines (63 loc) · 1.91 KB
/
Links.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type Expression from './Expression';
import type ExpressionRef from './ExpressionRef'
export default class Link {
source: string;
target: string;
predicate?: string;
constructor(obj) {
this.source = obj.source ? obj.source : ''
this.target = obj.target ? obj.target : ''
this.predicate = obj.predicate ? obj.predicate : ''
}
}
export class LinkQuery {
source?: string;
target?: string;
predicate?: string;
constructor(obj: object) {
if(obj) {
// @ts-ignore
this.source = obj.source
// @ts-ignore
this.predicate = obj.predicate
// @ts-ignore
this.target = obj.target
}
}
isMatch(l: Link): boolean {
if(this.source)
if(this.source !== l.source)
return false
if(this.predicate)
if(this.predicate !== l.predicate)
return false
if(this.target)
if(this.target !== l.target)
return false
return true
}
}
export function linkEqual(l1: Expression, l2: Expression): boolean {
return l1.author.did == l2.author.did &&
l1.timestamp == l2.timestamp &&
// @ts-ignore
l1.data.source == l2.data.source &&
// @ts-ignore
l1.data.predicate == l2.data.predicate &&
// @ts-ignore
l1.data.target == l2.data.target
}
export function isLink(l: any): boolean {
return l && l.source && l.target
}
export function hashLinkExpression(link: Expression): number {
const mash = JSON.stringify(link.data, Object.keys(link.data).sort()) +
JSON.stringify(link.author) + link.timestamp
let hash = 0, i, chr;
for (i = 0; i < mash.length; i++) {
chr = mash.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}