| 116 | * @returns Text that contains a lowercased term exactly when `searchExplorer` matches it. |
| 117 | */ |
| 118 | export function createSearchText(rootValue: unknown): string { |
| 119 | const rootText = primitiveText(rootValue) |
| 120 | if (rootText !== undefined) return rootText.toLowerCase() |
| 121 | const parts: Array<string> = [] |
| 122 | const seen = new Set<unknown>([rootValue]) |
| 123 | const queue: Array<{ value: unknown; depth: number }> = [ |
| 124 | { value: rootValue, depth: 0 }, |
| 125 | ] |
| 126 | // Also visits the entries pushed while iterating |
| 127 | for (const { value, depth } of queue) { |
| 128 | for (const entry of searchEntries(value) ?? []) { |
| 129 | parts.push(entry.label.toLowerCase()) |
| 130 | if (entry.accessor) continue |
| 131 | const text = primitiveText(entry.value) |
| 132 | if (text !== undefined) { |
| 133 | parts.push(text.toLowerCase()) |
| 134 | } else if ( |
| 135 | depth < 500 && |
| 136 | typeof entry.value === 'object' && |
| 137 | !seen.has(entry.value) |
| 138 | ) { |
| 139 | seen.add(entry.value) |
| 140 | queue.push({ value: entry.value, depth: depth + 1 }) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return parts.join(SEARCH_TEXT_SEPARATOR) |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Builds one search tree, stopping ancestor cycles while visiting shared siblings. |