{"_id":"@aardpro/tree","name":"@aardpro/tree","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@aardpro/tree","version":"1.0.0","description":"把包含父指针属性的平坦数组与嵌套结构的树形数组进行互相转化的函数库。 A function library that converts a flat array with parent pointer attributes into a nested array and vice versa.","module":"./index.js","exports":{".":{"import":"./index.js","require":"./index.cjs","types":"./index.d.ts"}},"author":{"name":"aardpro"},"license":"MIT","bugs":{"url":"https://github.com/aardpro/tree/issues"},"homepage":"https://github.com/aardpro/tree#readme","type":"module","private":false,"publishConfig":{"access":"public"},"_id":"@aardpro/tree@1.0.0","gitHead":"a595e3a88350feed4c1f0c903175af6d2b032428","types":"./index.d.ts","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-p0uLBfo6y4+K0mK8Rp8zOiwVnZck//fkl0YLcpOuO8pnBveQ5xfXAqIjdZicElGJGUykneXsjCSLKmzHdXQPYw==","shasum":"5bc561df93b15b24644d931ddc07d06866c7bc0a","tarball":"https://registry.npmjs.org/@aardpro/tree/-/tree-1.0.0.tgz","fileCount":5,"unpackedSize":43706,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQC6JbyURsyUH5UHbZvQkFT/x0bhEcg4VR/dZDiORaXeNQIgaSPqTCCAT1uyhYuAqOK9ezThf1LFMU3QFIdM58EHbjA="}]},"_npmUser":{"name":"aardpro","email":"chileehong@outlook.com"},"directories":{},"maintainers":[{"name":"aardpro","email":"chileehong@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/tree_1.0.0_1765078805506_0.9803075844983509"},"_hasShrinkwrap":false}},"time":{"created":"2025-12-07T03:40:05.431Z","1.0.0":"2025-12-07T03:40:05.800Z","modified":"2025-12-07T03:40:06.118Z"},"maintainers":[{"name":"aardpro","email":"chileehong@outlook.com"}],"description":"把包含父指针属性的平坦数组与嵌套结构的树形数组进行互相转化的函数库。 A function library that converts a flat array with parent pointer attributes into a nested array and vice versa.","homepage":"https://github.com/aardpro/tree#readme","author":{"name":"aardpro"},"bugs":{"url":"https://github.com/aardpro/tree/issues"},"license":"MIT","readme":"# @aardpro/tree\r\n\r\n<div>处理树形数据的函数，包括<span style=\"color:red\">打平</span>，<span style=\"color:red\">做树</span>，<span style=\"color:red\">遍历</span>等方法</div>\r\n<div>tree-shaped array helpers, including <span style=\"color:red\">flattening</span>, <span style=\"color:red\">making trees</span>, <span style=\"color:red\">traversing</span></div>\r\n\r\n# 类型导出 Types\r\n\r\n```ts\r\nexport type TreeNode = Record<string, unknown>;\r\nexport type TreeNodes = TreeNode[];\r\nexport type PointerNode = Record<string, unknown>;\r\nexport type PointerNodes = PointerNode[];\r\nexport type WalkTreeCallback<T extends object> = (\r\n  node: T,\r\n  controller: AbortController,\r\n  level: number\r\n) => Promise<void> | void;\r\n```\r\n\r\n说明：\r\n- `TreeNode/TreeNodes` 用于表示嵌套树形结构的数据；\r\n- `PointerNode/PointerNodes` 用于表示带父指针的平坦数组；\r\n- `WalkTreeCallback` 为遍历回调的类型，包含节点、可中止控制器与层级。\r\n\r\n# 安装 Installation\r\n\r\n## pnpm\r\n\r\n> pnpm i @aardpro/tree\r\n\r\n## npm\r\n\r\n> npm i @aardpro/tree\r\n\r\n## yarn\r\n\r\n> yarn add @aardpro/tree\r\n\r\n# 使用 Usage\r\n\r\n## walk 遍历函数\r\n\r\n```ts\r\ndeclare function walk<T extends object>(\r\n  treeArr: T[],\r\n  childProperty: string,\r\n  callback: WalkTreeCallback<T>\r\n): Promise<void>;\r\n```\r\n\r\n```js\r\nimport { walk } from \"@aardpro/tree\";\r\n\r\nconst rawData = [\r\n  {\r\n    id: 99,\r\n    name: \"root\",\r\n    children: [\r\n      {\r\n        id: 88,\r\n        name: \"child1\",\r\n        children: [\r\n          {\r\n            id: 77,\r\n            name: \"child2\",\r\n            children: [\r\n              {\r\n                id: 3,\r\n                name: \"child3\",\r\n              },\r\n            ],\r\n          },\r\n        ],\r\n      },\r\n    ],\r\n  },\r\n];\r\n\r\n// 使用同步回调\r\nwalk(rawData, \"children\", (node, ctrl) => {\r\n  if (node.id === 3) {\r\n    console.log(\"we found it:\", node);\r\n    ctrl.abort();\r\n  }\r\n});\r\n\r\n// 使用异步回调, 等待遍历结果\r\nconst sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));\r\n\r\nasync function main() {\r\n  console.log(\"start walk --------------\");\r\n  await walk(rawData, \"children\", async (node, ctrl) => {\r\n    if (node.id === 77) {\r\n      await sleep(1000);\r\n      console.log(\"we found it: \", node);\r\n      ctrl.abort();\r\n    }\r\n  });\r\n  console.log(\"-------------- walk end\");\r\n}\r\n```\r\n\r\n## flatten 扁平化函数\r\n\r\n```ts\r\ndeclare function flatten<T extends PointerNodes>(\r\n  treeArr1: T[],\r\n  id?: string,\r\n  pid?: string,\r\n  childProperty?: string\r\n): Promise<T[]>;\r\n```\r\n\r\n```js\r\nimport { flatten } from \"@aardpro/tree\";\r\n\r\nconst flatData = await flatten(rawData, 'id', 'pid', 'children')\r\n```\r\n\r\n## tree 做树函数\r\n\r\n把带有父指针的平坦数组转化为嵌套数组  \r\nconvert a array with all elements including pointer to parent into nesting tree array\r\n\r\n```ts\r\ndeclare function tree<T extends PointerNodes>(\r\n  flatArr1: T[],\r\n  id?: string,\r\n  pid?: string,\r\n  childProperty?: string\r\n): T[];\r\n```\r\n\r\n```js\r\nimport { tree } from \"@aardpro/tree\";\r\n\r\nconst flatData = [\r\n  {\r\n    value: 99,\r\n    name: \"root\",\r\n    parent: null,\r\n  },\r\n  {\r\n    value: 88,\r\n    name: \"child1\",\r\n    parent: 99,\r\n  },\r\n  {\r\n    value: 77,\r\n    name: \"child2\",\r\n    parent: 88,\r\n  },\r\n  {\r\n    value: 3,\r\n    name: \"child3\",\r\n    parent: 77,\r\n  },\r\n];\r\n\r\nconst treeData = tree(flatData, \"value\", \"parent\", \"children\");\r\n```\r\n\r\n说明：\r\n- 根节点判定：当元素的 `pid` 为非真值（例如 `null`、`undefined`、`0`、`\"\"`、`false` 等）时，将其视为根节点。\r\n- `flatten` 在缺少 `id` 时会生成一个 ID；在缺少 `pid` 时会设置为 `null` 并为其子节点补齐父指针。\r\n\r\n## version logs\r\n- 1.1.0 type WalkTreeCallback has changed\r\n  ```\r\n  # now\r\n  {\r\n    node: TreeNode,\r\n    controller: AbortController,\r\n    level: number\r\n  }\r\n  ```\r\n","readmeFilename":"readme.MD","_rev":"1-05ec08b5e88076c76db50306a1e6d294"}