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, + ]; } /**