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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _encoder = require('./encoder');
 
var _encoder2 = _interopRequireDefault(_encoder);
 
var _Barcode2 = require('../Barcode.js');
 
var _Barcode3 = _interopRequireDefault(_Barcode2);
 
var _UPC = require('./UPC.js');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // Encoding documentation:
// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
//
// UPC-E documentation:
// https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E
 
var EXPANSIONS = ["XX00000XXX", "XX10000XXX", "XX20000XXX", "XXX00000XX", "XXXX00000X", "XXXXX00005", "XXXXX00006", "XXXXX00007", "XXXXX00008", "XXXXX00009"];
 
var PARITIES = [["EEEOOO", "OOOEEE"], ["EEOEOO", "OOEOEE"], ["EEOOEO", "OOEEOE"], ["EEOOOE", "OOEEEO"], ["EOEEOO", "OEOOEE"], ["EOOEEO", "OEEOOE"], ["EOOOEE", "OEEEOO"], ["EOEOEO", "OEOEOE"], ["EOEOOE", "OEOEEO"], ["EOOEOE", "OEEOEO"]];
 
var UPCE = function (_Barcode) {
    _inherits(UPCE, _Barcode);
 
    function UPCE(data, options) {
        _classCallCheck(this, UPCE);
 
        var _this = _possibleConstructorReturn(this, (UPCE.__proto__ || Object.getPrototypeOf(UPCE)).call(this, data, options));
        // Code may be 6 or 8 digits;
        // A 7 digit code is ambiguous as to whether the extra digit
        // is a UPC-A check or number system digit.
 
 
        _this.isValid = false;
        if (data.search(/^[0-9]{6}$/) !== -1) {
            _this.middleDigits = data;
            _this.upcA = expandToUPCA(data, "0");
            _this.text = options.text || '' + _this.upcA[0] + data + _this.upcA[_this.upcA.length - 1];
            _this.isValid = true;
        } else if (data.search(/^[01][0-9]{7}$/) !== -1) {
            _this.middleDigits = data.substring(1, data.length - 1);
            _this.upcA = expandToUPCA(_this.middleDigits, data[0]);
 
            if (_this.upcA[_this.upcA.length - 1] === data[data.length - 1]) {
                _this.isValid = true;
            } else {
                // checksum mismatch
                return _possibleConstructorReturn(_this);
            }
        } else {
            return _possibleConstructorReturn(_this);
        }
 
        _this.displayValue = options.displayValue;
 
        // Make sure the font is not bigger than the space between the guard bars
        if (options.fontSize > options.width * 10) {
            _this.fontSize = options.width * 10;
        } else {
            _this.fontSize = options.fontSize;
        }
 
        // Make the guard bars go down half the way of the text
        _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
        return _this;
    }
 
    _createClass(UPCE, [{
        key: 'valid',
        value: function valid() {
            return this.isValid;
        }
    }, {
        key: 'encode',
        value: function encode() {
            if (this.options.flat) {
                return this.flatEncoding();
            } else {
                return this.guardedEncoding();
            }
        }
    }, {
        key: 'flatEncoding',
        value: function flatEncoding() {
            var result = "";
 
            result += "101";
            result += this.encodeMiddleDigits();
            result += "010101";
 
            return {
                data: result,
                text: this.text
            };
        }
    }, {
        key: 'guardedEncoding',
        value: function guardedEncoding() {
            var result = [];
 
            // Add the UPC-A number system digit beneath the quiet zone
            if (this.displayValue) {
                result.push({
                    data: "00000000",
                    text: this.text[0],
                    options: { textAlign: "left", fontSize: this.fontSize }
                });
            }
 
            // Add the guard bars
            result.push({
                data: "101",
                options: { height: this.guardHeight }
            });
 
            // Add the 6 UPC-E digits
            result.push({
                data: this.encodeMiddleDigits(),
                text: this.text.substring(1, 7),
                options: { fontSize: this.fontSize }
            });
 
            // Add the end bits
            result.push({
                data: "010101",
                options: { height: this.guardHeight }
            });
 
            // Add the UPC-A check digit beneath the quiet zone
            if (this.displayValue) {
                result.push({
                    data: "00000000",
                    text: this.text[7],
                    options: { textAlign: "right", fontSize: this.fontSize }
                });
            }
 
            return result;
        }
    }, {
        key: 'encodeMiddleDigits',
        value: function encodeMiddleDigits() {
            var numberSystem = this.upcA[0];
            var checkDigit = this.upcA[this.upcA.length - 1];
            var parity = PARITIES[parseInt(checkDigit)][parseInt(numberSystem)];
            return (0, _encoder2.default)(this.middleDigits, parity);
        }
    }]);
 
    return UPCE;
}(_Barcode3.default);
 
function expandToUPCA(middleDigits, numberSystem) {
    var lastUpcE = parseInt(middleDigits[middleDigits.length - 1]);
    var expansion = EXPANSIONS[lastUpcE];
 
    var result = "";
    var digitIndex = 0;
    for (var i = 0; i < expansion.length; i++) {
        var c = expansion[i];
        if (c === 'X') {
            result += middleDigits[digitIndex++];
        } else {
            result += c;
        }
    }
 
    result = '' + numberSystem + result;
    return '' + result + (0, _UPC.checksum)(result);
}
 
exports.default = UPCE;