Skip to content

Commit b8142cf

Browse files
committed
test(fieldNorm): cover full separator set; narrow "any whitespace" comment
Follow-up to #836. The isWordSeparator comment claimed "any whitespace character" but the predicate only covers common ASCII whitespace plus NBSP, not the full Unicode \s set (ideographic space, en/em spaces, U+2028/2029, etc.). Narrow the wording so the scope reads as deliberate rather than an oversight, and add assertions for CR, VT, FF, and NBSP to lock in the set the predicate actually claims.
1 parent 6fe85b0 commit b8142cf

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/tools/fieldNorm.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { NormInterface } from '../types'
22

3-
// Whitespace charCodes treated as word separators by the counter below:
4-
// tab (9), newline (10), vertical tab (11), form feed (12), CR (13),
5-
// space (32), non-breaking space (160).
3+
// charCodes we treat as word separators: the common ASCII whitespace
4+
// (tab 9, LF 10, VT 11, FF 12, CR 13, space 32) plus NBSP (160). This is
5+
// deliberately narrower than the full Unicode whitespace set (\s); it skips
6+
// rarer separators like the ideographic space and en/em spaces, which the
7+
// field-norm heuristic is too coarse to benefit from distinguishing.
68
function isWordSeparator(code: number): boolean {
79
return (code >= 9 && code <= 13) || code === 32 || code === 160
810
}
@@ -24,10 +26,10 @@ export default function norm(
2426
// transition-counter (starting at 1 and incrementing on every boundary)
2527
// would over-count by 1 for each stray boundary.
2628
//
27-
// A separator is any whitespace character (space, tab, newline, CR,
28-
// vertical tab, form feed, or non-breaking space) — not just plain
29-
// ASCII space. Checking charCode 32 alone missed tabs and newlines, so
30-
// e.g. a tab- or newline-joined field was scored as a single word.
29+
// A separator here is common ASCII whitespace (space, tab, newline, CR,
30+
// vertical tab, form feed) plus NBSP, not the full Unicode set. Checking
31+
// charCode 32 alone missed tabs and newlines, so a tab- or newline-joined
32+
// field was scored as a single word.
3133
let numTokens = 0
3234
let inWord = false
3335
for (let i = 0; i < value.length; i++) {

test/internals.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ describe('fieldNorm', () => {
5656
expect(n.get('one\ttwo\tthree')).toBe(n.get('one two three'))
5757
expect(n.get('one\ntwo\nthree')).toBe(n.get('one two three'))
5858
})
59+
60+
test('covers the rest of the separator set: CR, VT, FF, and NBSP', () => {
61+
const n = norm(1, 3)
62+
// Lock in the full set isWordSeparator claims: carriage return, vertical
63+
// tab, form feed, and non-breaking space all count as word boundaries.
64+
expect(n.get('hello\rworld')).toBe(n.get('hello world'))
65+
expect(n.get('hello\vworld')).toBe(n.get('hello world'))
66+
expect(n.get('hello\fworld')).toBe(n.get('hello world'))
67+
expect(n.get('hello world')).toBe(n.get('hello world'))
68+
})
5969
})
6070

6171
describe('InvertedIndex', () => {

0 commit comments

Comments
 (0)