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
2条答案
按热度按时间hjzp0vay1#
这将根据 ISO 6346:2022 计算校验位:
以上内容已在GitHub上发布。
c8ib6hqw2#
或者,使用map和reduce: