-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.js
41 lines (34 loc) · 938 Bytes
/
types.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
// @flow
export type TezJSON =
| number
| string
| boolean
| void
| {[string]: TezJSON}
| Array<TezJSON>
export function safeProp(x : TezJSON, ...props : Array<string | number>) {
let result = undefined
let prop
let curr = x
while (true) {
prop = props.shift()
if (prop === undefined)
break
if (curr instanceof Object && !(curr instanceof Array)) {
result = curr[prop]
curr = curr[prop]
} else if (curr instanceof Array) {
const prop_index = parseInt(prop)
if (isNaN(prop_index))
return undefined
result = curr[prop_index]
curr = curr[prop_index]
} else {
return undefined
}
}
return result
}
export type RPCFunc = (url: string, data?: TezJSON, method: 'POST' | 'GET') => Promise<TezJSON>
export type GetRPCFunc = (url: string, data?: TezJSON) => Promise<TezJSON>
export type PostRPCFunc = (url: string, data: TezJSON) => Promise<TezJSON>