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

@js-tiny/tree-walker

v0.0.1

Published

A zero-dependency, lazy tree traversal utility for JavaScript and TypeScript.

Readme

@js-tiny/tree-walker

零依赖、惰性求值的 JavaScript/TypeScript 树遍历工具库。

安装

npm install @js-tiny/tree-walker

TreeWalker

将任意嵌套数据结构包装为 for...of 接口。返回带有完整路径和深度信息的类型化 TreeNode 对象。

默认支持普通对象。提供自定义 getChildren 回调函数,可遍历任意树形结构。

基本用法

import { TreeWalker } from '@js-tiny/tree-walker'

const walker = new TreeWalker({
    a: 1,
    b: 2,
    c: {
        d: 3,
        e: 4,
    },
})

for (const node of walker) {
    console.log(node)
}
// { key: 'a', value: 1,          path: ['a'],    depth: 0, isLeaf: true  }
// { key: 'b', value: 2,          path: ['b'],    depth: 0, isLeaf: true  }
// { key: 'c', value: { d, e },   path: ['c'],    depth: 0, isLeaf: false }
// { key: 'd', value: 3,          path: ['c','d'],depth: 1, isLeaf: true  }
// { key: 'e', value: 4,          path: ['c','e'],depth: 1, isLeaf: true  }

遍历策略

for...of.pre() 默认使用前序 DFS。

// 前序 DFS(默认)— 父节点优先于子节点
for (const node of walker.pre()) { ... }
for (const node of walker) { ... }        // 等价

// 后序 DFS — 子节点优先于父节点
for (const node of walker.post()) { ... }

// 广度优先 — 按层级遍历
for (const node of walker.bfs()) { ... }

同一个实例可以多次迭代,使用不同的遍历策略:

const leaves = [...walker].filter(n => n.isLeaf).map(n => n.value)
const paths  = [...walker.bfs()].map(n => n.path.join('.'))

自定义 getChildren

传入 getChildren 回调函数,可以遍历任意树形结构,而不仅限于普通对象。

const categories = {
    id: 1,
    name: 'root',
    children: [
        { id: 2, name: 'clothing' },
        { id: 3, name: 'electronics', children: [
            { id: 4, name: 'phones' },
        ]},
    ],
}

const walker = new TreeWalker(categories, {
    getChildren: (node) =>
        node.children?.map((child, i) => [String(i), child]) ?? null,
})

for (const node of walker) {
    console.log(node.value.name, node.depth)
}
// clothing     0
// electronics  0
// phones       1

API

new TreeWalker(data, options?)

| 参数 | 类型 | 描述 | |---|---|---| | data | V | 根数据值 | | options.getChildren | GetChildren<V> | 可选。返回节点的 [key, child][] 对,如果为叶子节点则返回 null/undefined |

默认 getChildren 通过 Object.entries 将普通(非数组、非null)对象视为分支。其他类型 — 原始类型、数组、null — 均为叶子节点。

TreeNode<V>

type TreeNode<V> = {
    key: string      // 属性名或父节点中的索引
    value: V         // 节点值
    path: string[]    // 从根节点开始的完整路径,如 ['c', 'd']
    depth: number    // 深度,0 = 根节点的直接子节点
    isLeaf: boolean  // 如果 getChildren 返回 null/undefined/[] 则为 true
}

GetChildren<V>

type GetChildren<V> = (value: V) => Array<[string, V]> | null | undefined

License

MIT