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

lemon-tree

v4.3.8

Published

js tree 树 操作

Downloads

22

Readme

lemon-tree

lemon-tree 作为 js 操作 树形结构工具,它所支持的功能是从实际工作以及 leetcode 扫题中总结出的常用函数。

下载、安装

npm i lemon-tree
yarn add lemon-tree

导入

import Lemon from 'lemon-tree';

API

  • getNodeById(tree, nodeId) 返回树中特定节点对象
  • hasNodeId(tree, nodeId) 判断特定节点是否在树中
  • findParentIds(tree, nodeId) 获取全部父节点 id 数组(包括结点自身)
  • findParents(tree, nodeId) 获取全部父节点 数组(包括结点自身)
  • findTree(tree, nodeId) 获取 nodeId 所在树对象
  • makeTree(array) 将数组转化成树形结构返回
  • makeArray(tree) 将树转化成对象数组返回

使用

基本用法

const tree = {
    id: 1,
    children: {
        id: 2,
        children: {
            id: 3,
            children: {
                id: 4,
                children: {
                    id: 5,
                },
            },
        },
    },
};
const nodeId = 4;
const res = new Lemon().findParentIds(tree, nodeId);

返回值为:

[ 1, 2, 3, 4 ]

获取树中特定节点对象

const trees = [
  {
    id: 11,
    children: [
      { 
      	id: 112,
      	children: {
      	  id: 113
        } 
      } 
    ]
  },
  {
    id: 21,
    children: {
      id: 22,
      children: {
        id: 23
      }
    }
  }
];
const nodeId = 23;
const res = new Lemon().getNodeById(tree, nodeId);

返回值为:

{ id: 23 }

别名

lemon-tree 还支持列别名

const data = {
    ID: 'XX',
    CHILDREN: {
        ID: 'XXX',
        CHILDREN: {},
    },
};
const tree = new Lemon({ id: 'ID', children: 'CHILDREN' });
const res = tree.findParentIds(data, 'XXX');
const array2 = [
	{ Id: 1, pid: 0 },
	{ Id: 2, pid: 1 },
	{ Id: 3, pid: 1 },
	{ Id: 4, pid: 3 },
	{ Id: 5, pid: 4 },
	{ Id: 6, pid: 0 },
	{ Id: 7, pid: 6 },
	{ Id: 8, pid: 7 },
	{ Id: 9, pid: 8 },
	{ Id: 10, pid: 0 },
];
const template = { id: 'Id', pId: 'pid', children: 'child' };
const res = new Lemon(template).makeTree(array2);

(如果对使用有疑问,可以参考 test 目录下代码)