2

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.

Damien
  • 1,140
  • 15
  • 22

2 Answers2

1

This calculates the check digit as per ISO 6346:2022:

function iso6346CheckDigit(csc) {
  // Remove any non-alphanumeric characters and convert to upper-case.
  csc = csc.toUpperCase().replace(/[^A-Z0-9]+/g, '');

  // Check if length fits requirements.
  if (csc.length < 10 || csc.length > 11)
    return NaN;

  // Calculate check digit.
  var sum = 0;
  for (let i = 0; i < 10; i++) {

    // Map letters to numbers.
    let n = csc.charCodeAt(i);
    n -= n < 58 ? 48 : 55;

    // Numbers 11, 22, 33 are omitted.
    n += (n-1) / 10;

    // Sum of all numbers multiplied by weighting.
    sum += n << i;
  }

  // Modulus of 11, and map 10 to 0.
  return sum % 11 % 10;
}

console.log(iso6346CheckDigit('ZEPU 003725 [5]')); // 5
console.log(iso6346CheckDigit('GVTU 300038 [9]')); // 9
console.log(iso6346CheckDigit('TEXU 307007 [9]')); // 9
console.log(iso6346CheckDigit('GABU 307007 [0]')); // 0
console.log(iso6346CheckDigit('BAAD 10000.....')); // NaN

The above has been published on GitHub.

Damien
  • 1,140
  • 15
  • 22
0

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
Damien
  • 1,140
  • 15
  • 22