-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExpressionRef.ts
50 lines (40 loc) · 1.47 KB
/
ExpressionRef.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
import type Address from './Address';
import LanguageRef from './LanguageRef'
// Expression address + unique Language ID = global expression URL
export default class ExpressionRef {
language: LanguageRef;
expression: Address;
constructor(lang: LanguageRef, expr: Address) {
this.language = lang
this.expression = expr
}
}
// Creates unique expression URI like this:
// expr:Qm3490gfwe489hf34:Qm90ghjoaw4iehioefwe4ort
export function exprRef2String(ref: ExpressionRef): string {
if(ref.language.address === 'did')
return ref.expression.toString()
else
return `${ref.language.address}://${ref.expression}`
}
export function parseExprURL(url: string): ExpressionRef {
const URIregexp = /^([^:^\s]+):\/\/([^\s]+)$/
const URImatches = URIregexp.exec(url)
if(URImatches && URImatches.length == 3) {
const language = URImatches[1]
const expression = URImatches[2]
const languageRef = new LanguageRef()
languageRef.address = language
const ref = new ExpressionRef(languageRef, expression)
return ref
}
const DIDregexp = /^did:([^\s]+)$/
const DIDmatches = DIDregexp.exec(url)
if(DIDmatches && DIDmatches.length == 2) {
const languageRef = new LanguageRef()
languageRef.address = 'did'
const ref = new ExpressionRef(languageRef, url)
return ref
}
throw new Error("Couldn't parse string as expression URL or DID: " + url)
}