-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
57 lines (49 loc) · 1.37 KB
/
utils.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
51
52
53
54
55
56
57
const { FunctionRef, ActorRef, ModuleRef, generateId, getType } = require('./')
/**
* returns the ID of an actor, given the path
* @param {Array} path - indexes of each actor relative to parent
* @example [0, 1, 0] - the first child of the second child of the root actor
* @return {ID}
*/
const actorPathToId = path => path.reduce((parent, curr) => generateId({ parent, nonce: curr }), null)
const toHex = arg => Buffer.isBuffer(arg) ? `0x${arg.toString('hex')}` : arg
const fromHex = arg => typeof arg !== 'string' ? arg : Buffer.from(arg.slice(0, 2) === '0x' ? arg.slice(2) : arg, 'hex')
const toJSON = (arg, verbose = true) => {
switch (getType(arg)) {
case 'elem':
return arg.map(a => toJSON(a, verbose))
case 'id':
case 'func':
case 'mod':
case 'actor':
return arg.toJSON(verbose)
case 'link':
return {
'/': toJSON(arg['/'], verbose)
}
case 'data':
default:
return toHex(arg)
}
}
const fromJSON = arg => {
if (typeof arg === 'object') {
if (Array.isArray(arg)) {
return arg.map(fromJSON)
}
switch (arg.type) {
case 'funcref':
return FunctionRef.fromJSON(arg)
case 'actorref':
return ActorRef.fromJSON(arg)
case 'modref':
return ModuleRef.fromJSON(arg)
}
}
return fromHex(arg)
}
module.exports = {
actorPathToId,
toJSON,
fromJSON
}