cdigit
Version:
Collection of check digit algorithms implemented in JavaScript
73 lines (72 loc) • 2.5 kB
JavaScript
/**
* cdigit
*
* @copyright 2018-2021 LiosK
* @license (MIT OR Apache-2.0)
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.verhoeff = void 0;
const common_1 = require("./common");
/**
* Verhoeff algorithm implementation
*
* Note: There is not a firm consensus on the direction (left to right or right
* to left) in which a Verhoeff calculator scans numeric text to construct an
* input digit sequence. This implementation is hard coded to read a string from
* right to left and append the check digit at the rightmost position, which is
* a consistent behavior with other popular implementations. Reverse the input
* string before calling this class' methods if you need to interpret a string
* from left to right.
*/
class Verhoeff {
constructor() {
this.name = "verhoeff";
this.longName = "Verhoeff Algorithm";
/** Verhoeff multiplication table */
this.d = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
];
/** Verhoeff permutation table */
this.p = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
];
/** Verhoeff inverse table */
this.inv = ["0", "4", "3", "2", "1", "5", "6", "7", "8", "9"];
}
compute(num) {
const ds = `${String(num).replace(/[^0-9]/g, "")}0`;
let c = 0;
for (let i = 0, len = ds.length; i < len; i += 1) {
c = this.d[c][this.p[i & 7][Number(ds[len - i - 1])]];
}
return this.inv[c];
}
generate(num) {
return `${num}${this.compute(num)}`;
}
validate(num) {
const [src, cc] = this.parse(num);
return this.compute(src) === cc;
}
parse(num) {
return common_1.helper.parseTail(num, 1);
}
}
exports.verhoeff = new Verhoeff();
;