zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { VNode, VNodeArrayChildren, Fragment, isVNode } from 'vue';
import { isArray } from 'lodash';
 
export function isFragment(node: VNode): boolean;
export function isFragment(node: unknown): node is VNode;
export function isFragment(node: unknown): node is VNode {
  return isVNode(node) && node.type === Fragment;
}
 
export function isComment(node: VNode): boolean;
export function isComment(node: unknown): node is VNode;
export function isComment(node: unknown): node is VNode {
  return isVNode(node) && node.type === Comment;
}
 
export function isValidElementNode(node: VNode): boolean;
export function isValidElementNode(node: unknown): node is VNode;
export function isValidElementNode(node: unknown): node is VNode {
  return isVNode(node) && !isFragment(node) && !isComment(node);
}
 
export function extractChildren(
  children: VNodeArrayChildren,
  parentKey = '',
  extractedChildren: VNode[] = []
) {
  children.forEach((child) => {
    if (isFragment(child)) {
      if (isArray(child.children)) {
        child.children.forEach((nested, key) => {
          if (isFragment(nested) && isArray(nested.children)) {
            extractChildren(nested.children, `${parentKey + key}-`, extractedChildren);
          } else {
            extractedChildren.push(nested as any);
          }
        });
      }
    } else if (isValidElementNode(child)) {
      extractedChildren.push(child);
    }
  });
 
  return extractedChildren;
}