javascript 如何根据ISO 6346计算校验位

fivyi3re  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(168)

如何实现ISO 6346:2022 Freight containers — Coding, identification and marking中指定的校验位算法?
参见 how is the check digit of a container calculated?

这是一个自我回答的问题

hjzp0vay

hjzp0vay1#

这将根据 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

以上内容已在GitHub上发布。

c8ib6hqw

c8ib6hqw2#

或者,使用mapreduce

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

相关问题