Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2650,8 +2650,8 @@ export function getTopLevelPathsWithoutContext(db: Database, collectionName: str
// FTS Search
// =============================================================================

function sanitizeFTS5Term(term: string): string {
return term.replace(/[^\p{L}\p{N}']/gu, '').toLowerCase();
export function sanitizeFTS5Term(term: string): string {
return term.replace(/[^\p{L}\p{N}'_]/gu, '').toLowerCase();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions test/store.helpers.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isDocid,
handelize,
cleanupOrphanedVectors,
sanitizeFTS5Term,
} from "../src/store";

// =============================================================================
Expand Down Expand Up @@ -244,3 +245,41 @@ describe("handelize", () => {
expect(isDocid("12345")).toBe(false);
});
});

// =============================================================================
// sanitizeFTS5Term Tests
// =============================================================================

describe("sanitizeFTS5Term", () => {
test("preserves underscores in snake_case identifiers", () => {
expect(sanitizeFTS5Term("my_variable")).toBe("my_variable");
expect(sanitizeFTS5Term("MAX_RETRIES")).toBe("max_retries");
expect(sanitizeFTS5Term("__init__")).toBe("__init__");
});

test("preserves alphanumeric characters", () => {
expect(sanitizeFTS5Term("hello123")).toBe("hello123");
expect(sanitizeFTS5Term("test")).toBe("test");
});

test("preserves apostrophes for contractions", () => {
expect(sanitizeFTS5Term("don't")).toBe("don't");
expect(sanitizeFTS5Term("it's")).toBe("it's");
});

test("strips other punctuation", () => {
expect(sanitizeFTS5Term("hello!")).toBe("hello");
expect(sanitizeFTS5Term("test@value")).toBe("testvalue");
expect(sanitizeFTS5Term("a.b")).toBe("ab");
});

test("lowercases output", () => {
expect(sanitizeFTS5Term("Hello")).toBe("hello");
expect(sanitizeFTS5Term("MY_VAR")).toBe("my_var");
});

test("handles unicode letters and numbers", () => {
expect(sanitizeFTS5Term("café")).toBe("café");
expect(sanitizeFTS5Term("日本語")).toBe("日本語");
});
});
Loading