diff --git a/src/sv/nit.spec.ts b/src/sv/nit.spec.ts index 31aaa67d..d9094def 100644 --- a/src/sv/nit.spec.ts +++ b/src/sv/nit.spec.ts @@ -31,4 +31,24 @@ describe('sv/nit', () => { expect(result.error).toBeInstanceOf(InvalidChecksum); }); + + it('validate:0614-050707-001-1 (old NIT, sequential below 100)', () => { + // Sequential '001' <= '100' must use the old-NIT weights + const result = validate('0614-050707-001-1'); + + expect(result.isValid && result.compact).toEqual('06140507070011'); + }); + + it('validate:0614-060707-101-0 (new NIT, weighted sum divisible by 11)', () => { + // Weighted sum % 11 === 0, so the check digit is (-0 % 11) % 10 = 0 + const result = validate('0614-060707-101-0'); + + expect(result.isValid && result.compact).toEqual('06140607071010'); + }); + + it('validate:0614-060707-101-1', () => { + const result = validate('0614-060707-101-1'); + + expect(result.error).toBeInstanceOf(InvalidChecksum); + }); }); diff --git a/src/sv/nit.ts b/src/sv/nit.ts index 8e3fce36..631dfec2 100644 --- a/src/sv/nit.ts +++ b/src/sv/nit.ts @@ -84,7 +84,7 @@ const impl: Validator = { let sum; const [front, check] = strings.splitAt(value, 13); - if (value.substr(10, 3) === '100') { + if (value.slice(10, 13) <= '100') { sum = weightedSum(front, { weights: [14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2], @@ -92,11 +92,12 @@ const impl: Validator = { }) % 10; } else { sum = - (11 - + ((11 - weightedSum(front, { weights: [2, 7, 6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2], modulus: 11, })) % + 11) % 10; }