jsbarcode2
Version:
JsBarcode is a customizable barcode generator with support for multiple barcode formats.
102 lines (70 loc) • 3.51 kB
JavaScript
;
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 _ean_encoder = require("./ean_encoder.js");
var _ean_encoder2 = _interopRequireDefault(_ean_encoder);
var _Barcode2 = require("../Barcode.js");
var _Barcode3 = _interopRequireDefault(_Barcode2);
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:
// http://www.barcodeisland.com/ean8.phtml
var EAN8 = function (_Barcode) {
_inherits(EAN8, _Barcode);
function EAN8(data, options) {
_classCallCheck(this, EAN8);
// Add checksum if it does not exist
if (data.search(/^[0-9]{7}$/) !== -1) {
data += checksum(data);
}
return _possibleConstructorReturn(this, (EAN8.__proto__ || Object.getPrototypeOf(EAN8)).call(this, data, options));
}
_createClass(EAN8, [{
key: "valid",
value: function valid() {
return this.data.search(/^[0-9]{8}$/) !== -1 && this.data[7] == checksum(this.data);
}
}, {
key: "encode",
value: function encode() {
var encoder = new _ean_encoder2.default();
// Create the return variable
var result = "";
// Get the number to be encoded on the left side of the EAN code
var leftSide = this.data.substr(0, 4);
// Get the number to be encoded on the right side of the EAN code
var rightSide = this.data.substr(4, 4);
// Add the start bits
result += encoder.startBin;
// Add the left side
result += encoder.encode(leftSide, "LLLL");
// Add the middle bits
result += encoder.middleBin;
// Add the right side
result += encoder.encode(rightSide, "RRRR");
// Add the end bits
result += encoder.endBin;
return {
data: result,
text: this.text
};
}
}]);
return EAN8;
}(_Barcode3.default);
// Calulate the checksum digit
function checksum(number) {
var result = 0;
var i;
for (i = 0; i < 7; i += 2) {
result += parseInt(number[i]) * 3;
}
for (i = 1; i < 7; i += 2) {
result += parseInt(number[i]);
}
return (10 - result % 10) % 10;
}
exports.default = EAN8;