This commit is contained in:
2025-08-12 15:04:01 +08:00
parent df3f5f5269
commit 6708fb8eeb
544 changed files with 75169 additions and 17 deletions
@@ -0,0 +1,37 @@
import { START_BIN, END_BIN, BINARIES } from './constants';
import Barcode from '../Barcode';
class ITF extends Barcode {
valid() {
return this.data.search(/^([0-9]{2})+$/) !== -1;
}
encode() {
// Calculate all the digit pairs
const encoded = this.data
.match(/.{2}/g)
.map(pair => this.encodePair(pair))
.join('');
return {
data: START_BIN + encoded + END_BIN,
text: this.text
};
}
// Calculate the data of a number pair
encodePair(pair) {
const second = BINARIES[pair[1]];
return BINARIES[pair[0]]
.split('')
.map((first, idx) => (
(first === '1' ? '111' : '1') +
(second[idx] === '1' ? '000' : '0')
))
.join('');
}
}
export default ITF;
@@ -0,0 +1,33 @@
import ITF from './ITF';
// Calculate the checksum digit
const checksum = (data) => {
const res = data
.substr(0, 13)
.split('')
.map(num => parseInt(num, 10))
.reduce((sum, n, idx) => sum + (n * (3 - (idx % 2) * 2)), 0);
return Math.ceil(res / 10) * 10 - res;
};
class ITF14 extends ITF {
constructor(data, options) {
// Add checksum if it does not exist
if (data.search(/^[0-9]{13}$/) !== -1) {
data += checksum(data);
}
super(data, options);
}
valid() {
return (
this.data.search(/^[0-9]{14}$/) !== -1 &&
+this.data[13] === checksum(this.data)
);
}
}
export default ITF14;
@@ -0,0 +1,7 @@
export const START_BIN = '1010';
export const END_BIN = '11101';
export const BINARIES = [
'00110', '10001', '01001', '11000', '00101',
'10100', '01100', '00011', '10010', '01010',
];
@@ -0,0 +1,4 @@
import ITF from './ITF';
import ITF14 from './ITF14';
export { ITF, ITF14 };