(
children: IPublicTypeNodeSchema['children'],
handlers: {
string?: (i: string) => T;
expression?: (i: IPublicTypeJSExpression) => T;
node?: (i: IPublicTypeNodeSchema) => T;
},
options?: {
rerun?: boolean;
maxDepth?: number; // 防止出现死循环无穷递归
},
)
| 44 | * @returns |
| 45 | */ |
| 46 | export function handleSubNodes<T>( |
| 47 | children: IPublicTypeNodeSchema['children'], |
| 48 | handlers: { |
| 49 | string?: (i: string) => T; |
| 50 | expression?: (i: IPublicTypeJSExpression) => T; |
| 51 | node?: (i: IPublicTypeNodeSchema) => T; |
| 52 | }, |
| 53 | options?: { |
| 54 | rerun?: boolean; |
| 55 | maxDepth?: number; // 防止出现死循环无穷递归 |
| 56 | }, |
| 57 | ): T[] { |
| 58 | const opt = { |
| 59 | ...handleChildrenDefaultOptions, |
| 60 | ...(options || {}), |
| 61 | }; |
| 62 | const maxDepth = opt.maxDepth ?? DEFAULT_MAX_DEPTH; |
| 63 | if (maxDepth <= 0) { |
| 64 | throw new Error('handleSubNodes maxDepth reached'); |
| 65 | } |
| 66 | |
| 67 | if (Array.isArray(children)) { |
| 68 | const list: IPublicTypeNodeData[] = children as IPublicTypeNodeData[]; |
| 69 | return list |
| 70 | .map((child) => handleSubNodes(child, handlers, { ...opt, maxDepth: maxDepth - 1 })) |
| 71 | .reduce((p, c) => p.concat(c), []); |
| 72 | } |
| 73 | |
| 74 | let result: T | undefined; |
| 75 | const childrenRes: T[] = []; |
| 76 | if (children === null || children === undefined) { |
| 77 | return []; |
| 78 | } else if (isDOMText(children)) { |
| 79 | const handler = handlers.string || noop; |
| 80 | result = handler(children); |
| 81 | } else if (isJSExpression(children)) { |
| 82 | const handler = handlers.expression || noop; |
| 83 | result = handler(children); |
| 84 | } else if (isJSSlot(children)) { |
| 85 | return handleSubNodes(children.value, handlers, { ...opt, maxDepth: maxDepth - 1 }); |
| 86 | } else if (isNodeSchema(children)) { |
| 87 | const handler = handlers.node || noop; |
| 88 | const child = children as IPublicTypeNodeSchema; |
| 89 | result = handler(child); |
| 90 | |
| 91 | if (child.children) { |
| 92 | const childRes = handleSubNodes(child.children, handlers, opt); |
| 93 | childrenRes.push(...childRes); |
| 94 | } |
| 95 | |
| 96 | if (child.props) { |
| 97 | if (Array.isArray(child.props)) { |
| 98 | child.props.forEach(({ value }) => { |
| 99 | const childRes = handleCompositeValueInProps(value); |
| 100 | childrenRes.push(...childRes); |
| 101 | }); |
| 102 | } else { |
| 103 | Object.values(child.props).forEach((value) => { |
no test coverage detected
searching dependent graphs…