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

tree-utils.js

v0.0.3

Published

树结构数据相关逻辑操作集

Downloads

3

Readme

树结构数据相关逻辑操作集

功能包含以下:

  • 将树型结构数据转换成一维数组
  • 寻找指定子节点
  • 获取节点下的所有子节点
  • 遍历每个树节点
  • 获取所有父节点

Install

npm install

Use

安装

npm i tree-utils.js

引用

示例一

说明:对象字段为{key: 'xxx', parentKey: 'xxx', children: []}

import {
    getListFromTree,
    getNodeByKey,
    getChildrenToList,
    forEachNode,
    getParentNodes
} from 'tree-utils.js';

const treeData: any[] = [
    {
        title: '0-0',
        key: '0-0',
        parentKey: '',
        children: [
            {
                title: '0-0-0',
                key: '0-0-0',
                parentKey: '0-0',
                children: [
                    { title: '0-0-0-0', key: '0-0-0-0', parentKey: '0-0-0' },
                    { title: '0-0-0-1', key: '0-0-0-1', parentKey: '0-0-0' },
                    { title: '0-0-0-2', key: '0-0-0-2', parentKey: '0-0-0' },
                ],
            },
            {
                title: '0-0-1',
                key: '0-0-1',
                parentKey: '0-0',
                children: [
                    { title: '0-0-1-0', key: '0-0-1-0', parentKey: '0-0-1' },
                    { title: '0-0-1-1', key: '0-0-1-1', parentKey: '0-0-1' },
                    { title: '0-0-1-2', key: '0-0-1-2', parentKey: '0-0-1' },
                ],
            },
            {
                title: '0-0-2',
                key: '0-0-2',
                parentKey: '0-0',
                children: null
            },
        ],
    },
    {
        title: '0-1',
        key: '0-1',
        parentKey: '',
        children: [
            { title: '0-1-0-0', key: '0-1-0-0', parentKey: '0-1' },
            { title: '0-1-0-1', key: '0-1-0-1', parentKey: '0-1' },
            { title: '0-1-0-2', key: '0-1-0-2', parentKey: '0-1' },
        ],
    },
    {
        title: '0-2',
        key: '0-2',
        parentKey: '',
        children: null
    },
];


console.log('getListFromTree', getListFromTree(treeData))
console.log('getNodeByKey', getNodeByKey(treeData, '0-0-0'))
console.log('getChildrenToList', getChildrenToList(treeData, '0-1'))
console.log(forEachNode(treeData, (v: any, list: any) => {console.log('子项:', v)}))
console.log('getParentNodes', getParentNodes('0-0-1-0', treeData))

示例二

说明:对象字段为{id: 'xxx', parentId: 'xxx', children: []}

import {
    getListFromTree,
    getNodeByKey,
    getChildrenToList,
    forEachNode,
    getParentNodes
} from 'tree-utils.js';

const treeData: any[] = [
    {
        title: '0-0',
        id: '0-0',
        parentId: '',
        children: [
            {
                title: '0-0-0',
                id: '0-0-0',
                parentId: '0-0',
                children: [
                    { title: '0-0-0-0', id: '0-0-0-0', parentId: '0-0-0' },
                    { title: '0-0-0-1', id: '0-0-0-1', parentId: '0-0-0' },
                    { title: '0-0-0-2', id: '0-0-0-2', parentId: '0-0-0' },
                ],
            },
            {
                title: '0-0-1',
                id: '0-0-1',
                parentId: '0-0',
                children: [
                    { title: '0-0-1-0', id: '0-0-1-0', parentId: '0-0-1' },
                    { title: '0-0-1-1', id: '0-0-1-1', parentId: '0-0-1' },
                    { title: '0-0-1-2', id: '0-0-1-2', parentId: '0-0-1' },
                ],
            },
            {
                title: '0-0-2',
                id: '0-0-2',
                parentId: '0-0',
                children: null
            },
        ],
    },
    {
        title: '0-1',
        id: '0-1',
        parentId: '',
        children: [
            { title: '0-1-0-0', id: '0-1-0-0', parentId: '0-1' },
            { title: '0-1-0-1', id: '0-1-0-1', parentId: '0-1' },
            { title: '0-1-0-2', id: '0-1-0-2', parentId: '0-1' },
        ],
    },
    {
        title: '0-2',
        id: '0-2',
        parentId: '',
        children: null
    },
];


const fieldNames = {key: 'id', parentKey: 'parentId', children: 'children'}


console.log('getListFromTree1', getListFromTree(treeData, fieldNames))
console.log('getNodeByKey1', getNodeByKey(treeData, '0-0-0', fieldNames))
console.log('getChildrenToList1', getChildrenToList(treeData, '0-1', fieldNames))
console.log(forEachNode(treeData, (v: any, list: any) => {console.log('子项1:', v)}, fieldNames))
console.log('getParentNodes1', getParentNodes('0-0-1-0', treeData, fieldNames))

测试

打开cmd控制台,执行以下命令:

cd example

tsc test.ts 

node test.js

Author

👤 jacky010

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator