easy-tool
WalkForest
用于遍历树结构的对象
interface MockTree {
children?: MockTree[]
value: string
label?: string
}
interface BaseTree {
value: string
label?: string
}
interface HasChildTree extends BaseTree {
children: UnionTree[]
}
interface HasChildOtherTree extends BaseTree {
other: UnionTree[]
}
interface NotChildTree extends BaseTree {}
type UnionTree = NotChildTree | HasChildTree | HasChildOtherTree
用法
import { WalkForest } from 'easy-tool'
interface MockTree {
children?: MockTree[]
value: string
label?: string
}
const forest: MockTree[] = []
const walkForest = new WalkForest(forest, 'children', 'value')
walkForest.each((node, path) => {
})
设置遍历顺序
walkForest.setOrder('DLR')
API
walkForest
.setOrder // 设置遍历顺序
.each // 遍历所有的节点,若返回true则终止便利
.filterWithPath // 过滤返回true的节点和节点对应的路径列表
.filter // 过滤返回true的节点列表
.findWithPath // 返回第一个返回true的节点和其对应的路径,找到后终止遍历
.find // 返回第一个返回true的节点,找到后终止遍历
.some // 有一个为true,则结果为true(遇到true终止遍历)
.every // 有一个为false,则结果为false(遇到false终止遍历)
.count // 返回节点个数
.values // 返回所有value
.paths // 返回所有的path
.flatWithPath // 将树转为列表,每项是节点和其对应的路径
.flat // 将树转为列表,每项是节点
acute
一套响应式API
用法
import { useCell, brain } from 'easy-tool'
const state = useCell(10)
brain.watch(() => {
console.log(state.v)
})
const computeState = brain.compute(() => {
return state.v * 10
})