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
11 changes: 8 additions & 3 deletions src/scorers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ export function result(
};
}

/** Normalize text for lenient comparisons (trim + collapse whitespace). */
/** Normalize text for lenient comparisons.
* When `trim` is true (default): strip ends and collapse internal whitespace.
* When `trim` is false: leave whitespace untouched (only case folding may apply).
*/
export function normalize(text: string, opts: { caseSensitive?: boolean; trim?: boolean } = {}): string {
let out = text;
if (opts.trim !== false) out = out.trim();
if (opts.trim !== false) {
out = out.trim().replace(/\s+/g, " ");
}
if (!opts.caseSensitive) out = out.toLowerCase();
return out.replace(/\s+/g, " ");
return out;
}
11 changes: 11 additions & 0 deletions tests/scorers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,14 @@ describe("rubric", () => {
expect(r.passed).toBe(true);
});
});

describe("exact-match trim:false preserves internal whitespace", () => {
it("matches when internal spaces are identical", async () => {
const r = await run(exactMatchScorer, { type: "exact-match", expected: "a b", trim: false }, ctx("a b"));
expect(r.passed).toBe(true);
});
it("fails when internal whitespace differs", async () => {
const r = await run(exactMatchScorer, { type: "exact-match", expected: "a b", trim: false }, ctx("a b"));
expect(r.passed).toBe(false);
});
});
Loading