Files
admin-web/src/modules/trucking/iso6346.ts
T

18 lines
780 B
TypeScript

const letterValues = [10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38]
export function normalizeIso6346ContainerNumber(value: string | null | undefined) {
return value?.replace(/[\s\u00a0\u3000]+/g, '').toUpperCase() || ''
}
export function isIso6346ContainerNumber(value: string | null | undefined) {
const normalized = normalizeIso6346ContainerNumber(value)
if (!/^[A-Z]{3}[UJZ][0-9]{7}$/.test(normalized)) return false
let sum = 0
for (let index = 0; index < 10; index += 1) {
const current = normalized[index]!
const number = /[0-9]/.test(current) ? Number(current) : letterValues[current.charCodeAt(0) - 65]!
sum += number * (2 ** index)
}
return Number(normalized[10]) === (sum % 11) % 10
}