Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/sv/nit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 3 additions & 2 deletions src/sv/nit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ 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],
modulus: 11,
}) % 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;
Comment thread
menyinch marked this conversation as resolved.
}

Expand Down