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

@anys/find-path-in-tree

v1.0.2

Published

在树形结构中查找符合条件的节点路径和目标节点

Readme

find-path-in-tree

在树形结构中查找符合条件的节点路径和目标节点的 TypeScript 工具函数。

安装

npm install find-path-in-tree
# 或
pnpm add find-path-in-tree
# 或
yarn add find-path-in-tree

使用方法

基本用法

import { findPathInTree } from 'find-path-in-tree'

interface TreeNode {
  id: number
  name: string
  children?: TreeNode[]
}

const tree: TreeNode[] = [
  {
    id: 1,
    name: 'root',
    children: [
      {
        id: 2,
        name: 'child1',
        children: [
          { id: 3, name: 'grandchild1' },
          { id: 4, name: 'grandchild2' }
        ]
      },
      { id: 5, name: 'child2' }
    ]
  }
]

// 查找 id 为 4 的节点
const result = findPathInTree(tree, node => node.id === 4)

console.log(result.target) // { id: 4, name: 'grandchild2' }
console.log(result.path)   // [root, child1, grandchild2] 完整路径

自定义子节点字段名

interface CustomNode {
  id: number
  name: string
  items?: CustomNode[] // 使用 'items' 而不是 'children'
}

const customTree: CustomNode[] = [
  {
    id: 1,
    name: 'root',
    items: [
      { id: 2, name: 'child' }
    ]
  }
]

// 指定子节点字段名为 'items'
const result = findPathInTree(customTree, node => node.id === 2, 'items')

复杂查找条件

// 使用复杂的查找条件
const result = findPathInTree(
  tree,
  node => node.name.includes('child') && node.id > 3
)

// 处理包含 null/undefined 的树
const treeWithNulls = [
  null,
  {
    id: 1,
    name: 'root',
    children: [undefined, { id: 2, name: 'child' }, null]
  },
  undefined
]

const result2 = findPathInTree(
  treeWithNulls,
  node => node?.id === 2
)

// 支持原始数据类型
const stringTree = ['root', 'child1', 'target']
const result3 = findPathInTree(
  stringTree,
  node => node === 'target'
)

API

findPathInTree<T>(tree, predicate, childrenKey?)

参数

  • tree: T[] - 树形结构数组
  • predicate: (node: T) => boolean - 判断节点是否符合条件的函数
  • childrenKey?: string - 子节点字段名,默认为 'children'

返回值

{
  path: T[]     // 从根节点到目标节点的完整路径数组
  target?: T    // 找到的目标节点,如果未找到则为 undefined
}

特性

  • ✅ 完整的 TypeScript 类型支持
  • ✅ 支持自定义子节点字段名
  • ✅ 返回完整的节点路径
  • ✅ 深度优先搜索算法
  • ✅ 零依赖
  • ✅ 支持 ESM 和 CommonJS
  • ✅ 支持任意数据类型(不限于对象)
  • ✅ 安全处理 null 和 undefined 节点
  • ✅ 容错处理无效的子节点数据

开发

# 安装依赖
pnpm install

# 开发模式
pnpm run dev

# 构建
pnpm run build

# 测试
pnpm run test

# 代码检查
pnpm run lint

# 代码格式化
pnpm run format

许可证

MIT