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
21 changes: 21 additions & 0 deletions src/ar/cbu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,25 @@ describe('ar/cbu', () => {

expect(result.error).toBeInstanceOf(InvalidChecksum);
});

// Regression: a check digit of 0 must be accepted. `10 - weightedSum(...)` yields
// 10 (not 0) when the weighted sum is 0, so without the `% 10` wrap these valid
// numbers were wrongly rejected as InvalidChecksum.
it('validate:0720429088000002339140', () => {
const result = validate('0720429088000002339140');

expect(result.isValid && result.compact).toEqual('0720429088000002339140');
});

it('validate:0000000000000000000000', () => {
const result = validate('0000000000000000000000');

expect(result.isValid && result.compact).toEqual('0000000000000000000000');
});

it('validate:0720429088000002339141', () => {
const result = validate('0720429088000002339141');

expect(result.error).toBeInstanceOf(InvalidChecksum);
});
});
10 changes: 6 additions & 4 deletions src/ar/cbu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,22 @@ const impl: Validator = {
const [front, c1, back, c2] = strings.splitAt(value, 7, 8, 21);

const s1 = String(
10 -
(10 -
weightedSum(front, {
reverse: true,
weights: [3, 1, 7, 9, 3, 1, 7],
modulus: 10,
}),
})) %
10,
);
const s2 = String(
10 -
(10 -
weightedSum(back, {
reverse: true,
weights: [3, 1, 7, 9, 3, 1, 7, 9, 3, 1, 7, 9, 3, 1],
modulus: 10,
}),
})) %
10,
);

if (s1 !== c1 || s2 !== c2) {
Expand Down
Loading