Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-traverse   js

Repository URL to install this package:

Version: 6.0.1 

/ __tests__ / stop.ts

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])
})