{"_id":"@anys/find-path-in-tree","_rev":"2-b427f67e9403ead881391772b22b0760","name":"@anys/find-path-in-tree","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.1":{"name":"@anys/find-path-in-tree","version":"1.0.1","keywords":["tree","search","path","find","typescript","utility"],"author":{"name":"Mrcxt","email":"mrcxt@foxmail.com"},"license":"MIT","_id":"@anys/find-path-in-tree@1.0.1","maintainers":[{"name":"cixiangtao","email":"mrcxt@foxmail.com"}],"homepage":"https://github.com/Mrcxt/find-path-in-tree#readme","bugs":{"url":"https://github.com/Mrcxt/find-path-in-tree/issues"},"dist":{"shasum":"8de8d6d286c05f2871d3811598bbc57c78b8d532","tarball":"https://registry.npmjs.org/@anys/find-path-in-tree/-/find-path-in-tree-1.0.1.tgz","fileCount":8,"integrity":"sha512-IdFKxy/Qd74nIOp0Ep1hA9DA0STy+3SRiqna+IAqKNVFGpb9oOehbFzx4cJldqryqM77JuKbWjfM0Re3t2+y4A==","signatures":[{"sig":"MEUCIDQB+R/JuxHhkdrs4xKfMs0nPacuzUDU6CAO43ZFcR1DAiEAn+j5y8DLAZX5AamtGDJZvqDdSDWJ/LZzEEwyy3ev1II=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10723},"main":"dist/index.js","types":"dist/index.d.ts","module":"dist/index.mjs","engines":{"node":">=20"},"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"gitHead":"dc630dedd67f8f62aba335e556e5531a02fbc341","scripts":{"dev":"tsup --watch","lint":"eslint src --ext .ts","test":"vitest --watch=false","build":"tsup","format":"prettier --write src/**/*.ts","release":"release-it","lint:fix":"eslint src --ext .ts --fix","format:check":"prettier --check src/**/*.ts","test:coverage":"vitest --coverage","prepublishOnly":"pnpm run build"},"_npmUser":{"name":"cixiangtao","email":"mrcxt@foxmail.com"},"repository":{"url":"git+https://github.com/Mrcxt/find-path-in-tree.git","type":"git"},"_npmVersion":"10.8.2","description":"在树形结构中查找符合条件的节点路径和目标节点","directories":{},"_nodeVersion":"20.18.3","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"^8.0.0","eslint":"^8.0.0","vitest":"^1.0.0","prettier":"^3.0.0","release-it":"^16.0.0","typescript":"^5.0.0","@types/node":"^20.0.0","@vitest/coverage-v8":"^1.0.0","eslint-config-prettier":"^9.0.0","eslint-plugin-prettier":"^5.0.0","@typescript-eslint/parser":"^6.0.0","@typescript-eslint/eslint-plugin":"^6.0.0"},"_npmOperationalInternal":{"tmp":"tmp/find-path-in-tree_1.0.1_1753869273340_0.4872623240040328","host":"s3://npm-registry-packages-npm-production"}},"1.0.2":{"name":"@anys/find-path-in-tree","version":"1.0.2","description":"在树形结构中查找符合条件的节点路径和目标节点","main":"dist/index.js","module":"dist/index.mjs","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"scripts":{"build":"tsup","dev":"tsup --watch","test":"vitest --watch=false","test:coverage":"vitest --coverage","lint":"eslint src --ext .ts","lint:fix":"eslint src --ext .ts --fix","format":"prettier --write src/**/*.ts","format:check":"prettier --check src/**/*.ts","release":"release-it","prepublishOnly":"pnpm run build"},"keywords":["tree","search","path","find","typescript","utility"],"author":{"name":"Mrcxt","email":"mrcxt@foxmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/Mrcxt/find-path-in-tree.git"},"bugs":{"url":"https://github.com/Mrcxt/find-path-in-tree/issues"},"homepage":"https://github.com/Mrcxt/find-path-in-tree#readme","devDependencies":{"@types/node":"^20.0.0","@typescript-eslint/eslint-plugin":"^6.0.0","@typescript-eslint/parser":"^6.0.0","@vitest/coverage-v8":"^1.0.0","eslint":"^8.0.0","eslint-config-prettier":"^9.0.0","eslint-plugin-prettier":"^5.0.0","prettier":"^3.0.0","release-it":"^16.0.0","tsup":"^8.0.0","typescript":"^5.0.0","vitest":"^1.0.0"},"publishConfig":{"access":"public"},"engines":{"node":">=20"},"_id":"@anys/find-path-in-tree@1.0.2","gitHead":"a86f680d04bdfd4d909d37b0de7291b537955211","_nodeVersion":"20.18.3","_npmVersion":"10.8.2","dist":{"integrity":"sha512-ZJFXc5Gf0uHhRzYby/WXi1dp4Ex+3/HO/ToXoPwvEz91fqsJZLC75wfpuUIb0PeFtgA3WUdCjZSKv3FFoL9B0A==","shasum":"93b9220d78eabcb60c467c86847e2910eb4628f2","tarball":"https://registry.npmjs.org/@anys/find-path-in-tree/-/find-path-in-tree-1.0.2.tgz","fileCount":8,"unpackedSize":12176,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCICDhnr+CUAk/FfPNs0QIxfHq0F/ar/aYihXz7/sOn/tTAiEAkkyhfvtWfdEq7wgd56Duwmwts+tmxqERHajnkmnOwbo="}]},"_npmUser":{"name":"cixiangtao","email":"mrcxt@foxmail.com"},"directories":{},"maintainers":[{"name":"cixiangtao","email":"mrcxt@foxmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/find-path-in-tree_1.0.2_1753869950796_0.8879302314135105"},"_hasShrinkwrap":false}},"time":{"created":"2025-07-30T09:54:33.260Z","modified":"2025-07-30T10:05:51.130Z","1.0.1":"2025-07-30T09:54:33.563Z","1.0.2":"2025-07-30T10:05:50.961Z"},"bugs":{"url":"https://github.com/Mrcxt/find-path-in-tree/issues"},"author":{"name":"Mrcxt","email":"mrcxt@foxmail.com"},"license":"MIT","homepage":"https://github.com/Mrcxt/find-path-in-tree#readme","keywords":["tree","search","path","find","typescript","utility"],"repository":{"type":"git","url":"git+https://github.com/Mrcxt/find-path-in-tree.git"},"description":"在树形结构中查找符合条件的节点路径和目标节点","maintainers":[{"name":"cixiangtao","email":"mrcxt@foxmail.com"}],"readme":"# find-path-in-tree\n\n在树形结构中查找符合条件的节点路径和目标节点的 TypeScript 工具函数。\n\n## 安装\n\n```bash\nnpm install find-path-in-tree\n# 或\npnpm add find-path-in-tree\n# 或\nyarn add find-path-in-tree\n```\n\n## 使用方法\n\n### 基本用法\n\n```typescript\nimport { findPathInTree } from 'find-path-in-tree'\n\ninterface TreeNode {\n  id: number\n  name: string\n  children?: TreeNode[]\n}\n\nconst tree: TreeNode[] = [\n  {\n    id: 1,\n    name: 'root',\n    children: [\n      {\n        id: 2,\n        name: 'child1',\n        children: [\n          { id: 3, name: 'grandchild1' },\n          { id: 4, name: 'grandchild2' }\n        ]\n      },\n      { id: 5, name: 'child2' }\n    ]\n  }\n]\n\n// 查找 id 为 4 的节点\nconst result = findPathInTree(tree, node => node.id === 4)\n\nconsole.log(result.target) // { id: 4, name: 'grandchild2' }\nconsole.log(result.path)   // [root, child1, grandchild2] 完整路径\n```\n\n### 自定义子节点字段名\n\n```typescript\ninterface CustomNode {\n  id: number\n  name: string\n  items?: CustomNode[] // 使用 'items' 而不是 'children'\n}\n\nconst customTree: CustomNode[] = [\n  {\n    id: 1,\n    name: 'root',\n    items: [\n      { id: 2, name: 'child' }\n    ]\n  }\n]\n\n// 指定子节点字段名为 'items'\nconst result = findPathInTree(customTree, node => node.id === 2, 'items')\n```\n\n### 复杂查找条件\n\n```typescript\n// 使用复杂的查找条件\nconst result = findPathInTree(\n  tree,\n  node => node.name.includes('child') && node.id > 3\n)\n\n// 处理包含 null/undefined 的树\nconst treeWithNulls = [\n  null,\n  {\n    id: 1,\n    name: 'root',\n    children: [undefined, { id: 2, name: 'child' }, null]\n  },\n  undefined\n]\n\nconst result2 = findPathInTree(\n  treeWithNulls,\n  node => node?.id === 2\n)\n\n// 支持原始数据类型\nconst stringTree = ['root', 'child1', 'target']\nconst result3 = findPathInTree(\n  stringTree,\n  node => node === 'target'\n)\n```\n\n## API\n\n### `findPathInTree<T>(tree, predicate, childrenKey?)`\n\n#### 参数\n\n- `tree: T[]` - 树形结构数组\n- `predicate: (node: T) => boolean` - 判断节点是否符合条件的函数\n- `childrenKey?: string` - 子节点字段名，默认为 `'children'`\n\n#### 返回值\n\n```typescript\n{\n  path: T[]     // 从根节点到目标节点的完整路径数组\n  target?: T    // 找到的目标节点，如果未找到则为 undefined\n}\n```\n\n#### 特性\n\n- ✅ 完整的 TypeScript 类型支持\n- ✅ 支持自定义子节点字段名\n- ✅ 返回完整的节点路径\n- ✅ 深度优先搜索算法\n- ✅ 零依赖\n- ✅ 支持 ESM 和 CommonJS\n- ✅ 支持任意数据类型（不限于对象）\n- ✅ 安全处理 null 和 undefined 节点\n- ✅ 容错处理无效的子节点数据\n\n## 开发\n\n```bash\n# 安装依赖\npnpm install\n\n# 开发模式\npnpm run dev\n\n# 构建\npnpm run build\n\n# 测试\npnpm run test\n\n# 代码检查\npnpm run lint\n\n# 代码格式化\npnpm run format\n```\n\n## 许可证\n\nMIT","readmeFilename":"README.md"}