Skip to content

Commit d735420

Browse files
author
Johan Lindell
committed
📦 Release v3.9.0
1 parent d15efd0 commit d735420

26 files changed

+767
-795
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ Download or get the CDN link to the script:
134134

135135
| Name | Supported barcodes | Size (gzip) | CDN / Download |
136136
|------|--------------------|:-----------:|---------------:|
137-
| *All* | *All the barcodes!* | *9.6 kB* | *[JsBarcode.all.min.js][1]* |
138-
| CODE128 | CODE128 (auto and force mode) | 6 kB | [JsBarcode.code128.min.js][2] |
137+
| *All* | *All the barcodes!* | *10 kB* | *[JsBarcode.all.min.js][1]* |
138+
| CODE128 | CODE128 (auto and force mode) | 6.1 kB | [JsBarcode.code128.min.js][2] |
139139
| CODE39 | CODE39 | 5 kB | [JsBarcode.code39.min.js][3] |
140-
| EAN / UPC | EAN-13, EAN-8, EAN-5, EAN-2, UPC (A) | 6.2 kB | [JsBarcode.ean-upc.min.js][4] |
140+
| EAN / UPC | EAN-13, EAN-8, EAN-5, EAN-2, UPC (A) | 6.5 kB | [JsBarcode.ean-upc.min.js][4] |
141141
| ITF-14 | ITF-14 | 4.8 kB | [JsBarcode.itf-14.min.js][5] |
142142
| ITF | ITF | 4.7 kB | [JsBarcode.itf.min.js][6] |
143143
| MSI | MSI, MSI10, MSI11, MSI1010, MSI1110 | 4.9 kB | [JsBarcode.msi.min.js][7] |

bin/barcodes/CODE128/CODE128.js

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ var CODE128 = function (_Barcode) {
6060
throw new RangeError('The encoding does not start with a start character.');
6161
}
6262

63+
if (this.shouldEncodeAsEan128() === true) {
64+
bytes.unshift(_constants.FNC1);
65+
}
66+
6367
// Start encode with the right type
6468
var encodingResult = CODE128.next(bytes, 1, startSet);
6569

@@ -77,6 +81,18 @@ var CODE128 = function (_Barcode) {
7781
};
7882
}
7983

84+
// GS1-128/EAN-128
85+
86+
}, {
87+
key: 'shouldEncodeAsEan128',
88+
value: function shouldEncodeAsEan128() {
89+
var isEAN128 = this.options.ean128 || false;
90+
if (typeof isEAN128 === 'string') {
91+
isEAN128 = isEAN128.toLowerCase() === 'true';
92+
}
93+
return isEAN128;
94+
}
95+
8096
// Get a bar symbol by index
8197

8298
}], [{

bin/barcodes/CODE128/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var START_B = exports.START_B = 104;
2020
var START_C = exports.START_C = 105;
2121
var MODULO = exports.MODULO = 103;
2222
var STOP = exports.STOP = 106;
23+
var FNC1 = exports.FNC1 = 207;
2324

2425
// Get set by start code
2526
var SET_BY_CODE = exports.SET_BY_CODE = (_SET_BY_CODE = {}, _defineProperty(_SET_BY_CODE, START_A, SET_A), _defineProperty(_SET_BY_CODE, START_B, SET_B), _defineProperty(_SET_BY_CODE, START_C, SET_C), _SET_BY_CODE);

bin/barcodes/EAN_UPC/EAN.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; }; }();
8+
9+
var _constants = require('./constants');
10+
11+
var _encoder = require('./encoder');
12+
13+
var _encoder2 = _interopRequireDefault(_encoder);
14+
15+
var _Barcode2 = require('../Barcode');
16+
17+
var _Barcode3 = _interopRequireDefault(_Barcode2);
18+
19+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20+
21+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22+
23+
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; }
24+
25+
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; }
26+
27+
// Base class for EAN8 & EAN13
28+
var EAN = function (_Barcode) {
29+
_inherits(EAN, _Barcode);
30+
31+
function EAN(data, options) {
32+
_classCallCheck(this, EAN);
33+
34+
// Make sure the font is not bigger than the space between the guard bars
35+
var _this = _possibleConstructorReturn(this, (EAN.__proto__ || Object.getPrototypeOf(EAN)).call(this, data, options));
36+
37+
_this.fontSize = !options.flat && options.fontSize > options.width * 10 ? options.width * 10 : options.fontSize;
38+
39+
// Make the guard bars go down half the way of the text
40+
_this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
41+
return _this;
42+
}
43+
44+
_createClass(EAN, [{
45+
key: 'encode',
46+
value: function encode() {
47+
return this.options.flat ? this.encodeFlat() : this.encodeGuarded();
48+
}
49+
}, {
50+
key: 'leftText',
51+
value: function leftText(from, to) {
52+
return this.text.substr(from, to);
53+
}
54+
}, {
55+
key: 'leftEncode',
56+
value: function leftEncode(data, structure) {
57+
return (0, _encoder2.default)(data, structure);
58+
}
59+
}, {
60+
key: 'rightText',
61+
value: function rightText(from, to) {
62+
return this.text.substr(from, to);
63+
}
64+
}, {
65+
key: 'rightEncode',
66+
value: function rightEncode(data, structure) {
67+
return (0, _encoder2.default)(data, structure);
68+
}
69+
}, {
70+
key: 'encodeGuarded',
71+
value: function encodeGuarded() {
72+
var textOptions = { fontSize: this.fontSize };
73+
var guardOptions = { height: this.guardHeight };
74+
75+
return [{ data: _constants.SIDE_BIN, options: guardOptions }, { data: this.leftEncode(), text: this.leftText(), options: textOptions }, { data: _constants.MIDDLE_BIN, options: guardOptions }, { data: this.rightEncode(), text: this.rightText(), options: textOptions }, { data: _constants.SIDE_BIN, options: guardOptions }];
76+
}
77+
}, {
78+
key: 'encodeFlat',
79+
value: function encodeFlat() {
80+
var data = [_constants.SIDE_BIN, this.leftEncode(), _constants.MIDDLE_BIN, this.rightEncode(), _constants.SIDE_BIN];
81+
82+
return {
83+
data: data.join(''),
84+
text: this.text
85+
};
86+
}
87+
}]);
88+
89+
return EAN;
90+
}(_Barcode3.default);
91+
92+
exports.default = EAN;

