fix: case-sensitive abbreviation matching with word boundaries

- buildAbbreviationsMap now stores exact-case keys (no .toLowerCase())
- splitByAbbreviations regex uses 'g' flag instead of 'gi' — user-defined
  casing is matched exactly (FOSS won't match 'foss' in text)
- Word boundaries (\b) still prevent matching inside longer words
- RoomAbbreviations settings page duplicate check updated to exact match
- Add 19 unit tests covering edge cases: empty map, no-match, start/mid/end
  splits, case sensitivity, word boundaries, longest-match priority
This commit is contained in:
Evie Gauthier committed 2026-03-24 23:33:23 -04:00
1 parent 4460047365
commit 4c1dccf648
3 files changed
+152 -7

No files matched your search

@@ -56,7 +56,7 @@ export function RoomAbbreviations({ requestClose }: AbbreviationsProps) {
const definition = definitionInput.value.trim();
if (!term || !definition) return;
const alreadyExists = entries.some((e) => e.term.toLowerCase() === term.toLowerCase());
const alreadyExists = entries.some((e) => e.term === term);
if (alreadyExists) {
termInput.setCustomValidity('This term already exists.');
termInput.reportValidity();
+145
View File
@@ -0,0 +1,145 @@
import { describe, expect, it } from 'vitest';
import { buildAbbreviationsMap, splitByAbbreviations } from './abbreviations';
describe('buildAbbreviationsMap', () => {
it('returns an empty map for empty entries', () => {
expect(buildAbbreviationsMap([])).toEqual(new Map());
});
it('stores the term with its original casing as the key', () => {
const map = buildAbbreviationsMap([{ term: 'FOSS', definition: 'Free and Open Source Software' }]);
expect(map.get('FOSS')).toBe('Free and Open Source Software');
expect(map.get('foss')).toBeUndefined();
});
it('trims surrounding whitespace from terms', () => {
const map = buildAbbreviationsMap([{ term: ' FOSS ', definition: 'Free and Open Source Software' }]);
expect(map.get('FOSS')).toBe('Free and Open Source Software');
expect(map.size).toBe(1);
});
it('skips empty and whitespace-only terms', () => {
const map = buildAbbreviationsMap([
{ term: '', definition: 'ignored' },
{ term: ' ', definition: 'also ignored' },
]);
expect(map.size).toBe(0);
});
it('treats the same term with different casing as separate entries', () => {
const map = buildAbbreviationsMap([
{ term: 'OSS', definition: 'Open Source Software' },
{ term: 'oss', definition: 'open source software' },
]);
expect(map.size).toBe(2);
expect(map.get('OSS')).toBe('Open Source Software');
expect(map.get('oss')).toBe('open source software');
});
it('stores multiple distinct entries', () => {
const map = buildAbbreviationsMap([
{ term: 'FOSS', definition: 'Free and Open Source Software' },
{ term: 'RTFM', definition: 'Read The Fine Manual' },
]);
expect(map.size).toBe(2);
expect(map.get('FOSS')).toBe('Free and Open Source Software');
expect(map.get('RTFM')).toBe('Read The Fine Manual');
});
});
describe('splitByAbbreviations', () => {
const map = buildAbbreviationsMap([
{ term: 'FOSS', definition: 'Free and Open Source Software' },
{ term: 'RTFM', definition: 'Read The Fine Manual' },
{ term: 'OSS', definition: 'Open Source Software' },
]);
it('returns a single plain segment when the map is empty', () => {
expect(splitByAbbreviations('hello FOSS world', new Map())).toEqual([{ text: 'hello FOSS world' }]);
});
it('returns a single plain segment when there are no matches', () => {
expect(splitByAbbreviations('hello world', map)).toEqual([{ text: 'hello world' }]);
});
it('returns a single plain segment for an empty string', () => {
expect(splitByAbbreviations('', map)).toEqual([{ text: '' }]);
});
it('splits a term match in the middle of a string', () => {
expect(splitByAbbreviations('Use FOSS software', map)).toEqual([
{ text: 'Use ' },
{ text: 'FOSS', termKey: 'FOSS' },
{ text: ' software' },
]);
});
it('splits a term match at the start of a string', () => {
expect(splitByAbbreviations('FOSS is great', map)).toEqual([
{ text: 'FOSS', termKey: 'FOSS' },
{ text: ' is great' },
]);
});
it('splits a term match at the end of a string', () => {
expect(splitByAbbreviations('I love FOSS', map)).toEqual([
{ text: 'I love ' },
{ text: 'FOSS', termKey: 'FOSS' },
]);
});
it('handles multiple terms in the same string', () => {
expect(splitByAbbreviations('FOSS and RTFM', map)).toEqual([
{ text: 'FOSS', termKey: 'FOSS' },
{ text: ' and ' },
{ text: 'RTFM', termKey: 'RTFM' },
]);
});
it('is case-sensitive and does not match the wrong casing', () => {
expect(splitByAbbreviations('I like foss and Foss', map)).toEqual([
{ text: 'I like foss and Foss' },
]);
});
it('does not match a term that is a prefix of a longer word (word boundary)', () => {
// OSS is in the map, but should not match inside FOSS because the F provides no boundary
const ossOnlyMap = buildAbbreviationsMap([{ term: 'OSS', definition: 'Open Source Software' }]);
expect(splitByAbbreviations('FOSS rocks', ossOnlyMap)).toEqual([{ text: 'FOSS rocks' }]);
});
it('does not match a term embedded inside a longer word', () => {
// OSS should not match inside CROSS or GLOSS
const ossOnlyMap = buildAbbreviationsMap([{ term: 'OSS', definition: 'Open Source Software' }]);
expect(splitByAbbreviations('CROSS the GLOSS', ossOnlyMap)).toEqual([{ text: 'CROSS the GLOSS' }]);
});
it('matches a shorter term standalone when the longer overlapping term is also defined', () => {
// OSS is a suffix of FOSS; when standalone it should still match
expect(splitByAbbreviations('OSS is related to FOSS', map)).toEqual([
{ text: 'OSS', termKey: 'OSS' },
{ text: ' is related to ' },
{ text: 'FOSS', termKey: 'FOSS' },
]);
});
it('prefers the longer term when a shorter one is a suffix of it', () => {
// FOSS contains OSS; FOSS should win and OSS should not be matched separately
expect(splitByAbbreviations('Use FOSS today', map)).toEqual([
{ text: 'Use ' },
{ text: 'FOSS', termKey: 'FOSS' },
{ text: ' today' },
]);
});
it('preserves plain text between two consecutive matches', () => {
expect(splitByAbbreviations('FOSS, RTFM, OSS', map)).toEqual([
{ text: 'FOSS', termKey: 'FOSS' },
{ text: ', ' },
{ text: 'RTFM', termKey: 'RTFM' },
{ text: ', ' },
{ text: 'OSS', termKey: 'OSS' },
]);
});
});
+6 -6
View File
@@ -7,12 +7,12 @@ export type RoomAbbreviationsContent = {
entries: AbbreviationEntry[];
};
/** Build a map of lowercase term → definition for O(1) lookup. */
/** Build a map of exact-case term → definition for O(1) lookup. */
export const buildAbbreviationsMap = (entries: AbbreviationEntry[]): Map<string, string> => {
const map = new Map<string, string>();
entries.forEach(({ term, definition }) => {
const t = term.trim();
if (t) map.set(t.toLowerCase(), definition);
if (t) map.set(t, definition);
});
return map;
};
@@ -20,10 +20,10 @@ export const buildAbbreviationsMap = (entries: AbbreviationEntry[]): Map<string,
/**
* Split a plain-text string into alternating [plain, term, plain, term, …] segments.
* Matched terms preserve their original casing from the source string.
* Matching is whole-word and case-insensitive.
* Matching is whole-word and case-sensitive.
*
* Returns an array of `{ text, termKey }` objects where `termKey` is undefined for
* plain segments and is the lowercase lookup key for abbreviation segments.
* plain segments and is the exact-case lookup key for abbreviation segments.
*/
export type TextSegment = {
text: string;
@@ -38,7 +38,7 @@ export const splitByAbbreviations = (text: string, abbrMap: Map<string, string>)
// Sort longest first so "HTTP/2" matches before "HTTP".
const terms = [...abbrMap.keys()].sort((a, b) => b.length - a.length);
const escaped = terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'gi');
const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'g');
const segments: TextSegment[] = [];
let lastIndex = 0;
@@ -49,7 +49,7 @@ export const splitByAbbreviations = (text: string, abbrMap: Map<string, string>)
if (match.index > lastIndex) {
segments.push({ text: text.slice(lastIndex, match.index) });
}
segments.push({ text: match[0], termKey: match[0].toLowerCase() });
segments.push({ text: match[0], termKey: match[0] });
lastIndex = match.index + match[0].length;
}