ean13: fix edge case on checksum calculating

master
Lauren Liberda 2021-07-23 18:31:11 +02:00
parent b369daa4af
commit 28cca74755
2 changed files with 10 additions and 1 deletions

View File

@ -11,7 +11,7 @@ export class EANService {
.map((d) => parseInt(d, 10))
.map((d, i) => d * (i % 2 === 0 ? 1 : 3))
.reduce((s, d) => s + d, 0);
while (sum >= 10) {
while (sum > 10) {
sum -= 10;
}
return 10 - sum;

View File

@ -6,4 +6,13 @@ describe('EANService', () => {
const sum = eans.calcChecksum('400638133393');
expect(sum).toBe(1);
});
test('always returns num of correct length', () => {
const initial = 140000000000n;
for (let i = 0; i < 100; i += 1) {
const ean = eans.fromID((initial + BigInt(i)).toString(10));
if (ean.length !== 13) console.log(ean);
expect(ean.length).toBe(13);
}
});
});