Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-tree-utils

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-tree-utils - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

41

dist/browser-bundle.js

@@ -25,8 +25,8 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.treeUtils = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){

static list2Tree(list, parentId = null) {
return list
.filter(item => item.parentId === parentId)
.map(item => (Object.assign(Object.assign({}, item), { children: this.list2Tree(list, item.id) })));
return this.deepCopy(list)
.filter((item) => item.parentId === parentId)
.map((item) => (Object.assign(Object.assign({}, item), { children: this.list2Tree(list, item.id) })));
}
static tree2List(tree, parentId = null) {
return tree.reduce((acc, curr) => {
return this.deepCopy(tree).reduce((acc, curr) => {
const { children } = curr, rest = __rest(curr, ["children"]);

@@ -41,7 +41,10 @@ return [

static findTreeNodeById(tree, id) {
const node = tree.find(item => item.id === id);
return this.findTreeNode(tree, item => item.id === id);
}
static findTreeNode(tree, fn) {
const node = tree.find(item => fn(item));
if (node) {
return node;
}
return tree.reduce((acc, curr) => acc || this.findTreeNodeById(curr.children || [], id), null);
return tree.reduce((acc, curr) => acc || this.findTreeNode(curr.children || [], fn), null);
}

@@ -51,12 +54,9 @@ static deleteNode(tree, id) {

if (index != -1) {
tree.splice(index, 1);
return;
return tree.splice(index, 1)[0];
}
tree.forEach(item => {
item.children && this.deleteNode(item.children, id);
});
return tree.reduce((acc, curr) => acc || this.deleteNode(curr.children, id), null);
}
static addNode(tree, parentId, child) {
static addNode(tree, parentId, childData) {
if (parentId == null) {
tree.push(child);
tree.push(childData);
return;

@@ -66,8 +66,6 @@ }

if (index != -1) {
tree[index].children = tree[index].children ? [...tree[index].children, child] : [child];
tree[index].children.push(Object.assign({ children: [] }, childData));
return;
}
tree.forEach(item => {
item.children && this.addNode(item.children, parentId, child);
});
tree.forEach(item => this.addNode(item.children, parentId, childData));
}

@@ -77,9 +75,10 @@ static editNode(tree, id, data) {

if (index != -1) {
tree[index] = Object.assign(Object.assign({ id: tree[index].id }, data), (data.children ? { children: [] } : {}));
tree[index] = Object.assign({ id: tree[index].id, children: [] }, data);
return;
}
tree.forEach(item => {
item.children && this.editNode(item.children, id, data);
});
tree.forEach(item => this.editNode(item.children, id, data));
}
static deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
}

@@ -86,0 +85,0 @@ exports.TreeUtils = TreeUtils;

@@ -1,1 +0,1 @@

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.treeUtils=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.TreeUtils=void 0;var main_1=require("./main");Object.defineProperty(exports,"TreeUtils",{enumerable:true,get:function(){return main_1.TreeUtils}})},{"./main":2}],2:[function(require,module,exports){"use strict";var __rest=this&&this.__rest||function(s,e){var t={};for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p)&&e.indexOf(p)<0)t[p]=s[p];if(s!=null&&typeof Object.getOwnPropertySymbols==="function")for(var i=0,p=Object.getOwnPropertySymbols(s);i<p.length;i++){if(e.indexOf(p[i])<0&&Object.prototype.propertyIsEnumerable.call(s,p[i]))t[p[i]]=s[p[i]]}return t};Object.defineProperty(exports,"__esModule",{value:true});exports.TreeUtils=void 0;class TreeUtils{static list2Tree(list,parentId=null){return list.filter(item=>item.parentId===parentId).map(item=>Object.assign(Object.assign({},item),{children:this.list2Tree(list,item.id)}))}static tree2List(tree,parentId=null){return tree.reduce((acc,curr)=>{const{children}=curr,rest=__rest(curr,["children"]);return[...acc,Object.assign(Object.assign({},rest),{parentId:parentId}),...children.length?this.tree2List(children,rest.id):[]]},[])}static findTreeNodeById(tree,id){const node=tree.find(item=>item.id===id);if(node){return node}return tree.reduce((acc,curr)=>acc||this.findTreeNodeById(curr.children||[],id),null)}static deleteNode(tree,id){const index=tree.findIndex(item=>item.id==id);if(index!=-1){tree.splice(index,1);return}tree.forEach(item=>{item.children&&this.deleteNode(item.children,id)})}static addNode(tree,parentId,child){if(parentId==null){tree.push(child);return}const index=tree.findIndex(item=>item.id==parentId);if(index!=-1){tree[index].children=tree[index].children?[...tree[index].children,child]:[child];return}tree.forEach(item=>{item.children&&this.addNode(item.children,parentId,child)})}static editNode(tree,id,data){const index=tree.findIndex(item=>item.id==id);if(index!=-1){tree[index]=Object.assign(Object.assign({id:tree[index].id},data),data.children?{children:[]}:{});return}tree.forEach(item=>{item.children&&this.editNode(item.children,id,data)})}}exports.TreeUtils=TreeUtils},{}]},{},[1])(1)});
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.treeUtils=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.TreeUtils=void 0;var main_1=require("./main");Object.defineProperty(exports,"TreeUtils",{enumerable:true,get:function(){return main_1.TreeUtils}})},{"./main":2}],2:[function(require,module,exports){"use strict";var __rest=this&&this.__rest||function(s,e){var t={};for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p)&&e.indexOf(p)<0)t[p]=s[p];if(s!=null&&typeof Object.getOwnPropertySymbols==="function")for(var i=0,p=Object.getOwnPropertySymbols(s);i<p.length;i++){if(e.indexOf(p[i])<0&&Object.prototype.propertyIsEnumerable.call(s,p[i]))t[p[i]]=s[p[i]]}return t};Object.defineProperty(exports,"__esModule",{value:true});exports.TreeUtils=void 0;class TreeUtils{static list2Tree(list,parentId=null){return this.deepCopy(list).filter(item=>item.parentId===parentId).map(item=>Object.assign(Object.assign({},item),{children:this.list2Tree(list,item.id)}))}static tree2List(tree,parentId=null){return this.deepCopy(tree).reduce((acc,curr)=>{const{children}=curr,rest=__rest(curr,["children"]);return[...acc,Object.assign(Object.assign({},rest),{parentId:parentId}),...children.length?this.tree2List(children,rest.id):[]]},[])}static findTreeNodeById(tree,id){return this.findTreeNode(tree,item=>item.id===id)}static findTreeNode(tree,fn){const node=tree.find(item=>fn(item));if(node){return node}return tree.reduce((acc,curr)=>acc||this.findTreeNode(curr.children||[],fn),null)}static deleteNode(tree,id){const index=tree.findIndex(item=>item.id==id);if(index!=-1){return tree.splice(index,1)[0]}return tree.reduce((acc,curr)=>acc||this.deleteNode(curr.children,id),null)}static addNode(tree,parentId,childData){if(parentId==null){tree.push(childData);return}const index=tree.findIndex(item=>item.id==parentId);if(index!=-1){tree[index].children.push(Object.assign({children:[]},childData));return}tree.forEach(item=>this.addNode(item.children,parentId,childData))}static editNode(tree,id,data){const index=tree.findIndex(item=>item.id==id);if(index!=-1){tree[index]=Object.assign({id:tree[index].id,children:[]},data);return}tree.forEach(item=>this.editNode(item.children,id,data))}static deepCopy(obj){return JSON.parse(JSON.stringify(obj))}}exports.TreeUtils=TreeUtils},{}]},{},[1])(1)});
export declare class TreeUtils {
static list2Tree(list: any[], parentId?: number | null): any;
static tree2List(tree: any[], parentId?: any): any;
static list2Tree(list: any[], parentId?: any): any[];
static tree2List(tree: any[], parentId?: any): any[];
static findTreeNodeById(tree: any[], id: any): any;
static deleteNode(tree: any[], id: any): void;
static addNode(tree: any[], parentId: any, child: any): void;
static findTreeNode(tree: any[], fn: (item: any) => boolean): any;
static deleteNode(tree: any[], id: any): any;
static addNode(tree: any[], parentId: any, childData: any): void;
static editNode(tree: any[], id: any, data: any): void;
private static deepCopy;
}

