tree.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. interface TreeHelperConfig {
  2. id: string
  3. children: string
  4. pid: string
  5. }
  6. const DEFAULT_CONFIG: TreeHelperConfig = {
  7. id: 'id',
  8. children: 'children',
  9. pid: 'pid'
  10. }
  11. const getConfig = (config: Partial<TreeHelperConfig>) => Object.assign({}, DEFAULT_CONFIG, config)
  12. // tree from list
  13. export const listToTree = <T = any>(list: any[], config: Partial<TreeHelperConfig> = {}): T[] => {
  14. const conf = getConfig(config) as TreeHelperConfig
  15. const nodeMap = new Map()
  16. const result: T[] = []
  17. const { id, children, pid } = conf
  18. for (const node of list) {
  19. node[children] = node[children] || []
  20. nodeMap.set(node[id], node)
  21. }
  22. for (const node of list) {
  23. const parent = nodeMap.get(node[pid])
  24. ;(parent ? parent.children : result).push(node)
  25. }
  26. return result
  27. }
  28. export const treeToList = <T = any>(tree: any, config: Partial<TreeHelperConfig> = {}): T => {
  29. config = getConfig(config)
  30. const { children } = config
  31. const result: any = [...tree]
  32. for (let i = 0; i < result.length; i++) {
  33. if (!result[i][children!]) continue
  34. result.splice(i + 1, 0, ...result[i][children!])
  35. }
  36. return result
  37. }
  38. export const findNode = <T = any>(
  39. tree: any,
  40. func: Fn,
  41. config: Partial<TreeHelperConfig> = {}
  42. ): T | null => {
  43. config = getConfig(config)
  44. const { children } = config
  45. const list = [...tree]
  46. for (const node of list) {
  47. if (func(node)) return node
  48. node[children!] && list.push(...node[children!])
  49. }
  50. return null
  51. }
  52. export const findNodeAll = <T = any>(
  53. tree: any,
  54. func: Fn,
  55. config: Partial<TreeHelperConfig> = {}
  56. ): T[] => {
  57. config = getConfig(config)
  58. const { children } = config
  59. const list = [...tree]
  60. const result: T[] = []
  61. for (const node of list) {
  62. func(node) && result.push(node)
  63. node[children!] && list.push(...node[children!])
  64. }
  65. return result
  66. }
  67. export const findPath = <T = any>(
  68. tree: any,
  69. func: Fn,
  70. config: Partial<TreeHelperConfig> = {}
  71. ): T | T[] | null => {
  72. config = getConfig(config)
  73. const path: T[] = []
  74. const list = [...tree]
  75. const visitedSet = new Set()
  76. const { children } = config
  77. while (list.length) {
  78. const node = list[0]
  79. if (visitedSet.has(node)) {
  80. path.pop()
  81. list.shift()
  82. } else {
  83. visitedSet.add(node)
  84. node[children!] && list.unshift(...node[children!])
  85. path.push(node)
  86. if (func(node)) {
  87. return path
  88. }
  89. }
  90. }
  91. return null
  92. }
  93. export const findPathAll = (tree: any, func: Fn, config: Partial<TreeHelperConfig> = {}) => {
  94. config = getConfig(config)
  95. const path: any[] = []
  96. const list = [...tree]
  97. const result: any[] = []
  98. const visitedSet = new Set(),
  99. { children } = config
  100. while (list.length) {
  101. const node = list[0]
  102. if (visitedSet.has(node)) {
  103. path.pop()
  104. list.shift()
  105. } else {
  106. visitedSet.add(node)
  107. node[children!] && list.unshift(...node[children!])
  108. path.push(node)
  109. func(node) && result.push([...path])
  110. }
  111. }
  112. return result
  113. }
  114. export const filter = <T = any>(
  115. tree: T[],
  116. func: (n: T) => boolean,
  117. config: Partial<TreeHelperConfig> = {}
  118. ): T[] => {
  119. config = getConfig(config)
  120. const children = config.children as string
  121. function listFilter(list: T[]) {
  122. return list
  123. .map((node: any) => ({ ...node }))
  124. .filter((node) => {
  125. node[children] = node[children] && listFilter(node[children])
  126. return func(node) || (node[children] && node[children].length)
  127. })
  128. }
  129. return listFilter(tree)
  130. }
  131. export const forEach = <T = any>(
  132. tree: T[],
  133. func: (n: T) => any,
  134. config: Partial<TreeHelperConfig> = {}
  135. ): void => {
  136. config = getConfig(config)
  137. const list: any[] = [...tree]
  138. const { children } = config
  139. for (let i = 0; i < list.length; i++) {
  140. // func 返回true就终止遍历,避免大量节点场景下无意义循环,引起浏览器卡顿
  141. if (func(list[i])) {
  142. return
  143. }
  144. children && list[i][children] && list.splice(i + 1, 0, ...list[i][children])
  145. }
  146. }
  147. /**
  148. * @description: Extract tree specified structure
  149. */
  150. export const treeMap = <T = any>(
  151. treeData: T[],
  152. opt: { children?: string; conversion: Fn }
  153. ): T[] => {
  154. return treeData.map((item) => treeMapEach(item, opt))
  155. }
  156. /**
  157. * @description: Extract tree specified structure
  158. */
  159. export const treeMapEach = (
  160. data: any,
  161. { children = 'children', conversion }: { children?: string; conversion: Fn }
  162. ) => {
  163. const haveChildren = Array.isArray(data[children]) && data[children].length > 0
  164. const conversionData = conversion(data) || {}
  165. if (haveChildren) {
  166. return {
  167. ...conversionData,
  168. [children]: data[children].map((i: number) =>
  169. treeMapEach(i, {
  170. children,
  171. conversion
  172. })
  173. )
  174. }
  175. } else {
  176. return {
  177. ...conversionData
  178. }
  179. }
  180. }
  181. /**
  182. * 递归遍历树结构
  183. * @param treeDatas 树
  184. * @param callBack 回调
  185. * @param parentNode 父节点
  186. */
  187. export const eachTree = (treeDatas: any[], callBack: Fn, parentNode = {}) => {
  188. treeDatas.forEach((element) => {
  189. const newNode = callBack(element, parentNode) || element
  190. if (element.children) {
  191. eachTree(element.children, callBack, newNode)
  192. }
  193. })
  194. }