-
Notifications
You must be signed in to change notification settings - Fork 1
/
object-adapter.js
46 lines (31 loc) · 1.07 KB
/
object-adapter.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
'use strict'
const is = require( '@mojule/is' )
const $createRawNode = value => ({ value, children: [] })
const ObjectTreeAdapter = ( node, state ) => {
const $isNode = rawNode =>
is.object( rawNode ) && node.isValue( rawNode.value ) &&
is.array( rawNode.children )
const $isValue = value => !node.isNode( value )
const getChildren = () => state.node.children.slice()
const getValue = () => state.node.value
const setValue = value => state.node.value = value
const remove = rawChild => {
const index = state.node.children.indexOf( rawChild )
state.node.children.splice( index, 1 )
return rawChild
}
const add = ( rawChild, rawReference ) => {
if( is.undefined( rawReference ) ){
state.node.children.push( rawChild )
} else {
const referenceIndex = state.node.children.indexOf( rawReference )
state.node.children.splice( referenceIndex, 0, rawChild )
}
return rawChild
}
return {
$isNode, $isValue, $createRawNode,
getChildren, getValue, setValue, remove, add
}
}
module.exports = ObjectTreeAdapter