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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@aardpro/tree

v1.0.0

Published

把包含父指针属性的平坦数组与嵌套结构的树形数组进行互相转化的函数库。 A function library that converts a flat array with parent pointer attributes into a nested array and vice versa.

Readme

@aardpro/tree

类型导出 Types

export type TreeNode = Record<string, unknown>;
export type TreeNodes = TreeNode[];
export type PointerNode = Record<string, unknown>;
export type PointerNodes = PointerNode[];
export type WalkTreeCallback<T extends object> = (
  node: T,
  controller: AbortController,
  level: number
) => Promise<void> | void;

说明:

  • TreeNode/TreeNodes 用于表示嵌套树形结构的数据;
  • PointerNode/PointerNodes 用于表示带父指针的平坦数组;
  • WalkTreeCallback 为遍历回调的类型,包含节点、可中止控制器与层级。

安装 Installation

pnpm

pnpm i @aardpro/tree

npm

npm i @aardpro/tree

yarn

yarn add @aardpro/tree

使用 Usage

walk 遍历函数

declare function walk<T extends object>(
  treeArr: T[],
  childProperty: string,
  callback: WalkTreeCallback<T>
): Promise<void>;
import { walk } from "@aardpro/tree";

const rawData = [
  {
    id: 99,
    name: "root",
    children: [
      {
        id: 88,
        name: "child1",
        children: [
          {
            id: 77,
            name: "child2",
            children: [
              {
                id: 3,
                name: "child3",
              },
            ],
          },
        ],
      },
    ],
  },
];

// 使用同步回调
walk(rawData, "children", (node, ctrl) => {
  if (node.id === 3) {
    console.log("we found it:", node);
    ctrl.abort();
  }
});

// 使用异步回调, 等待遍历结果
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
  console.log("start walk --------------");
  await walk(rawData, "children", async (node, ctrl) => {
    if (node.id === 77) {
      await sleep(1000);
      console.log("we found it: ", node);
      ctrl.abort();
    }
  });
  console.log("-------------- walk end");
}

flatten 扁平化函数

declare function flatten<T extends PointerNodes>(
  treeArr1: T[],
  id?: string,
  pid?: string,
  childProperty?: string
): Promise<T[]>;
import { flatten } from "@aardpro/tree";

const flatData = await flatten(rawData, 'id', 'pid', 'children')

tree 做树函数

把带有父指针的平坦数组转化为嵌套数组
convert a array with all elements including pointer to parent into nesting tree array

declare function tree<T extends PointerNodes>(
  flatArr1: T[],
  id?: string,
  pid?: string,
  childProperty?: string
): T[];
import { tree } from "@aardpro/tree";

const flatData = [
  {
    value: 99,
    name: "root",
    parent: null,
  },
  {
    value: 88,
    name: "child1",
    parent: 99,
  },
  {
    value: 77,
    name: "child2",
    parent: 88,
  },
  {
    value: 3,
    name: "child3",
    parent: 77,
  },
];

const treeData = tree(flatData, "value", "parent", "children");

说明:

  • 根节点判定:当元素的 pid 为非真值(例如 nullundefined0""false 等)时,将其视为根节点。
  • flatten 在缺少 id 时会生成一个 ID;在缺少 pid 时会设置为 null 并为其子节点补齐父指针。

version logs

  • 1.1.0 type WalkTreeCallback has changed
    # now
    {
      node: TreeNode,
      controller: AbortController,
      level: number
    }