forked from syntax-tree/unist-util-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
151 lines (125 loc) · 3.58 KB
/
test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {filter} from './index.js'
import * as mod from './index.js'
test('filter', () => {
assert.deepEqual(
Object.keys(mod).sort(),
['filter'],
'should expose the public api'
)
})
test('should not traverse into children of filtered out nodes', () => {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Record<string, number>} */
const types = {}
assert.deepEqual(filter(tree, predicate), u('root', [u('leaf', '2')]))
assert.deepEqual(types, {root: 1, node: 1, leaf: 1})
/**
* @param {Node} node
*/
function predicate(node) {
types[node.type] = (types[node.type] || 0) + 1
return node.type !== 'node'
}
})
test('should return `null` if root node is filtered out', () => {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, predicate), null)
function predicate() {
return false
}
})
test('should cascade-remove parent nodes', () => {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, notOne), u('root', [u('leaf', '2')]))
assert.deepEqual(filter(tree, notLeaf), null)
/**
* @param {Node} node
*/
function notOne(node) {
// @ts-expect-error: fine.
return node.value !== '1'
}
/**
* @param {Node} node
*/
function notLeaf(node) {
return node.type !== 'leaf'
}
})
test('should not cascade-remove nodes that were empty initially', () => {
const tree = u('node', [u('node', []), u('node', [u('leaf')])])
assert.deepEqual(filter(tree, 'node'), u('node', [u('node', [])]))
})
test('should call iterator with `index` and `parent` args', () => {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Array<[Node, number|null|undefined, Parent|null|undefined]>} */
const callLog = []
assert.deepEqual(
filter(tree, (a, b, c) => {
callLog.push([a, b, c])
return true
}),
tree
)
assert.deepEqual(callLog, [
[tree, undefined, undefined],
[tree.children[0], 0, tree],
// @ts-expect-error yeah, it exists.
[tree.children[0].children[0], 0, tree.children[0]],
[tree.children[1], 1, tree]
])
})
test('should support type and node tests', () => {
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(filter(tree, 'node'), null)
assert.deepEqual(
filter(tree, {cascade: false}, 'node'),
u('node', [u('node', [])])
)
assert.deepEqual(filter(tree, {cascade: false}, 'leaf'), null)
})
test('opts.cascade', () => {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
assert.deepEqual(
filter(tree, {cascade: true}, predicate),
null,
'opts.cascade = true'
)
assert.deepEqual(
filter(tree, {cascade: false}, predicate),
u('root', [u('node', [])]),
'opts.cascade = false'
)
/**
* @param {Node} node
*/
function predicate(node) {
return node.type !== 'leaf'
}
})
test('example from README', () => {
const tree = u('root', [
u('leaf', '1'),
u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
u('leaf', '4')
])
assert.deepEqual(
filter(tree, predicate),
u('root', [u('node', [u('leaf', '2')]), u('leaf', '4')]),
'example from readme'
)
/**
* @param {Node} node
*/
function predicate(node) {
// @ts-expect-error: fine.
return node.type !== 'leaf' || Number(node.value) % 2 === 0
}
})