import traverse from '../src'
test('stop', () => {
let visits = 0
traverse('abcdefghij'.split('')).forEach(function(node) {
if (typeof node === 'string') {
visits++
if (node === 'e') this.stop()
}
})
expect(visits).toEqual(5)
})
test('stopMap', () => {
const s = traverse('abcdefghij'.split(''))
.map(function(node) {
if (typeof node === 'string') {
if (node === 'e') this.stop()
return node.toUpperCase()
}
})
.join('')
expect(s).toEqual('ABCDEfghij')
})
test('stopReduce', () => {
const obj = {
a: [4, 5],
b: [6, [7, 8, 9]],
}
const xs = traverse(obj).reduce(function(acc, node) {
if (this.isLeaf) {
if (node === 7) this.stop()
else acc.push(node)
}
return acc
}, [])
expect(xs).toEqual([4, 5, 6])
})