Skip to content
Merged
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
23 changes: 23 additions & 0 deletions __test__/clean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "bun:test";

import { clean } from "../src/_util/clean";

describe("clean", () => {
test("removes surrounding whitespace", () => {
// Newlines and tabs are not in the separator set, so without the
// trailing trim they would survive and break length/format checks.
expect(clean("988077917\n", " -")).toBe("988077917");
expect(clean("\t 988077917 ", " -")).toBe("988077917");
expect(clean("\r\n12 34\r\n", " -")).toBe("1234");
});

test("preserves internal characters and removes separators", () => {
expect(clean("123-456 789", " -")).toBe("123456789");
expect(clean("a b", "")).toBe("a b");
});

test("normalizes unicode separators before stripping", () => {
// en dash -> ascii hyphen, then removed by the strip set
expect(clean("123–456", " -")).toBe("123456");
});
});
10 changes: 10 additions & 0 deletions __test__/no.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe("no.orgnr", () => {
expect(r.valid).toBe(true);
});

test("valid with surrounding whitespace", () => {
// compact() trims newlines/tabs that the separator set misses.
expect(no.orgnr.validate("988077917\n").valid).toBe(
true,
);
expect(no.orgnr.validate("\t 988077917 ").valid).toBe(
true,
);
});

test("invalid checksum", () => {
const r = no.orgnr.validate("988077918");
expect(r.valid).toBe(false);
Expand Down
9 changes: 7 additions & 2 deletions src/_util/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ const CHAR_REGEX = new RegExp(

/**
* Normalize Unicode artifacts to ASCII
* equivalents and strip the given characters.
* equivalents, strip the given characters, and
* remove surrounding whitespace.
*
* The trailing trim removes whitespace outside the
* separator set (newlines, tabs) so validators
* accept copy-pasted input.
*
* @param value - The input string
* @param strip - Characters to remove
Expand All @@ -82,5 +87,5 @@ export const clean = (
result = result.replaceAll(ch, "");
}
}
return result;
return result.trim();
};
Loading