(rootValue: unknown, term: string)
| 151 | * @returns Match and safe rendering metadata for every original entry. |
| 152 | */ |
| 153 | export function searchExplorer(rootValue: unknown, term: string): SearchMatch { |
| 154 | const ancestors = new Set<unknown>() |
| 155 | /** |
| 156 | * Visits one original path with its current ancestors. |
| 157 | * @param value - The entry value. |
| 158 | * @param label - The entry label; absent for the synthetic root. |
| 159 | * @param accessor - Whether reading the entry would invoke a getter. |
| 160 | * @param depth - The current nesting depth. |
| 161 | * @returns The entry's match metadata. |
| 162 | */ |
| 163 | function visit( |
| 164 | value: unknown, |
| 165 | label?: string, |
| 166 | accessor = false, |
| 167 | depth = 0, |
| 168 | ): SearchMatch { |
| 169 | const labelMatch = label?.toLowerCase().includes(term) ?? false |
| 170 | const text = accessor ? undefined : primitiveText(value) |
| 171 | const valueMatch = text?.toLowerCase().includes(term) ?? false |
| 172 | const children = new Map<unknown, SearchMatch>() |
| 173 | const match: SearchMatch = { |
| 174 | label: labelMatch, |
| 175 | value: valueMatch, |
| 176 | hasMatch: labelMatch || valueMatch, |
| 177 | kind: accessor ? 'accessor' : text !== undefined ? 'value' : 'opaque', |
| 178 | children, |
| 179 | } |
| 180 | if (accessor || text !== undefined) return match |
| 181 | if (depth > 500) return match |
| 182 | if (ancestors.has(value)) { |
| 183 | match.kind = 'circular' |
| 184 | return match |
| 185 | } |
| 186 | const entries = searchEntries(value) |
| 187 | if (!entries) return match |
| 188 | match.kind = 'branch' |
| 189 | ancestors.add(value) |
| 190 | for (const entry of entries) { |
| 191 | const child = visit(entry.value, entry.label, entry.accessor, depth + 1) |
| 192 | children.set(entry.key, child) |
| 193 | match.hasMatch ||= child.hasMatch |
| 194 | } |
| 195 | ancestors.delete(value) |
| 196 | return match |
| 197 | } |
| 198 | return visit(rootValue) |
| 199 | } |
no test coverage detected