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

think-tree-utils

v1.0.7

Published

树结构数据处理工具,增删改查移动等。

Downloads

8

Readme

think-tree-utils

Think-Tree-Utils 是一个 JavaScript 库,用于处理树结构的数据。它提供了很多常用的方法来操作树结构,例如查询,排序,修改,删除等等

目录结构

.
├── src
│   ├── buildTree.ts             // 数组转树结构
│   ├── delNodeByKey.ts          // 根据某个属性删除节点
│   ├── getNodeByKey.ts          // 根据某个属性获取节点
│   ├── getParentNodeByKey.ts    // 根据某个属性获取父节点
│   ├── getTopNodeByKey.ts       // 根据某个属性获取顶级节点
│   ├── insertNode.ts            // 插入节点在某节点之前或之后或子节点
│   ├── moveNodeByKey.ts         // 根据某个属性向上或向下移动
│   ├── setNodeByKey.ts          // 根据某个属性修改节点
│   ├── rewriteFields.ts         // 遍历更新字段名
│   ├── getFieldPathByKey.ts     // 获取某个字段值所在的链路
│   ├── searchTreeByKey.ts       // 根据某个属性模糊查询树结构
│   └── sortTreeByKey.ts         // 根据某个属性排序
├── test
│    └── index.js
├── README.md
├── index.js
├── package.json
├── tsconfig.json

安装

npm install think-tree-utils

常用方法演示

1. 数组转树结构 (buildTree)

参数:(data, { parentId = null, idName = 'id', parentIdName = 'parentid', childrenName = 'children' } = {})

const items = [
    {
        id: 1,
        name: '张三'
    },
    {
        id: 2,
        parentid: 1,
        name: '李四'
    },
    {
        id: 3,
        parentid: 1,
        name: '王五'
    },
    {
        id: 4,
        parentid: 2,
        name: '赵六'
    },
    {
        id: 5,
        parentid: 3,
        name: '刘七'
    },
    {
        id: 6,
        name: '周大生'
    },
    {
        id: 7,
        parentid: 1,
        name: '陈翔六点半'
    }
]

const tree = TreeUtil.buildTree(items, { childrenName: 'child' })
console.log('构建树结构', tree)

2. 根据某个属性获取节点 (getNodeByKey)

参数:(key, tree, { keyName = 'id', childrenName = 'children' } = {})

const node2 = TreeUtil.getNodeByKey(2, tree, { childrenName: 'child' })
console.log('ID为2的节点', node2)

3. 根据某个属性修改节点 (setNodeByKey)

参数:(tree, newNode, { keyName = 'id', childrenName = 'children' } = {})

TreeUtil.setNodeByKey(tree, { ...node2, name: '李达康' }, { childrenName: 'child' })
console.log('修改id=2的这个节点后的树', tree)

4. 根据某个属性删除节点 (delNodeByKey)

参数:(key, tree, { keyName = 'id', childrenName = 'children' } = {})

TreeUtil.delNodeByKey(3, tree, { childrenName: 'child' })
console.log('删除id=3的这个节点后的树', tree)

5. 根据某个属性排序 (sortTreeByKey)

参数:(tree, sort = 'desc', { keyName = 'id', childrenName = 'children' } = {})

TreeUtil.sortTreeByKey(tree, 'desc', { keyName: 'id', childrenName: 'child' })
console.log('根据id对数结构进行降序', tree)

6. 根据某个属性获取父节点 (getParentNodeByKey)

参数:(key, tree, { keyName = 'id', childrenName = 'children' } = {})

const node4 = TreeUtil.getParentNodeByKey(4, tree, { childrenName: 'child' })
console.log('获取id=4的父节点', node4)

7. 插入节点在某节点之前或之后或子节点 (insertNode)

参数:(key, tree, obj, type = 'before', { keyName = 'id', childrenName = 'children' } = {})

TreeUtil.insertNode(4, tree, {id: 8, parentid: 2, name: '老六'}, 'before', { childrenName: 'child' })
console.log('给id=4的节点前插入节点', tree)

TreeUtil.insertNode(4, tree, {id: 9, parentid: 2, name: '老七'}, 'after', { childrenName: 'child' })
console.log('给id=4的节点后插入节点', tree)

TreeUtil.insertNode(4, tree, {id: 10, parentid: 4, name: '老八'}, 'children', { childrenName: 'child' })
console.log('给id=4的节点插入子节点', tree)

8. 根据某个属性向上或向下移动 (moveNodeByKey)

参数:(key, tree, type = 'up', num = 1, { keyName = 'id', childrenName = 'children' } = {})

TreeUtil.moveNodeByKey(6, tree, 'lower', 1, { childrenName: 'child' })
console.log('id=6的节点向下移动一位', tree)

9. 根据某个属性获取顶级节点 (getTopNodeByKey)

参数:(key, tree, { keyName = 'id', childrenName = 'children' } = {})

const node5 = TreeUtil.getTopNodeByKey(6, tree, { childrenName: 'child' })
console.log('获取id=6的顶级节点', node5)

10. 遍历更新字段名 (rewriteFields)

参数:(tree, transformFn, childrenName = 'children')

const node6 = TreeUtil.rewriteFields(tree, (node) => {
    const { name, ...rest } = node
    return {
        nickname: name,
        ...rest
    }
}, 'child')
console.log('把字段name替换成nickname', node6)

11. 获取某个字段值所在的链路 (getFieldPathByKey)

参数:(key: string, tree: Node[], { keyName = 'id', childrenName = 'children' })

const node7 = TreeUtil.getFieldPathByKey(10, tree, { childrenName: 'child' })
console.log('获取id=10的字段路径', node7)

12. 根据某个属性模糊查询树结构 (searchTreeByKey)

参数:(key, tree, { keyName = 'id', childrenName = 'children' } = {})

const node8 = TreeUtil.searchTreeByKey('六', node6, { keyName: 'nickname', childrenName: 'child' })
console.log('模糊查询nickname里有六的节点,并得到新树', node8)