Skip to content

faukwaa/more-utils

Repository files navigation

📦 Install

ALL Utils

npm i more-utils-all

Tree

npm i more-utils-tree

🦄 Usage

flatToTree

import { flatToTree } from 'more-utils-tree'

const flat = [
  { id: 1, name: '1' },
  { id: 2, name: '2', parentId: 1 },
  { id: 3, name: '3', parentId: 2 }
]

const tree = flatToTree(flat)

// tree:
// [
//   {
//     children: [
//       {
//         children: [
//            ...
//         ],
//         id: 2,
//         name: '2',
//         parentId: 1,
//         parentIds: [1],
//         parent: {
//           id: 1,
//           name: '1',
//           parentId: null,
//           parentIds: [],
//           depth: 0,
//           path: '1',
//           isLeaf: false
//         },
//         depth: 1,
//         path: '1/2',
//         isLeaf: false
//       }],
//     id: 1,
//     name: '1',
//     parentId: null,
//     parentIds: [],
//     depth: 0,
//     path: '1',
//     isLeaf: false
//   }
// ]

treeToFlat

const tree = [
  {
    id: 1,
    name: '1',
    children: [
      {
        id: 2,
        name: '2',
        children: [
          {
            id: 3,
            name: '3'
          }
        ]
      }
    ]
  }
]

const flat = treeToFlat(tree)

// flat:
// [
//   {
//     id: 1, name: '1', parentId: null, parentIds: [], depth: 0, path: '1', isLeaf: false
//   },
//   {
//     id: 2, name: '2', parentId: 1, parentIds: [1],
//     parent: { id: 1, name: '1', parentId: null, parentIds: [], depth: 0, path: '1', isLeaf: false }, depth: 1, path: '1/2', isLeaf: false
//   },
//   ...
// ]

🐼 More Tree Utils

pathToTree

filterTree

eachTree

findTree

findIndexTree

mapTree

replaceTree

genTreeAttrs

Color

npm i more-utils-color

Format

npm i more-utils-format

About

Collection of essential JS Tree Utilities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published