1
hao
2025-03-27 e610e1c17f62b423a717fadaaa7b139d02857793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;