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

zyc_tree

v1.0.2

Published

自己封装的树处理插件

Downloads

5

Readme

排序函数

function objSortBykey (objArr, key) {

let result = objArr.slice(0);

return result.sort((a, b) => a[key] - b[key]);

}

初始化构造函数

export function TREEOBJ (pidKey, childrenKey, label, topPid) {

// 对象主体

this.pidKey = pidKey; // 所属父节点id key

this.topPid = topPid; // 顶级父节点id

this.childrenKey = childrenKey; // 子节点children

this.label = label; // 节点labe 的key值

// data 传进的树数据列表 pidKey 所属父级id对应的key值

this.create = function (data, pidKey = this.pidKey, pid) {

pid = pid || this.topPid; // 0为最高父级节点父id

let list = [];

data.map(item => {

if (item[pidKey] == pid) {

item[childrenKey] = this.create(data, pidKey, item.id);

list.push(item);

}

});

return list;

};

将树型结构数据处理成二位数组返回

this.getTreeDataList = function (data) {

const res = [];

function getData (data) {

data.forEach(v => {

res.push(v);

if (v[childrenKey]) {

getData(v[childrenKey]);

}

});

}

getData(data);

return res;

};

获取树节点的数量

this.getTreeNodeCount = function (data) {

return this.getTreeDataList(data).length || null;

};

根据对树节点的子节点根据key值进行排序

this.sortTreeNodeChildrenByKey = function (data, id, key) {

this.getTreeItem(data, id, item => {

if (item) {

item[childrenKey] = objSortBykey(item[childrenKey], key);

}

});

return data;

};

id获取节点递归函数

this.getTreeItemByKey = function (data, key, val, callBack) {

data.map(item => {

if (item[key] == val) {

callBack && (typeof callBack == 'function') && callBack(item);

} else {

if (item[childrenKey]) {

this.getTreeItemByKey(item[childrenKey], key, val, callBack);

}

}

});

};

根绝树节点id获取树节点的值

this.getTreeNodeById = function (data, id) {

let result = null; // 运行结果

this.getTreeItem(data, id, item => {

result = item;

});

return result;

};

设置修改树中的key对象值

this.setTreeNodeValue = function (data, id, key, value) {

this.getTreeItem(data, id, item => {

item[key] = value;

});

};

根据id获取对应树中的key值

this.getTreeNodeValueByKey = function (data, id, key) {

let value = null;

this.getTreeItem(data, id, item => {

value = item[key];

});

return value;

};

根据id删除对应树中的key值

this.delTreeNodeKey = function (data, id, key) {

let value = null;

this.getTreeItem(data, id, item => {

value = item[key];

if (key in item) {

delete item[key];

}

});

return value;

};

往树里面追加子节点

this.appendChildrenNode = function (data, id, node = {}) {

this.getTreeItem(data, id, item => {

if (item) {

if (item[childrenKey]) {

item[childrenKey].push(node);

} else {

item[childrenKey] = [];

item[childrenKey].push(node);

}

}

});

return data;

};

根据id 清空子节点

this.clearChildrenNode = function (data, id) {

this.getTreeItem(data, id, item => {

if (item) {

item[childrenKey] = [];

}

});

};

通过子节点id删除子节点

this.delChildrenNodeById = function (data, id, childId) {

this.getTreeItem(data, id, item => {

if (item) {

(item[childrenKey] || []).forEach((v, idx) => {

if (v.id == childId) {

item[childrenKey].splice(idx, 1);

}

});

}

});

};

获取父级节点

this.getParentNode = function (data, id) {

let currentNode = null;

let parentNode = null;

this.getTreeItem(data, id, item => {

if (item) {

currentNode = item;

}

});

if (currentNode) {

this.getTreeItem(data, currentNode[this.pidKey], item => {

if (item) {

parentNode = item;

}

});

}

return parentNode;

};

获取父级及祖先节点

this.getParentNodes = function (data, id) {

let parentNodes = this.getLevelNode(data, id);

return parentNodes;

};

获取子节点及子孙所有节点

this.getChildrenNodes = function (data, id) {

let res = [];

let childrens = [];

this.getTreeItem(data, id, item => {

if (item) {

childrens = item[childrenKey];

}

});

function getChs (data) {

for (let i = 0, len = data.length; i < len; i++) {

res.push(data[i]);

if (data[i][childrenKey] && data[i][childrenKey].length > 0) {

getChs(data[i][childrenKey]);

}

}

}

getChs(childrens);

return res;

};

获取子节点及子节点

this.getChildrenNode = function (data, id) {

let childrens = [];

this.getTreeItem(data, id, item => {

if (item) {

childrens = item[childrenKey];

}

});

return childrens;

};

辅组查询层级递归遍历函数

this.getTreeLevelNode = function (data, id, topPid = this.topPid, result, pidKey = this.pidKey) {

let currentId = id;

let node = this.getTreeNodeById(data, currentId);

result.push(node);

if (node && node[pidKey] && node[pidKey] !== topPid) {

this.getTreeLevelNode(data, node[pidKey], topPid, result, pidKey);

}

};

根据id获取当前树节点的层级节点数据(倒序), topPid 最顶级父节点id

this.getLevelNode = function (data, id, topPid = this.topPid, pidKey = this.pidKey) {

let result = [];

this.getTreeLevelNode(data, id, topPid, result, pidKey);

return result || [];

};

获取当前节点层级

this.getNodeLevel = function (data, id) {

const res = this.getLevelNode(data, id);

return res.length || 0;

};

}

module.exports = { TREEOBJ };