From d145ebcb8ed011968506df8c4d68b6a42497be76 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 19 Nov 2026 18:59:45 +0530 Subject: [PATCH] fix(nl/btw, se/personnummer): return invalid for empty input instead of throwing validate('') threw a TypeError in both validators. clean() destructured strings.splitAt(), which drops empty parts, so the first part was undefined for an empty (or very short) value. Use plain slices there. --- src/nl/btw.spec.ts | 6 ++++++ src/nl/btw.ts | 6 +++--- src/se/personnummer.spec.ts | 6 ++++++ src/se/personnummer.ts | 9 ++++++--- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/nl/btw.spec.ts b/src/nl/btw.spec.ts index b20c9f8c..c3a628ca 100644 --- a/src/nl/btw.spec.ts +++ b/src/nl/btw.spec.ts @@ -51,4 +51,10 @@ describe('nl/btw', () => { expect(result.error).toBeInstanceOf(InvalidChecksum); }); + + it('validate:empty', () => { + for (const input of ['', ' ', 'NL', '-']) { + expect(validate(input).error).toBeInstanceOf(InvalidLength); + } + }); }); diff --git a/src/nl/btw.ts b/src/nl/btw.ts index e189d0bc..6996f1dc 100644 --- a/src/nl/btw.ts +++ b/src/nl/btw.ts @@ -31,9 +31,9 @@ function clean(input: string): ReturnType { return [value, err]; } - const [a, b] = strings.splitAt(value, -3); - - return [`${a.padStart(9, '0')}${b}`, null]; + // Plain slices: splitAt drops empty parts, so a short value left `a` + // undefined and threw. + return [`${value.slice(0, -3).padStart(9, '0')}${value.slice(-3)}`, null]; } const impl: Validator = { diff --git a/src/se/personnummer.spec.ts b/src/se/personnummer.spec.ts index 50684aed..8e9b63f8 100644 --- a/src/se/personnummer.spec.ts +++ b/src/se/personnummer.spec.ts @@ -137,4 +137,10 @@ describe('se/personnummer', () => { expect(result.error).toBeInstanceOf(InvalidChecksum); }); + + it('validate:empty', () => { + for (const input of ['', ' ', ':']) { + expect(validate(input).error).toBeInstanceOf(InvalidLength); + } + }); }); diff --git a/src/se/personnummer.ts b/src/se/personnummer.ts index 661bfe2c..bb7a417c 100644 --- a/src/se/personnummer.ts +++ b/src/se/personnummer.ts @@ -46,9 +46,12 @@ function clean(input: string): ReturnType { ? `${value.slice(0, -4)}-${value.slice(-4)}` : value; - const [a, b, c] = strings.splitAt(separated, -5, -4); - - return [`${a.replace(/[-+]/g, '')}${b}${c}`, null]; + // Plain slices: splitAt drops empty parts, so a short value left `a` + // undefined and threw. + return [ + `${separated.slice(0, -5).replace(/[-+]/g, '')}${separated.slice(-5)}`, + null, + ]; } /**