npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tree-visit

v0.4.1

Published

A tree traversal library.

Downloads

1,648

Readme

tree-visit

A tree traversal library.

npm install --save tree-visit

OR

yarn add tree-visit

API

The recommended way to use tree-visit is by calling defineTree(getChildren), where getChildren is a function which returns a node's children as an array. The defineTree API returns an object containing every library function with the getChildren option already set.

You may alternately import individual functions, e.g. visit, and pass the { getChildren } option when calling them. Importing individual functions can reduce your bundle size to the bear minimum (though the entire library is small and has 0 dependencies, so this may not be necessary).

Most callback functions, such as getChildren, predicate, onEnter, and onLeave, are passed an IndexPath as the second argument, containing an array of integer indexes that identify that node. The root node is implicitly included in the IndexPath (i.e. there's no 0 first in every IndexPath).


access

Returns a node by its IndexPath.

Type: function access<T>(node: T, indexPath: IndexPath, options: BaseOptions<T>): T

Example

import { access } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

access(rootNode, [1, 0], { getChildren })
// #=> { name: 'd' }

accessPath

Returns an array of each node in an IndexPath.

Type: function accessPath<T>(node: T, indexPath: IndexPath, options: BaseOptions<T>): T

Example

import { accessPath } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

access(rootNode, [1, 0], { getChildren })
// #=> [{ name: 'a', children: [...] }, { name: 'c', children: [...] }, { name: 'd' }]

defineTree

Returns a version of every library function with the getChildren option already set.

This also allows for more concise calls to most functions.

Type: function defineTree<T>(baseOptions: BaseOptions<T>): Tree<T>

Example

import { defineTree } from 'tree-visit'

const getChildren = (node) => node.children || []

const { visit, find } = defineTree({ getChildren })

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

visit(rootNode, (node) => {
  console.log(node)
})
// #=> a, b, c, d

find(rootNode, (node) => node.name === 'd')
// #=> { name: 'd' }

diagram

Generate a diagram of the tree, as a string.

Type: function diagram<T>(node: T, options: DiagramOptions<T>): string

Example

import { diagram } from 'tree-visit'

const getChildren = (node) => node.children || []
const getLabel = (node) => node.name

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

diagram(rootNode, { getChildren, getLabel })
// #=> a
// #=> ├── b
// #=> └── c / d

find

Find a node matching a predicate function.

Type: function find<T>(node: T, options: FindOptions<T>): T | undefined

Example

import { find } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

find(rootNode, { getChildren, predicate: (node) => node.name === 'd' })
// #=> { name: 'd' }

findAll

Find all nodes matching a predicate function.

Type: findAll<T>(node: T, options: FindOptions<T>): T[]

Example

import { findAll } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

findAll(rootNode, { getChildren, predicate: (node) => node.name === 'd' })
// #=> [{ name: 'd' }]

findIndexPath

Find the IndexPath of a node matching a predicate function.

Type: findIndexPath<T>(node: T, options: FindOptions<T>): T[]

Example

import { findIndexPath } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

findIndexPath(rootNode, { getChildren, predicate: (node) => node.name === 'd' })
// #=> [1, 0]

findAllIndexPaths

Find the IndexPath of all nodes matching a predicate function.

Type: findAllIndexPaths<T>(node: T, options: FindOptions<T>): T[]

Example

import { findAllIndexPaths } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

findAllIndexPaths(rootNode, {
  getChildren,
  predicate: (node) => node.name === 'c' || node.name === 'd',
})
// #=> [[1], [1, 0]]

flat

Returns an array containing the root node and all of its descendants.

This is analogous to Array.prototype.flat for flattening arrays.

Type: function flat<T>(node: T, options: BaseOptions<T>): T[]

Example

import { flat } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

flat(rootNode, { getChildren }).map((node) => node.name)
// #=> ['a', 'b', 'c', 'd']

visit

Visit each node in the tree, calling an optional onEnter and onLeave for each.

From onEnter:

  • return nothing or undefined to continue
  • return "skip" to skip the children of that node and the subsequent onLeave
  • return "stop" to end traversal

From onLeave:

  • return nothing or undefined to continue
  • return "stop" to end traversal

Type: function visit<T>(node: T, options: VisitOptions<T>): void

Example

import { visit } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
  name: 'a',
  children: [
    { name: 'b' },
    {
      name: 'c',
      children: [{ name: 'd' }],
    },
  ],
}

visit(rootNode, {
  getChildren,
  onEnter: (node) => {
    console.log(node)
  },
})
// #=> a, b, c, d