@@ -17,8 +17,8 @@ "use strict";

static list2Tree(list, parentId = null) {
return list
.filter(item => item.parentId === parentId)
.map(item => (Object.assign(Object.assign({}, item), { children: this.list2Tree(list, item.id) })));
return this.deepCopy(list)
.filter((item) => item.parentId === parentId)
.map((item) => (Object.assign(Object.assign({}, item), { children: this.list2Tree(list, item.id) })));
}
static tree2List(tree, parentId = null) {
return tree.reduce((acc, curr) => {
return this.deepCopy(tree).reduce((acc, curr) => {
const { children } = curr, rest = __rest(curr, ["children"]);

@@ -33,7 +33,10 @@ return [

static findTreeNodeById(tree, id) {
const node = tree.find(item => item.id === id);
return this.findTreeNode(tree, item => item.id === id);
}
static findTreeNode(tree, fn) {
const node = tree.find(item => fn(item));
if (node) {
return node;
}
return tree.reduce((acc, curr) => acc || this.findTreeNodeById(curr.children || [], id), null);
return tree.reduce((acc, curr) => acc || this.findTreeNode(curr.children || [], fn), null);
}

@@ -43,12 +46,9 @@ static deleteNode(tree, id) {

if (index != -1) {
tree.splice(index, 1);
return;
return tree.splice(index, 1)[0];
}
tree.forEach(item => {
item.children && this.deleteNode(item.children, id);
});
return tree.reduce((acc, curr) => acc || this.deleteNode(curr.children, id), null);
}
static addNode(tree, parentId, child) {
static addNode(tree, parentId, childData) {
if (parentId == null) {
tree.push(child);
tree.push(childData);
return;

@@ -58,8 +58,6 @@ }

if (index != -1) {
tree[index].children = tree[index].children ? [...tree[index].children, child] : [child];
tree[index].children.push(Object.assign({ children: [] }, childData));
return;
}
tree.forEach(item => {
item.children && this.addNode(item.children, parentId, child);
});
tree.forEach(item => this.addNode(item.children, parentId, childData));
}

@@ -69,10 +67,11 @@ static editNode(tree, id, data) {

if (index != -1) {
tree[index] = Object.assign(Object.assign({ id: tree[index].id }, data), (data.children ? { children: [] } : {}));
tree[index] = Object.assign({ id: tree[index].id, children: [] }, data);
return;
}
tree.forEach(item => {
item.children && this.editNode(item.children, id, data);
});
tree.forEach(item => this.editNode(item.children, id, data));
}
static deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
}
exports.TreeUtils = TreeUtils;
{
"name": "simple-tree-utils",
"version": "0.0.2",
"version": "0.0.3",
"description": "Tree utils library.",

@@ -5,0 +5,0 @@ "keywords": [],

export class TreeUtils {
static list2Tree(list: any[], parentId: number | null = null): any {
return list
.filter(item => item.parentId === parentId)
.map(item => ({
static list2Tree(list: any[], parentId: any = null): any[] {
return this.deepCopy(list)
.filter((item: any) => item.parentId === parentId)
.map((item: any) => ({
...item,

@@ -12,4 +12,4 @@ children: this.list2Tree(list, item.id)

static tree2List(tree: any[], parentId: any = null): any {
return tree.reduce((acc, curr) => {
static tree2List(tree: any[], parentId: any = null): any[] {
return this.deepCopy(tree).reduce((acc: any, curr: any) => {
const {children, ...rest} = curr;

@@ -25,3 +25,7 @@ return [

static findTreeNodeById(tree: any[], id: any): any {
const node = tree.find(item => item.id === id);
return this.findTreeNode(tree, item => item.id === id);
}
static findTreeNode(tree: any[], fn: (item: any) => boolean): any {
const node = tree.find(item => fn(item));
if (node) {

@@ -31,20 +35,17 @@ return node;

return tree.reduce((acc, curr) =>
acc || this.findTreeNodeById(curr.children || [], id), null
acc || this.findTreeNode(curr.children || [], fn), null
);
}
static deleteNode(tree: any[], id: any): void {
static deleteNode(tree: any[], id: any): any {
const index = tree.findIndex(item => item.id == id);
if (index != -1) {
tree.splice(index, 1);
return;
return tree.splice(index, 1)[0];
}
tree.forEach(item => {
item.children && this.deleteNode(item.children, id);
});
return tree.reduce((acc, curr) => acc || this.deleteNode(curr.children, id), null);
}
static addNode(tree: any[], parentId: any, child: any): void {
static addNode(tree: any[], parentId: any, childData: any): void {
if (parentId == null) {
tree.push(child);
tree.push(childData);
return;

@@ -54,8 +55,6 @@ }

if (index != -1) {
tree[index].children = tree[index].children ? [...tree[index].children, child] : [child];
tree[index].children.push({children: [], ...childData});
return;
}
tree.forEach(item => {
item.children && this.addNode(item.children, parentId, child);
});
tree.forEach(item => this.addNode(item.children, parentId, childData));
}

@@ -66,13 +65,11 @@

if (index != -1) {
tree[index] = {
id: tree[index].id,
...data,
...(data.children ? {children: []} : {})
};
tree[index] = {id: tree[index].id, children: [], ...data};
return;
}
tree.forEach(item => {
item.children && this.editNode(item.children, id, data);
});
tree.forEach(item => this.editNode(item.children, id, data));
}
private static deepCopy(obj: any): any {
return JSON.parse(JSON.stringify(obj));
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc