This package has been deprecated

Author message:

@colin-luo/tree-utils has renamed to @colin-luo/tree

@colin-luo/tree-utils
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

Tree utils Build Status

Tree utils for Typescript and Javascript;

Install

npm install @colin-luo/tree-utils

Examples

Javascript

with default data structure (has key and children properties).

import TreeUtils from '@colin-luo/tree-utils';

const data = [
  {key: 'a', label: 'A', icon: 'a.svg'},
  {key: 'b', label: 'B', icon: 'b.svg', children: [
    {key: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {key: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {key: 'c', label: 'C', icon: 'c.svg', children: []},
];

const tree2 = new TreeUtils();

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

tree2.hasChildren(data[0]) // => false
tree2.hasChildren(data[1]) // => true
tree2.hasChildren(data[2]) // => false

with custom data structure (id as key fieldname and sublist as children fieldname)

import TreeUtils from '@colin-luo/tree-utils';

const data = [
  {id: 'a', label: 'A', icon: 'a.svg'},
  {id: 'b', label: 'B', icon: 'b.svg', sublist: [
    {id: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {id: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {id: 'c', label: 'C', icon: 'c.svg', sublist: []},
];

const tree = new TreeUtils('id', 'sublist');

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

Typescript

with default data structure (has key and children properties).

import TreeUtils from '@colin-luo/tree-utils';

innterface NodeProps {
  key: string;
  children: NodeProps[];
  label: string;
  icon: string;
}

const data = [
  {key: 'a', label: 'A', icon: 'a.svg'},
  {key: 'b', label: 'B', icon: 'b.svg', children: [
    {key: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {key: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {key: 'c', label: 'C', icon: 'c.svg', children: []},
];

const tree = new TreeUtils<'key', 'children', NodeProps>('key', 'children');
const tree2 = new TreeUtils<NodeProps>();

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

tree2.hasChildren(data[0]) // => false
tree2.hasChildren(data[1]) // => true
tree2.hasChildren(data[2]) // => false

with custom data structure (id as key fieldname and sublist as children fieldname)

import TreeUtils from '@colin-luo/tree-utils';

innterface NodeProps {
  id: string;
  sublist: NodeProps[];
  text: string;
  avatar: string;
}

const data = [
  {id: 'a', text: 'A', avatar: 'a.svg'},
  {id: 'b', text: 'B', avatar: 'b.svg', sublist: [
    {id: 'b-a', text: 'B-A', avatar: 'b-a.svg'},
    {id: 'b-b', text: 'B-B', avatar: 'b-b.svg'},
  ]},
  {id: 'c', text: 'C', avatar: 'c.svg', sublist: []},
];

const tree = new TreeUtils<'id', 'sublist', NodeProps>('id', 'sublist');

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

Api documentation

Api documentation

Package Sidebar

Install

npm i @colin-luo/tree-utils

Weekly Downloads

1

Version

3.0.2

License

ISC

Unpacked Size

83.9 kB

Total Files

15

Last publish

Collaborators

  • colin-luo