11import { objectOmit } from './object' ;
2- import { AnyObject , isEmpty , isObject , objectHas } from './type' ;
2+ import { AnyObject , isEmpty , isNodeList , isObject , objectHas } from './type' ;
33
44export interface IFieldOptions {
55 keyField : string ;
@@ -26,7 +26,7 @@ export interface IFilterCondition<V> {
2626}
2727
2828/**
29- * 树遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
29+ * 树遍历函数(支持continue和break操作), 可用于遍历Array和NodeList类型的数据
3030 * @param {ArrayLike<V> } tree 树形数据
3131 * @param {Function } iterator 迭代函数, 返回值为true时continue, 返回值为false时break
3232 * @param {options } options 支持定制子元素名称、反向遍历、广度优先遍历,默认{
@@ -46,13 +46,19 @@ export function forEachDeep<V>(
4646 parent : V | null ,
4747 level : number
4848 ) => boolean | void ,
49- options : { childField ?: string ; reverse ?: boolean ; breadthFirst ?: boolean } = {
49+ options : { childField ?: string ; reverse ?: boolean ; breadthFirst ?: boolean ; isDomNode ?: boolean } = {
5050 childField : 'children' ,
5151 reverse : false ,
52- breadthFirst : false
52+ breadthFirst : false ,
53+ isDomNode : false
5354 }
5455) : void {
55- const { childField = 'children' , reverse = false , breadthFirst = false } = isObject ( options ) ? options : { } ;
56+ const {
57+ childField = 'children' ,
58+ reverse = false ,
59+ breadthFirst = false ,
60+ isDomNode = false
61+ } = isObject ( options ) ? options : { } ;
5662 let isBreak = false ;
5763 const queue : {
5864 item : V ;
@@ -80,7 +86,7 @@ export function forEachDeep<V>(
8086 } else if ( re === true ) {
8187 continue ;
8288 }
83- if ( item && Array . isArray ( item [ childField ] ) ) {
89+ if ( item && ( isDomNode ? isNodeList ( item [ childField ] ) : Array . isArray ( item [ childField ] ) ) ) {
8490 walk ( item [ childField ] , item , level + 1 ) ;
8591 }
8692 }
@@ -98,7 +104,7 @@ export function forEachDeep<V>(
98104 continue ;
99105 }
100106
101- if ( item && Array . isArray ( item [ childField ] ) ) {
107+ if ( item && ( isDomNode ? isNodeList ( item [ childField ] ) : Array . isArray ( item [ childField ] ) ) ) {
102108 walk ( item [ childField ] , item , level + 1 ) ;
103109 }
104110 }
@@ -122,7 +128,7 @@ export function forEachDeep<V>(
122128 continue ;
123129 }
124130
125- if ( item && Array . isArray ( item [ childField ] ) ) {
131+ if ( item && ( isDomNode ? isNodeList ( item [ childField ] ) : Array . isArray ( item [ childField ] ) ) ) {
126132 walk ( item [ childField ] , item , level + 1 ) ;
127133 }
128134 }
@@ -140,7 +146,7 @@ export function forEachDeep<V>(
140146 continue ;
141147 }
142148
143- if ( item && Array . isArray ( item [ childField ] ) ) {
149+ if ( item && ( isDomNode ? isNodeList ( item [ childField ] ) : Array . isArray ( item [ childField ] ) ) ) {
144150 walk ( item [ childField ] , item , level + 1 ) ;
145151 }
146152 }
0 commit comments