-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
50 lines (41 loc) · 1.28 KB
/
util.js
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
// @flow
export function checkProps(obj : Object, ...props : Array<string>) {
let prop = props.shift()
let cursor = obj
while (prop !== undefined) {
if (!(prop in cursor) || cursor[prop] === undefined)
throw `Property: ${prop} is not in the object`
prop = props.shift()
cursor = cursor[prop]
}
}
export function filterHashUrl(x : string) {
if (x.indexOf('/') === -1)
throw "The input hash_url should be in this format: `xx/xx/xx/xx/xx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`"
const start = x[0] === '/' ? 1 : 0
const end = x.slice(-1) === '/' ? -1 : x.length
return x.slice(start, end)
}
export class OpStep {
main_fn: any => any
next_nodes: Array<OpStep>
constructor(main_fn : any => any, ...next_nodes : Array<OpStep>) {
this.main_fn = main_fn
this.next_nodes = next_nodes
}
run(...args : Array<any>) {
const result = this.main_fn.apply(this, args)
if (typeof result === 'boolean') {
if (result)
return this.next_nodes[0] && this.next_nodes[0].run()
else
return this.next_nodes[1] && this.next_nodes[1].run()
}
if (result instanceof Promise) {
return result.then(x => this.next_nodes[0] && this.next_nodes[0].run(x))
.catch(err => {
throw `OpStep error caught: ${err}`
})
}
}
}