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

@newarea/tree-utils

v0.0.2

Published

一些树结构数据(数组、对象)操作的工具函数

Downloads

3

Readme

tree-utils

一些树结构数据(数组、对象)操作的工具函数,所提供的函数都是 “纯函数”,并不会对原数据结构直接产生修改。

安装

pnpm install tree-utils

使用

import { filter } from 'tree-utils'

const tree = [
  { id: 1, children: [{ id: 2, children: [{ id: 3 }] }] },
  { id: 4, children: [{ id: 5 }] },
]

const result = filter(tree, node => node.id > 2)

console.log(result)

配置项

strategy

设置搜索策略。默认策略为 pre,所有本库提供的方法都支持以下三种策略:

  • pre: 深度优先,正序搜索;
  • post: 深度优先,反序搜索;
  • breadth: 广度优先
import { filter } from 'tree-utils'

const result = filter(tree, node => node.id > 2, { strategy: 'post' })

childrenKey

自定义子节点 key 名。默认值为 children:

import { filter } from 'tree-utils'

const result = filter(tree, node => node.id > 2, { childrenKey: 'items' })

getChildrenKey

设置一棵树上多种自定义子节点 key 名。

import { filter } from 'tree-utils'

const treeMultiChildrenKey: Tree = {
  key: '1',
  children: [
    {
      key: '2',
      subItems: [
        {
          key: '3'
        }
      ]
    },
    {
      key: '4',
      subItems: [
        {
          key: '5'
        }
      ]
    }
  ]
}

const result = filter(
  treeMultiChildrenKey,
  node => node.key > 2,
  {
    getChildrenKey: (tree, meta) => {
      if (meta.depth === 1) {
        return 'subItems'
      }
    }
  }
)

方法列表

foreach

遍历树形数组/对象,对每个节点执行回调。

foreach(tree, predicate, [options])
  • tree: 树形数组/对象
  • predicate: 回调函数,对每个节点执行回调
  • options: 配置项,可选,对象类型,支持 strategychildrenKey
import { foreach } from 'tree-utils'

const data = {
  key: 1,
  children: [
    {
      key: 11,
      children: [
        {
          key: 111
        },
        {
          key: 112
        }
      ]
    },
    {
      key: 12,
      children: [
        {
          key: 122,
          children: [
            {
              key: 1221
            },
            {
              key: 1222
            }
          ]
        }
      ]
    }
  ]
}
foreach(data, t => console.log(t.key))
// 1
// 11
// 111
// 112
// 12
// 122
// 1221
// 1222

filter

遍历树形数组/对象,并把返回非真值的节点剔除。(不会影响原结构,返回的树是新生成的)

filter(tree, predicate, [options])
  • tree: 树形数组/对象
  • predicate: 每次迭代调用的函数,返回非真值时,该节点会从树上剔除。
  • options: 配置项,可选,支持 strategychildrenKey
import { filter } from 'tree-utils'

const data = {
  key: 1,
  children: [
    {
      key: 11,
      children: [
        {
          key: 99
        },
        {
          key: 112
        }
      ]
    },
    {
      key: 12,
      children: [
        {
          key: 122,
          children: [
            {
              key: 1221
            },
            {
              key: 1222
            }
          ]
        }
      ]
    }
  ]
}
const res = filter(data, t => t.key < 100)
console.log(res)
// {
//   "key": 1,
//   "children": [
//     {
//       "key": 11,
//       "children": [
//         {
//           "key": 99
//         }
//       ]
//     },
//     {
//       "key": 12,
//       "children": []
//     }
//   ]
// }

map

遍历树形数组/对象,根据返回的对象,组成新的树。

map(tree, predicate, [options])
  • tree: 树形数组/对象
  • predicate: 每次迭代调用的函数,需要返回一个对象,返回的对象上无需包括子节点。
  • options: 配置项,可选,支持 strategychildrenKey
import { map } from 'tree-utils'

const data = {
  key: 1,
  children: [
    {
      key: 11,
      children: [
        {
          key: 111
        },
        {
          key: 112
        }
      ]
    },
    {
      key: 12,
      children: [
        {
          key: 122,
          children: [
            {
              key: 1221
            },
            {
              key: 1222
            }
          ]
        }
      ]
    }
  ]
}
const res = map(data, t => ({ name: `No.${t.key}` }))
console.log(res)
// {
//   "name": "No.1",
//   "children": [
//     {
//       "name": "No.11",
//       "children": [
//         { "name": "No.111" },
//         { "name": "No.112" }
//       ]
//     },
//     {
//       "name": "No.12",
//       "children": [
//         {
//           "name": "No.122",
//           "children": [
//             { "name": "No.1221" },
//             { "name": "No.1222" }
//           ]
//         }
//       ]
//     }
//   ]
// }

find

遍历树形数组/对象,找到第一个返回非空值的节点。

find(tree, predicate, [options])
  • tree: 树形数组/对象
  • predicate: 每次迭代调用的函数
  • options: 配置项,可选,支持 strategychildrenKey
import { find } from 'tree-utils'

const data = {
  key: 1,
  children: [
    {
      key: 11,
      children: [
        {
          key: 111
        },
        {
          key: 112
        }
      ]
    },
    {
      key: 12,
      children: [
        {
          key: 122,
          children: [
            {
              key: 1221
            },
            {
              key: 1222
            }
          ]
        }
      ]
    }
  ]
}
const res = find(data, t => t.key < 100 && t.key > 10)
console.log(res)
// {
//     "key": 11,
//     "children": [
//         {
//             "key": 111
//         },
//         {
//             "key": 112
//         }
//     ]
// }

some

遍历树形数组/对象,判断是否存在符合条件的节点。

some(tree, predicate, [options])
  • tree: 树形数组/对象
  • predicate: 每次迭代调用的函数
  • options: 配置项,可选,支持 strategychildrenKey
import { some } from 'tree-utils'

const data = {
  key: 1,
  children: [
    {
      key: 11,
      children: [
        {
          key: 111
        },
        {
          key: 112
        }
      ]
    },
    {
      key: 12,
      children: [
        {
          key: 122,
          children: [
            {
              key: 1221
            },
            {
              key: 1222
            }
          ]
        }
      ]
    }
  ]
}
const res = some(data, t => t.key < 100 && t.key > 10)
console.log(res)
// true

toArray

将树形数组/对象转换为一维数组,数组会包含所有节点。

toArray(tree, [options])
  • tree: 树形数组/对象
  • options: 配置项,可选,支持 strategychildrenKey
import { toArray } from 'tree-utils'

const tree = {
  key: '1',
  children: [
    {
      key: '2',
      children: [
        {
          key: '3'
        }
      ]
    }
  ]
}
toArray(tree).map(t => t.key)
// ['1', '2', '3']

fromArray

将数组转换为树形数组/对象。

fromArray(array, [options])
  • array: 数组
  • options: 配置项,可选
    • itemKey: 指定节点 key 字段名,默认值:id
    • parentKey: 指定节点 key 字段名,默认值:pid
    • childrenKey: 指定节点 key 字段名,默认值:children
import { fromArray } from 'tree-utils'
const tree = [
  {
    id: '1',
    name: '1',
  },
  {
    id: '2',
    name: '2',
    pid: '1',
  },
  {
    id: '3',
    name: '3',
    pid: '1',
  },
  {
    id: '4',
    name: '4',
    pid: '2',
  },
  {
    id: '5',
    name: '5',
  },
]
fromArray(tree)
// =>
// [
//   {
//     id: '1',
//     name: '1',
//     children: [
//       {
//         id: "2",
//         name: "2",
//         pid: "1",
//         children: [
//           {
//             id: "4",
//             name: "4",
//             pid: "2",
//           }
//         ]
//       },
//       {
//         id: "3",
//         name: "3",
//         pid: "1",
//       },
//     ]
//   },
//   {
//     id: "5",
//     name: "5",
//   },
// ]