( field: FieldTree<unknown>, value: unknown, filter?: ExtractFilter, )
| 118 | } |
| 119 | |
| 120 | function extractChildren( |
| 121 | field: FieldTree<unknown>, |
| 122 | value: unknown, |
| 123 | filter?: ExtractFilter, |
| 124 | ): unknown { |
| 125 | if (isArray(value)) { |
| 126 | const record = field as unknown as Record<number, FieldTree<unknown>>; |
| 127 | const arrayValue = value as readonly FieldTree<unknown>[]; |
| 128 | const result: unknown[] = new Array(arrayValue.length); |
| 129 | let hasMatch = false; |
| 130 | |
| 131 | for (let i = 0; i < arrayValue.length; i++) { |
| 132 | const child = record[i]; |
| 133 | |
| 134 | const childResult = visitFieldTree(child, filter); |
| 135 | if (childResult !== undefined) { |
| 136 | hasMatch = true; |
| 137 | } |
| 138 | result[i] = childResult; |
| 139 | } |
| 140 | |
| 141 | return hasMatch ? result : undefined; |
| 142 | } |
| 143 | |
| 144 | if (isObject(value)) { |
| 145 | const record = field as unknown as Record<string, unknown>; |
| 146 | const objectValue = value as Record<string, unknown>; |
| 147 | const entries = Object.keys(objectValue) |
| 148 | .map<[string, FieldTree<unknown>] | undefined>((key) => { |
| 149 | const child = record[key]; |
| 150 | return isFieldTreeNode(child) ? [key, child] : undefined; |
| 151 | }) |
| 152 | .filter(isKeyedChild) |
| 153 | .map(([key, child]) => { |
| 154 | const childResult = visitFieldTree(child, filter); |
| 155 | return childResult !== undefined ? ([key, childResult] as [string, unknown]) : undefined; |
| 156 | }) |
| 157 | .filter((v) => v !== undefined); |
| 158 | |
| 159 | return entries.length ? Object.fromEntries(entries) : undefined; |
| 160 | } |
| 161 | |
| 162 | return undefined; |
| 163 | } |
| 164 | |
| 165 | function isFieldTreeNode(value: unknown): value is FieldTree<unknown> { |
| 166 | return typeof value === 'function'; |
no test coverage detected
searching dependent graphs…