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-list-tool

v1.0.3

Published

tools used for convert list to tree,or convert tree to list,or beauty nested list

Downloads

6

Readme

###本库主要用来整理,扁平化和反扁平化数组

安装: npm install tree-list-tool


引入: const { beautyFilter,Convert } = require("tree-list-tool")


beautyFilter函数主要用于把数组或嵌套数组按自定义规则美化为自己需要的结构和数据. beautyFilter函数使用方式:

beautyFilter({
  config
})
config包含以下可配置项,除list为必填,其他均为选填
/**
 * 过滤美化一个list,包括修改list中的key到目标key,删除不要的属性,根据条件过滤列表
 *
 * @param {*} list 目标列表
 * @param {*} keys 需要替换的key的集合 {'originKey':'targetKey'} 的结构
 * @param {*} children 子项的键值,该项需要填写为字符串,默认为'children'
 * @param {*} addPropertys 给所有项增加指点的键及对应的属性, 如{isOk:true}
 * @param {*} deleteList 需要删除的属性的列表,例如: ['a','b','c'],表示目标元素需要删掉a,b,c三个属性
 * @param {*} filterList 过滤函数列表,不满足该条件的列表将会从列表中删除, [function(ele){return ele.a > 0},function(ele){return ele.b = 0}]
 * @param {*} deleteNullChild 当一个项的children的长度为0时是否删除该属性,默认为true
 *
 * @returns 返回一个美化后的数组
 */

Convert类是用于转换树形结构数组到扁平结构的数组构造函数,可以对扁平化的数组进行增删改查等基础功能,也可以逆扁平化为树形数组嵌套结构.

Convert类使用方式:

let convert = new Convert(config)
config为选填,可配置项为如下:
/**
 *  类初始化时会定义如下扁平化新增的属性
 *  deep:'_indent', 表示数据项的深度
 *  parentId:'_parent_xxxid', 表示数据的父级id
 *  id:'_xxxid', 表示数据的id
 *  tree:'_parentTree', 表示数据的所有父类的列表的key
 *  isParent:'_isParent', 表示数据是否是父项
 *  children:'childrens' 表示数据的所有子项
 *
 * @class TreeListConvert
 */

Convert类生成的实例(例如:convert)有如下属性及方法:

******  初始化实例数据
convert.setTable(list)
list为嵌套树形结构的数组

******  获取扁平化数组结构
convert.listData

******  获取逆扁平化数组结构(将扁平的一维数组还原为多维数组)
convert.treeData

******  给指定index项上部新增同级项
convert.unshfit(index,obj)
obj为自定义数据,如不填则会按照指定index所在的项按属性类型进行初始化

******  给指定index项下部新增同级项
convert.push(index,obj)
obj为自定义数据,如不填则会按照指定index所在的项按属性类型进行初始化

******  给指定index项新增单个子项
convert.pushChild(index,obj)
obj为自定义数据,如不填则会按照指定index所在的项按属性类型进行初始化

******  给指定index项批量新增子项
convert.pushChildren(index,[obj,obj...])
obj为自定义数据,如不填则会按照指定index所在的项按属性类型进行初始化

******  查找指定index项是否存在子项,如存在则返回true,如不存在返回false
convert.hasChildren(index)

******  返回指定项index的所有子项,并返回为一个列表,如不存在子项则返回空列表
convert.getChildren(index)

******  删除指定index项的所有子项及嵌套子项(不包括自己)
convert.clearChildren(index)

******  删除指定index项并包含所有子项及嵌套子项
convert.deleteItems(index)

******  返回指定index项的父index所在位置,如不存在父index则返回-1
convert.parentIndex(index)

******  返回指定项的嵌套深度,如不输入指定项(index),则返回数组的最大深度
convert.getDeep(index)

******  返回指定index项的父index,如父index不存在则返回-1
convert.getParent(index)

******  返回指定index项的上一个同级项(嵌套深度相同,且为同一个父项的子项),如不存在则返回-1
convert.previousSameDeepItem(index)

******  返回指定index项的下一个同级项(嵌套深度相同,且为同一个父项的子项),如不存在则返回-1
convert.nextSameDeepItem(index)

****** 把指定项index移动到目标targetIndex位置,如果指定项index包含子项则子项一起移动
convert.moveItem(index,targetIndex)