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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shencom/utils-tree

v1.3.1

Published

树形数据结构相关工具方法

Downloads

41

Readme

@shencom/utils-tree

树形数据结构相关工具方法

Install

pnpm add @shencom/utils

# or

pnpm add @shencom/utils-tree

Basic Usage

import { TreeFindNode, TreeToArray } from '@shencom/utils';
// import { TreeFindNode, TreeToArray } from '@shencom/utils-tree';

Methods

TreeToArray

  • 说明: 转数组
  • 类型: (tree: T, options?: TreeToArrayOptions): AnyToArray<T>
  • 参数:
    • tree - 数据
    • options - 配置属性
    • options.children - 子节点映射字段,默认: children
    • options.keepChildren - 是否保留完整子节点数据,默认: false
  • 示例:
    TreeToArray(tree);
    TreeToArray(tree, { children: 'child' });
    TreeToArray(tree, { children: 'child', keepChildren: true });

TreeFindNode

  • 说明: 查找树形数据中的某个节点

  • 类型: (tree: T, id: string, maps?: Omit<TreeNodeOptions, 'pid'>): Flatten<T> | null

  • 参数:

    • tree - 数据
    • id - 节点 id
    • maps - 配置属性
    • maps.id - 节点 id 映射字段,默认: id
    • maps.children - 子节点映射字段,默认: children
  • 示例:

    TreeFindNode(tree, '1');
    TreeFindNode(tree, '1', { id: 'id', children: 'child' });

TreeFindParentNodes

  • 说明: 查找树形数据中的某个节点的所有父节点,返回数据中包含自身
  • 类型: (tree: T, id: string, maps?: TreeNodeOptions, keepChildren?: boolean): AnyToArray<T>
  • 参数:
    • tree - 数据
    • id - 节点 id
    • maps - 配置属性
    • maps.id - 节点 id 映射字段,默认: id
    • maps.children - 子节点映射字段,默认: children
    • maps.pid - 父节点 id 映射字段,默认: pid
    • keepChildren - 是否保留完整子节点数据,默认: false
  • 示例:
    TreeFindParentNodes(tree, '1');
    TreeFindParentNodes(tree, '1', { id: 'id', children: 'child', pid: 'pid' });
    TreeFindParentNodes(tree, '1', { id: 'id', children: 'child', pid: 'pid' }, true);

TreeFindParentByKey

  • 说明: 通过树形数据中的某个 key 的值,查找所有的父级节点,返回数据中包含自身
  • 类型: (tree: T, props: ParentByKeyProps, keepChildren?: boolean): AnyToArray<T>
  • 参数:
    • tree - 数据
    • props - 配置属性
    • props.key - 要查找的 key
    • props.value - 要查找的 key 的值
    • props.id - 节点 id 映射字段,默认: id
    • props.children - 子节点映射字段,默认: children
    • props.pid - 父节点 id 映射字段,默认: pid
    • keepChildren - 是否保留完整子节点数据,默认: false
  • 示例:
    TreeFindParentByKey(tree, { key: 'name', value: '1' });
    TreeFindParentByKey(tree, { key: 'name', value: '1', id: 'id', pid: 'pid', children: 'child' });
    TreeFindParentByKey(
      tree,
      { key: 'name', value: '1', id: 'id', pid: 'pid', children: 'child' },
      true,
    );

TreeFindParentIds

  • 说明: 查找树形数据中的某个节点的所有父节点 id,返回数据中包含自身
  • 类型: (tree: TreeType, id: string, maps?: TreeNodeOptions): string[]
  • 参数:
    • tree - 数据
    • id - 节点 id
    • maps - 配置属性
    • maps.id - 节点 id 映射字段,默认: id
    • maps.children - 子节点映射字段,默认: children
    • maps.pid - 父节点 id 映射字段,默认: pid
  • 示例:
    TreeFindParentIds(tree, '1');
    TreeFindParentIds(tree, '1', { id: 'id', children: 'child', pid: 'pid' });

TreeMap

  • 说明: 处理树形结构每一项的数据
  • 类型: (tree: T, handler: TreeCallbackfn<Flatten<T>>, maps?: Pick<TreeNodeOptions, 'children'>): T
  • 参数:
    • tree - 数据
    • handler - 处理函数
    • maps - 配置属性
    • maps.children - 子节点映射字段,默认: children
  • 示例:
    TreeMap(tree, (node) => ({ ...node, title: node.name }));
    TreeMap(tree, (node) => ({ ...node, title: node.name }), { children: 'child' });

TreeForEach

  • 说明: 遍历树形结构每一项的数据
  • 类型: (tree: T, handler: (node: T) => void, maps?: Pick<TreeNodeOptions, 'children'>): void
  • 参数:
    • tree - 数据
    • handler - 处理函数
    • maps - 配置属性
    • maps.children - 子节点映射字段,默认: children
  • 示例:
    TreeForEach(tree, (node) => {
      // ...
    });
    TreeForEach(
      tree,
      (node) => {
        // ..
      },
      { children: 'child' },
    );

TreeMapOption

  • 说明: 对树形结构中每一项的进行映射
  • 类型: (tree: T, maps: Record<string, string>, childKey?: string) => AnyToArray<T>
  • 参数:
    • tree - 数据
    • maps - 映射关系
    • childKey - 子节点字段,默认: children
  • 示例:
    TreeMapOption(tree, { id: 'ID' });
    TreeMapOption(tree, { id: 'ID' }, 'child');

TreeFilter

  • 说明: 过滤树形结构中的每一项的数据
  • 类型: (tree: T, condition: (node: Flatten<T>) => boolean, maps?: Pick<TreeNodeOptions, 'children'>): T | T[] | null
  • 参数:
    • tree - 数据
    • condition - 判断函数
    • maps - 配置属性
    • maps.children - 子节点映射字段,默认: children
  • 示例:
    TreeFilter(tree, (node) => node.active);
    TreeFilter(tree, (node) => node.active, { children: 'child' });

TreeFilterChildEmpty

  • 说明: 将子节点列表为空数组处理成 null
  • 类型: (tree: T, maps?: Pick<TreeNodeOptions, 'children'>): T
  • 参数:
    • tree - 数据
    • maps - 配置属性
    • maps.children - 子节点映射字段,默认: children
  • 示例:
    TreeFilterChildEmpty(tree);
    TreeFilterChildEmpty(tree, { children: 'child' });