EAN13.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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; }; }();
  6. 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); } };
  7. var _constants = require('./constants');
  8. var _EAN2 = require('./EAN');
  9. var _EAN3 = _interopRequireDefault(_EAN2);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. 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; }
  13. 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:
  14. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
  15. // Calculate the checksum digit
  16. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
  17. var checksum = function checksum(number) {
  18. var res = number.substr(0, 12).split('').map(function (n) {
  19. return +n;
  20. }).reduce(function (sum, a, idx) {
  21. return idx % 2 ? sum + a * 3 : sum + a;
  22. }, 0);
  23. return (10 - res % 10) % 10;
  24. };
  25. var EAN13 = function (_EAN) {
  26. _inherits(EAN13, _EAN);
  27. function EAN13(data, options) {
  28. _classCallCheck(this, EAN13);
  29. // Add checksum if it does not exist
  30. if (data.search(/^[0-9]{12}$/) !== -1) {
  31. data += checksum(data);
  32. }
  33. // Adds a last character to the end of the barcode
  34. var _this = _possibleConstructorReturn(this, (EAN13.__proto__ || Object.getPrototypeOf(EAN13)).call(this, data, options));
  35. _this.lastChar = options.lastChar;
  36. return _this;
  37. }
  38. _createClass(EAN13, [{
  39. key: 'valid',
  40. value: function valid() {
  41. return this.data.search(/^[0-9]{13}$/) !== -1 && +this.data[12] === checksum(this.data);
  42. }
  43. }, {
  44. key: 'leftText',
  45. value: function leftText() {
  46. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'leftText', this).call(this, 1, 6);
  47. }
  48. }, {
  49. key: 'leftEncode',
  50. value: function leftEncode() {
  51. var data = this.data.substr(1, 6);
  52. var structure = _constants.EAN13_STRUCTURE[this.data[0]];
  53. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'leftEncode', this).call(this, data, structure);
  54. }
  55. }, {
  56. key: 'rightText',
  57. value: function rightText() {
  58. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'rightText', this).call(this, 7, 6);
  59. }
  60. }, {
  61. key: 'rightEncode',
  62. value: function rightEncode() {
  63. var data = this.data.substr(7, 6);
  64. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'rightEncode', this).call(this, data, 'RRRRRR');
  65. }
  66. // The "standard" way of printing EAN13 barcodes with guard bars
  67. }, {
  68. key: 'encodeGuarded',
  69. value: function encodeGuarded() {
  70. var data = _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype), 'encodeGuarded', this).call(this);
  71. // Extend data with left digit & last character
  72. if (this.options.displayValue) {
  73. data.unshift({
  74. data: '000000000000',
  75. text: this.text.substr(0, 1),
  76. options: { textAlign: 'left', fontSize: this.fontSize }
  77. });
  78. if (this.options.lastChar) {
  79. data.push({
  80. data: '00'
  81. });
  82. data.push({
  83. data: '00000',
  84. text: this.options.lastChar,
  85. options: { fontSize: this.fontSize }
  86. });
  87. }
  88. }
  89. return data;
  90. }
  91. }]);
  92. return EAN13;
  93. }(_EAN3.default);
  94. exports.default = EAN13;