Skip to content

Latest commit

 

History

History
127 lines (102 loc) · 2.52 KB

README.zh-CN.md

File metadata and controls

127 lines (102 loc) · 2.52 KB

Welcome to @ifake/tree-path 👋

Version codecov Npm Bundle Size License: MIT Build Status

根据条件获取该条件下的所有树的节点.

English | 简体中文

🏠 首页

安装

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

使用

  • 浏览器
// 在浏览器端, 我们暴露了一个全局变量`IFakeTP`

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 接受一个树即数组对象的形式.
  • childrenKey 自定义树的嵌套的属性值, 默认是 children
  • breakCondition 实时地返回当前遍历的树的节点, 根据该函数返回的条件返回所需要查询的数据结构

例子

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'
})

// 结果:
[
    {
       id: 1,
       child: [...]
     },
    {
       id: '1-2',
       child: [...]
     },
    {
      id: '1-2-1'
    }
]

测试

yarn run test

作者信息

🤝 Contributing

欢迎贡献代码, 发现问题!

问题报告地址 issues page.

📝 License

Copyright © 2020 [email protected].

当前项目的许可是MIT.


This README was generated with ❤️ by readme-md-generator