Files
t3code/packages/shared/src/threadSearch.ts
T
12069eefd7 feat(web): find messages and plans in the current thread (#10439)
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-08 08:24:50 -05:00

71 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
interface ThreadSearchTextPart {
readonly text: string;
readonly highlighted: boolean;
readonly start: number;
}
function foldThreadSearchCase(text: string): string {
const lowered = text.toLowerCase().replace(/ς/g, "σ");
if (lowered.length === text.length) return lowered;
let folded = "";
for (const character of text) {
const loweredCharacter = character.toLowerCase().replace(/ς/g, "σ");
folded += loweredCharacter.length === character.length ? loweredCharacter : character;
}
return folded;
}
function scanThreadSearchOccurrences(text: string, query: string, offsets?: number[]): number {
if (text.length === 0 || query.length === 0) return 0;
const foldedText = foldThreadSearchCase(text);
const foldedQuery = foldThreadSearchCase(query);
let count = 0;
let cursor = 0;
while (cursor <= foldedText.length - foldedQuery.length) {
const offset = foldedText.indexOf(foldedQuery, cursor);
if (offset === -1) break;
offsets?.push(offset);
count += 1;
cursor = offset + foldedQuery.length;
}
return count;
}
export function countThreadSearchOccurrences(text: string, query: string): number {
return scanThreadSearchOccurrences(text, query);
}
export function findThreadSearchOccurrences(text: string, query: string): number[] {
const offsets: number[] = [];
scanThreadSearchOccurrences(text, query, offsets);
return offsets;
}
export function splitThreadSearchText(text: string, query: string): ThreadSearchTextPart[] {
const normalizedQuery = query.trim();
if (normalizedQuery.length === 0) {
return [{ text, highlighted: false, start: 0 }];
}
const parts: ThreadSearchTextPart[] = [];
let cursor = 0;
for (const offset of findThreadSearchOccurrences(text, normalizedQuery)) {
if (offset > cursor) {
parts.push({ text: text.slice(cursor, offset), highlighted: false, start: cursor });
}
parts.push({
text: text.slice(offset, offset + normalizedQuery.length),
highlighted: true,
start: offset,
});
cursor = offset + normalizedQuery.length;
}
if (cursor < text.length) {
parts.push({ text: text.slice(cursor), highlighted: false, start: cursor });
}
return parts;
}