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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Encoding documentation:
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
 
import { EAN13_STRUCTURE } from './constants';
import EAN from './EAN';
 
// Calculate the checksum digit
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
const checksum = (number) => {
    const res = number
        .substr(0, 12)
        .split('')
        .map((n) => +n)
        .reduce((sum, a, idx) => (
            idx % 2 ? sum + a * 3 : sum + a
        ), 0);
 
    return (10 - (res % 10)) % 10;
};
 
class EAN13 extends EAN {
 
    constructor(data, options) {
        // Add checksum if it does not exist
        if (data.search(/^[0-9]{12}$/) !== -1) {
            data += checksum(data);
        }
 
        super(data, options);
 
        // Adds a last character to the end of the barcode
        this.lastChar = options.lastChar;
    }
 
    valid() {
        return (
            this.data.search(/^[0-9]{13}$/) !== -1 &&
            +this.data[12] === checksum(this.data)
        );
    }
 
    leftText() {
        return super.leftText(1, 6);
    }
 
    leftEncode() {
        const data = this.data.substr(1, 6);
        const structure = EAN13_STRUCTURE[this.data[0]];
        return super.leftEncode(data, structure);
    }
 
    rightText() {
        return super.rightText(7, 6);
    }
 
    rightEncode() {
        const data = this.data.substr(7, 6);
        return super.rightEncode(data, 'RRRRRR');
    }
 
    // The "standard" way of printing EAN13 barcodes with guard bars
    encodeGuarded() {
        const data = super.encodeGuarded();
 
        // Extend data with left digit & last character
        if (this.options.displayValue) {
            data.unshift({
                data: '000000000000',
                text: this.text.substr(0, 1),
                options: { textAlign: 'left', fontSize: this.fontSize }
            });
 
            if (this.options.lastChar) {
                data.push({
                    data: '00'
                });
                data.push({
                    data: '00000',
                    text: this.options.lastChar,
                    options: { fontSize: this.fontSize }
                });
            }
        }
 
        return data;
    }
 
}
 
export default EAN13;