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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@idlesummer/traverse

v0.1.3

Published

Declarative depth-first tree traversal with pre-order and post-order hooks.

Readme

@idlesummer/traverse

Declarative depth-first tree traversal with pre-order and post-order hooks.

Background

Kept writing the same tree traversal algorithm by hand every time my projects needed to walk a tree. So I made a simple declarative helper for pre-order and post-order depth-first search where I just pass behavior instead of rewriting the algorithm inline.

Install

npm install @idlesummer/traverse

Usage

import { traverse } from '@idlesummer/traverse'

type Node = {
  name: string
  children?: Node[]
}

const tree: Node = {
  name: 'root',
  children: [
    { name: 'a', children: [{ name: 'a1' }] },
    { name: 'b' },
  ],
}

traverse(tree, {
  visit:  (node) => console.log(`visit ${node.name}`),
  expand: (node) => node.children ?? [],
  leave:  (node) => console.log(`leave ${node.name}`),
})

// visit root, visit a, visit a1, leave a1, leave a, visit b, leave b, leave root

That tree, visualized, with both orders it produces:

root
├─ a
│  └─ a1
└─ b

pre-order  (visit): root → a → a1 → b
post-order (leave): a1 → a → b → root

Stopping and pruning

  • visit or leave returning true stops the entire traversal immediately, wherever it happens to be.
  • expand returning null/undefined also stops the entire traversal immediately — not just that one node's branch.
  • expand returning [] does the opposite: it prunes just that one node (treats it as childless), while the rest of the tree keeps going normally.

So to skip a subtree without stopping everything else, prune it instead of stopping it:

traverse(tree, {
  expand: (node) => node.hidden ? [] : node.children ?? [],
})

See test/traverse.test.ts for the full test suite. Each test has a small diagram of the tree it uses and the order it produces. test/traverse.bench.ts benchmarks traverse() against the naive recursive equivalent.

API

traverse(root, hooks)

type: (root: TNode, hooks: TraverseHooks<TNode>) => void Walks root depth-first, iteratively. Safe for very deep trees, no call-stack recursion. For each node:

  1. visit(node) — called before its children.
  2. expand(node) — returns the node's children.
  3. Children, in the order expand returned them.
  4. leave(node) — called after all of a node's children (and their descendants) are done.

root

type: TNode The tree's root node.

hooks.visit

type: (node: TNode) => unknown Called before a node's children.

hooks.expand

type: (node: TNode) => TNode[] | null | undefined Returns the node's children.

  • Required

hooks.leave

type: (node: TNode) => unknown Called after all of a node's children (and their descendants) are done.

hooks.attach

type: (child: TNode, parent: TNode) => unknown Called once per child, right before it's queued.

Contributors

License

MIT © Nash Luis Maramag