From ef14b966f722091fae50a47150ba8954138dff31 Mon Sep 17 00:00:00 2001 From: xingyu Date: Tue, 4 Jul 2023 16:43:58 +0800 Subject: [PATCH] refactor: use function --- src/utils/tree.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/utils/tree.ts b/src/utils/tree.ts index e9d30f4..40d3b1a 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -17,7 +17,7 @@ export const defaultProps = { const getConfig = (config: Partial) => Object.assign({}, DEFAULT_CONFIG, config) // tree from list -export const listToTree = (list: any[], config: Partial = {}): T[] => { +export function listToTree(list: any[], config: Partial = {}): T[] { const conf = getConfig(config) as TreeHelperConfig const nodeMap = new Map() const result: T[] = [] @@ -34,7 +34,7 @@ export const listToTree = (list: any[], config: Partial(tree: any, config: Partial = {}): T => { +export function treeToList(tree: any, config: Partial = {}): T { config = getConfig(config) const { children } = config const result: any = [...tree] @@ -45,7 +45,7 @@ export const treeToList = (tree: any, config: Partial return result } -export const findNode = (tree: any, func: Fn, config: Partial = {}): T | null => { +export function findNode(tree: any, func: Fn, config: Partial = {}): T | null { config = getConfig(config) const { children } = config const list = [...tree] @@ -56,7 +56,7 @@ export const findNode = (tree: any, func: Fn, config: Partial(tree: any, func: Fn, config: Partial = {}): T[] => { +export function findNodeAll(tree: any, func: Fn, config: Partial = {}): T[] { config = getConfig(config) const { children } = config const list = [...tree] @@ -68,7 +68,7 @@ export const findNodeAll = (tree: any, func: Fn, config: Partial(tree: any, func: Fn, config: Partial = {}): T | T[] | null => { +export function findPath(tree: any, func: Fn, config: Partial = {}): T | T[] | null { config = getConfig(config) const path: T[] = [] const list = [...tree] @@ -91,7 +91,7 @@ export const findPath = (tree: any, func: Fn, config: Partial = {}) => { +export function findPathAll(tree: any, func: Fn, config: Partial = {}) { config = getConfig(config) const path: any[] = [] const list = [...tree] @@ -113,7 +113,7 @@ export const findPathAll = (tree: any, func: Fn, config: Partial(tree: T[], func: (n: T) => boolean, config: Partial = {}): T[] => { +export function filter(tree: T[], func: (n: T) => boolean, config: Partial = {}): T[] { config = getConfig(config) const children = config.children as string function listFilter(list: T[]) { @@ -127,7 +127,7 @@ export const filter = (tree: T[], func: (n: T) => boolean, config: Part return listFilter(tree) } -export const forEach = (tree: T[], func: (n: T) => any, config: Partial = {}): void => { +export function forEach(tree: T[], func: (n: T) => any, config: Partial = {}): void { config = getConfig(config) const list: any[] = [...tree] const { children } = config @@ -143,14 +143,14 @@ export const forEach = (tree: T[], func: (n: T) => any, config: Partial /** * @description: Extract tree specified structure */ -export const treeMap = (treeData: T[], opt: { children?: string; conversion: Fn }): T[] => { +export function treeMap(treeData: T[], opt: { children?: string; conversion: Fn }): T[] { return treeData.map((item) => treeMapEach(item, opt)) } /** * @description: Extract tree specified structure */ -export const treeMapEach = (data: any, { children = 'children', conversion }: { children?: string; conversion: Fn }) => { +export function treeMapEach(data: any, { children = 'children', conversion }: { children?: string; conversion: Fn }) { const haveChildren = Array.isArray(data[children]) && data[children].length > 0 const conversionData = conversion(data) || {} if (haveChildren) { @@ -176,7 +176,7 @@ export const treeMapEach = (data: any, { children = 'children', conversion }: { * @param callBack 回调 * @param parentNode 父节点 */ -export const eachTree = (treeDatas: any[], callBack: Fn, parentNode = {}) => { +export function eachTree(treeDatas: any[], callBack: Fn, parentNode = {}) { treeDatas.forEach((element) => { const newNode = callBack(element, parentNode) || element if (element.children) { @@ -192,7 +192,7 @@ export const eachTree = (treeDatas: any[], callBack: Fn, parentNode = {}) => { * @param {*} parentId 父节点字段 默认 'parentId' * @param {*} children 孩子节点字段 默认 'children' */ -export const handleTree = (data: any[], id?: string, parentId?: string, children?: string) => { +export function handleTree(data: any[], id?: string, parentId?: string, children?: string) { if (!Array.isArray(data)) { console.warn('data must be an array') return [] @@ -248,7 +248,7 @@ export const handleTree = (data: any[], id?: string, parentId?: string, children * @param {*} children 孩子节点字段 默认 'children' * @param {*} rootId 根Id 默认 0 */ -export const handleTree2 = (data, id, parentId, children, rootId) => { +export function handleTree2(data, id, parentId, children, rootId) { id = id || 'id' parentId = parentId || 'parentId' children = children || 'children'