-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
82 lines (73 loc) · 1.34 KB
/
examples.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import test from 'ava'
import morph from '../index'
import tree from './helpers/tree'
import {
add,
copy
} from './helpers/functions'
test('map', t => {
function map(root, iteratee) {
return morph(root, iteratee, add)
}
const newTree = map(tree,
(node, context) => ({ depth: context.depth })
)
t.deepEqual(newTree, {
depth: 0,
children: [
{
depth: 1,
children: [
{ depth: 2 },
{ depth: 2 }
]
},
{
depth: 1,
children: [
{ depth: 2 }
]
}
]
})
})
test('pluck', t => {
function pluck(root, key) {
return morph(root, node => ({ [key]: node[key] }), add)
}
const newTree = pluck(tree, 'value')
t.deepEqual(newTree, {
value: 1,
children: [
{
value: 2,
children: [
{ value: 3 },
{ value: 4 }
]
},
{
value: 5,
children: [
{ value: 6 }
]
}
]
})
})
test('filter', t => {
function filter(root, predicate) {
return morph(root,
node => predicate(node) ? copy(node) : null
, add)
}
const newTree = filter(tree, node => 'node' === node.type)
t.deepEqual(newTree, {
type: 'node',
value: 1,
children: [
{ type: 'node', value: 2 },
{ type: 'node', value: 5 }
]
})
})