How to implement the check digit algorithm specified in ISO 6346:2022 Freight containers — Coding, identification and marking?
See how is the check digit of a container calculated?
This is a self-answered question.
How to implement the check digit algorithm specified in ISO 6346:2022 Freight containers — Coding, identification and marking?
See how is the check digit of a container calculated?
This is a self-answered question.
On
Alternatively, using map and reduce:
function iso6346CheckDigit(csc) {
csc = csc.toUpperCase().replace(/[^A-Z0-9]+/g, '');
// Check if length fits requirements.
if (csc.length < 10 || csc.length > 11)
return NaN;
return Array(10).fill().map((_, i) => csc.charCodeAt(i))
.map(a => a -= a < 58 ? 48 : 55)
.map(a => a + (a-1) / 10)
.map((a, i) => a << i)
.reduce((a, b) => a + b) % 11 % 10;
}
console.log(iso6346CheckDigit('ZEPU 003725 [5]')); // 5
console.log(iso6346CheckDigit('CEBU 100107 [5]')); // 5
console.log(iso6346CheckDigit('TEXU 307007 [9]')); // 9
console.log(iso6346CheckDigit('ABCD 123456 [0]')); // 0
This calculates the check digit as per ISO 6346:2022:
The above has been published on GitHub.