jsbarcode
Version:
JsBarcode is a customizable barcode generator with support for multiple barcode formats.
110 lines (84 loc) • 3.87 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 _constants = require('./constants');
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:
// https://en.wikipedia.org/wiki/Code_93#Detailed_outline
var CODE93 = function (_Barcode) {
_inherits(CODE93, _Barcode);
function CODE93(data, options) {
_classCallCheck(this, CODE93);
return _possibleConstructorReturn(this, (CODE93.__proto__ || Object.getPrototypeOf(CODE93)).call(this, data, options));
}
_createClass(CODE93, [{
key: 'valid',
value: function valid() {
return (/^[0-9A-Z\-. $/+%]+$/.test(this.data)
);
}
}, {
key: 'encode',
value: function encode() {
var symbols = this.data.split('').flatMap(function (c) {
return _constants.MULTI_SYMBOLS[c] || c;
});
var encoded = symbols.map(function (s) {
return CODE93.getEncoding(s);
}).join('');
// Compute checksum symbols
var csumC = CODE93.checksum(symbols, 20);
var csumK = CODE93.checksum(symbols.concat(csumC), 15);
return {
text: this.text,
data:
// Add the start bits
CODE93.getEncoding('\xff') +
// Add the encoded bits
encoded +
// Add the checksum
CODE93.getEncoding(csumC) + CODE93.getEncoding(csumK) +
// Add the stop bits
CODE93.getEncoding('\xff') +
// Add the termination bit
'1'
};
}
// Get the binary encoding of a symbol
}], [{
key: 'getEncoding',
value: function getEncoding(symbol) {
return _constants.BINARIES[CODE93.symbolValue(symbol)];
}
// Get the symbol for a symbol value
}, {
key: 'getSymbol',
value: function getSymbol(symbolValue) {
return _constants.SYMBOLS[symbolValue];
}
// Get the symbol value of a symbol
}, {
key: 'symbolValue',
value: function symbolValue(symbol) {
return _constants.SYMBOLS.indexOf(symbol);
}
// Calculate a checksum symbol
}, {
key: 'checksum',
value: function checksum(symbols, maxWeight) {
var csum = symbols.slice().reverse().reduce(function (sum, symbol, idx) {
var weight = idx % maxWeight + 1;
return sum + CODE93.symbolValue(symbol) * weight;
}, 0);
return CODE93.getSymbol(csum % 47);
}
}]);
return CODE93;
}(_Barcode3.default);
exports.default = CODE93;