bin/barcodes/EAN_UPC/EAN13.js

+61-129
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
"use strict";
1+
'use strict';
22

33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66

77
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; }; }();
88

9-
var _ean_encoder = require("./ean_encoder.js");
9+
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
1010

11-
var _ean_encoder2 = _interopRequireDefault(_ean_encoder);
11+
var _constants = require('./constants');
1212

13-
var _Barcode2 = require("../Barcode.js");
13+
var _EAN2 = require('./EAN');
1414

15-
var _Barcode3 = _interopRequireDefault(_Barcode2);
15+
var _EAN3 = _interopRequireDefault(_EAN2);
1616

1717
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1818

@@ -23,8 +23,20 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2323
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:
2424
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
2525

26-
var EAN13 = function (_Barcode) {
27-
_inherits(EAN13, _Barcode);
26+
// Calculate the checksum digit
27+
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
28+
var checksum = function checksum(number) {
29+
var res = number.substr(0, 12).split('').map(function (n) {
30+
return +n;
31+
}).reduce(function (sum, a, idx) {
32+
return idx % 2 ? sum + a * 3 : sum + a;
33+
}, 0);
34+
35+
return (10 - res % 10) % 10;
36+
};
37+
38+
var EAN13 = function (_EAN) {
39+
_inherits(EAN13, _EAN);
2840

2941
function EAN13(data, options) {
3042
_classCallCheck(this, EAN13);
@@ -34,154 +46,74 @@ var EAN13 = function (_Barcode) {
3446
data += checksum(data);
3547
}
3648

37-
// Make sure the font is not bigger than the space between the guard bars
49+
// Adds a last character to the end of the barcode
3850
var _this = _possibleConstructorReturn(this, (EAN13.__proto__ || Object.getPrototypeOf(EAN13)).call(this, data, options));
3951

40-
if (!options.flat && options.fontSize > options.width * 10) {
41-
_this.fontSize = options.width * 10;
42-
} else {
43-
_this.fontSize = options.fontSize;
44-
}
45-
46-
// Make the guard bars go down half the way of the text
47-
_this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
48-
49-
// Adds a last character to the end of the barcode
5052
_this.lastChar = options.lastChar;
5153
return _this;
5254
}
5355

5456
_createClass(EAN13, [{
55-
key: "valid",
57+
key: 'valid',
5658
value: function valid() {
57-
return this.data.search(/^[0-9]{13}$/) !== -1 && this.data[12] == checksum(this.data);
59+
return this.data.search(/^[0-9]{13}$/) !== -1 && +this.data[12] === checksum(this.data);
5860
}
5961
}, {
60-
key: "encode",
61-
value: function encode() {
62-
if (this.options.flat) {
63-
return this.flatEncoding();
64-
} else {
65-
return this.guardedEncoding();
66-
}
62+
key: 'leftText',
63+
value: function leftText() {
64+
return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'leftText', this).call(this, 1, 6);
65+
}
66+
}, {
67+
key: 'leftEncode',
68+
value: function leftEncode() {
69+
var data = this.data.substr(1, 6);
70+
var structure = _constants.EAN13_STRUCTURE[this.data[0]];
71+
return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'leftEncode', this).call(this, data, structure);
72+
}
73+
}, {
74+
key: 'rightText',
75+
value: function rightText() {
76+
return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'rightText', this).call(this, 7, 6);
6777
}
68-
69-
// Define the EAN-13 structure
70-
7178
}, {
72-
key: "getStructure",
73-
value: function getStructure() {
74-
return ["LLLLLL", "LLGLGG", "LLGGLG", "LLGGGL", "LGLLGG", "LGGLLG", "LGGGLL", "LGLGLG", "LGLGGL", "LGGLGL"];
79+
key: 'rightEncode',
80+
value: function rightEncode() {
81+
var data = this.data.substr(7, 6);
82+
return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'rightEncode', this).call(this, data, 'RRRRRR');
7583
}
7684

7785
// The "standard" way of printing EAN13 barcodes with guard bars
7886

7987
}, {
80-
key: "guardedEncoding",
81-
value: function guardedEncoding() {
82-
var encoder = new _ean_encoder2.default();
83-
var result = [];
84-
85-
var structure = this.getStructure()[this.data[0]];
88+
key: 'encodeGuarded',
89+
value: function encodeGuarded() {
90+
var data = _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'encodeGuarded', this).call(this);
8691

87-
// Get the string to be encoded on the left side of the EAN code
88-
var leftSide = this.data.substr(1, 6);
89-
90-
// Get the string to be encoded on the right side of the EAN code
91-
var rightSide = this.data.substr(7, 6);
92-
93-
// Add the first digigt
92+
// Extend data with left digit & last character
9493
if (this.options.displayValue) {
95-
result.push({
96-
data: "000000000000",
94+
data.unshift({
95+
data: '000000000000',
9796
text: this.text.substr(0, 1),
98-
options: { textAlign: "left", fontSize: this.fontSize }
97+
options: { textAlign: 'left', fontSize: this.fontSize }
9998
});
100-
}
10199

102-
// Add the guard bars
103-
result.push({
104-
data: "101",
105-
options: { height: this.guardHeight }
106-
});
107-
108-
// Add the left side
109-
result.push({
110-
data: encoder.encode(leftSide, structure),
111-
text: this.text.substr(1, 6),
112-
options: { fontSize: this.fontSize }
113-
});
114-
115-
// Add the middle bits
116-
result.push({
117-
data: "01010",
118-
options: { height: this.guardHeight }
119-
});
120-
121-
// Add the right side
122-
result.push({
123-
data: encoder.encode(rightSide, "RRRRRR"),
124-
text: this.text.substr(7, 6),
125-
options: { fontSize: this.fontSize }
126-
});
127-
128-
// Add the end bits
129-
result.push({
130-
data: "101",
131-
options: { height: this.guardHeight }
132-
});
133-
134-
if (this.options.lastChar && this.options.displayValue) {
135-
result.push({ data: "00" });
136-
137-
result.push({
138-
data: "00000",
139-
text: this.options.lastChar,
140-
options: { fontSize: this.fontSize }
141-
});
100+
if (this.options.lastChar) {
101+
data.push({
102+
data: '00'
103+
});
104+
data.push({
105+
data: '00000',
106+
text: this.options.lastChar,
107+
options: { fontSize: this.fontSize }
108+
});
109+
}
142110
}
143-
return result;
144-
}
145-
}, {
146-
key: "flatEncoding",
147-
value: function flatEncoding() {
148-
var encoder = new _ean_encoder2.default();
149-
var result = "";
150-
151-
var structure = this.getStructure()[this.data[0]];
152-
153-
result += "101";
154-
result += encoder.encode(this.data.substr(1, 6), structure);
155-
result += "01010";
156-
result += encoder.encode(this.data.substr(7, 6), "RRRRRR");
157-
result += "101";
158-
159-
return {
160-
data: result,
161-
text: this.text
162-
};
111+
112+
return data;
163113
}
164114
}]);
165115

166116
return EAN13;
167-
}(_Barcode3.default);
168-
169-
// Calulate the checksum digit
170-
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
171-
172-
173-
function checksum(number) {
174-
var result = 0;
175-
176-
var i;
177-
for (i = 0; i < 12; i += 2) {
178-
result += parseInt(number[i]);
179-
}
180-
for (i = 1; i < 12; i += 2) {
181-
result += parseInt(number[i]) * 3;
182-
}
183-
184-
return (10 - result % 10) % 10;
185-
}
117+
}(_EAN3.default);
186118

187119
exports.default = EAN13;

0 commit comments

Comments
 (0)