@ifake/tree-path

Get tree path by the given condition.

Usage no npm install needed!

<script type="module">
  import ifakeTreePath from 'https://cdn.skypack.dev/@ifake/tree-path';
</script>

README

Welcome to @ifake/tree-path 👋

Version codecov Npm Bundle Size License: MIT Build Status

Get tree path by the given condition.

English | įŽ€äŊ“中文

🏠 Homepage

Install

npm install @ifake/tree-path -S
# or
yarn add @ifake/tree-path -S

Usage

  • Browser
// We expose a global variable that can be used directly in the browser.

const { treePath } = window.IFakeTP
  • ES6 Module
import { treePath } from '@ifake/tree-path'
  • CommonJs
const { treePath } = require('@ifake/tree-path')

API

treePath(TreePath)

export interface TreePath {
  tree: Tree
  childrenKey?: string
  breakCondition: BreakCondition
}
  • tree Receive an array of object data.
  • childrenKey Custom property values, default is children
  • breakCondition Receives a real-time value from inside the function, defines conditions based on the value and returns a boolean value.

Example

const sourceData = [
 {
   id: 1,
   child: [
     {
       id: '1-1'
     },
     {
       id: '1-2',
       child: [
         {
           id: '1-2-1'
         }
       ]
     }
   ]
 },
 {
   id: 2
 }
]

const result = treePath({
    tree: sourceData,
    childrenKey: 'child',
    breakCondition: treeNode => treeNode.id === '1-2-1'
})

// The value should be:
[
    {
       id: 1,
       child: [...]
     },
    {
       id: '1-2',
       child: [...]
     },
    {
      id: '1-2-1'
    }
]

Run tests

yarn run test

Author

👤 biyuqiwan@163.com

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

📝 License

Copyright Š 2020 biyuqiwan@163.com.

This project is MIT licensed.


This README was generated with ❤ī¸ by readme-md-generator