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

@build-tree/tools

v1.0.8

Published

将扁平数据转换为树状结构

Downloads

29

Readme

@build-tree/tools 工具使用指南

功能概述

@build-tree/tools 是一款基于 TypeScript 开发的实用工具,可以将扁平数据转成树状结构。

安装方式

使用npm安装

npm install @build-tree/tools

使用pnpm安装

pnpm add @build-tree/tools

核心特性

  • 高性能转换‌:采用两次遍历算法,时间复杂度O(n)
  • 灵活配置‌:支持自定义ID和父ID字段名
  • 安全警告‌:自动检测超大子树并发出性能警告
  • 零依赖‌:不增加项目体积
  • 类型安全‌:完整的TypeScript支持(需配合.d.ts文件使用)

基本用法

import { buildTree } from '@build-tree/tools';

const flatData = [
  { id: 1, name: '根节点' },
  { id: 2, parentId: 1, name: '子节点1' },
  { id: 3, parentId: 1, name: '子节点2' }
];

const tree = buildTree(flatData);
/* 输出:
[
  {
    id: 1,
    name: '根节点',
    children: [
      { id: 2, parentId: 1, name: '子节点1', children: [] },
      { id: 3, parentId: 1, name: '子节点2', children: [] }
    ]
  }
]
*/

高级用法

自定义字段名

const tree = buildTree(data, {
  idKey: 'uid',
  parentIdKey: 'pid'
});

处理大型数据集

// 调整警告阈值
const tree = buildTree(largeData, {
  maxChildrenWarning: 5000
});

// 禁用警告
const tree = buildTree(largeData, {
  maxChildrenWarning: Infinity
});

配置选项

| 参数 | 类型 | 默认值 | 描述 | | -------------------- | ------ | ---------- | ---------------------------- | | idKey | string | 'id' | 节点唯一标识字段名 | | parentIdKey | string | 'parentId' | 父节点引用字段名 | | maxChildrenWarning | number | 1000 | 触发性能警告的子节点数量阈值 |

性能优化建议

合理设置阈值‌:根据实际场景调整maxChildrenWarning