EAN13.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. var _createClass = function() {
  3. function defineProperties(target, props) {
  4. for (var i = 0; i < props.length; i++) {
  5. var descriptor = props[i];
  6. descriptor.enumerable = descriptor.enumerable || false;
  7. descriptor.configurable = true;
  8. if ("value" in descriptor) descriptor.writable = true;
  9. Object.defineProperty(target, descriptor.key, descriptor);
  10. }
  11. }
  12. return function(Constructor, protoProps, staticProps) {
  13. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  14. if (staticProps) defineProperties(Constructor, staticProps);
  15. return Constructor;
  16. };
  17. }();
  18. var _get = function get(object, property, receiver) {
  19. if (object === null) object = Function.prototype;
  20. var desc = Object.getOwnPropertyDescriptor(object, property);
  21. if (desc === undefined) {
  22. var parent = Object.getPrototypeOf(object);
  23. if (parent === null) {
  24. return undefined;
  25. } else {
  26. return get(parent, property, receiver);
  27. }
  28. } else if ("value" in desc) {
  29. return desc.value;
  30. } else {
  31. var getter = desc.get;
  32. if (getter === undefined) {
  33. return undefined;
  34. }
  35. return getter.call(receiver);
  36. }
  37. };
  38. import _constants from './constants'
  39. import _EAN3 from './EAN'
  40. function _interopRequireDefault(obj) {
  41. return obj && obj.__esModule ? obj : {
  42. default: obj
  43. };
  44. }
  45. function _classCallCheck(instance, Constructor) {
  46. if (!(instance instanceof Constructor)) {
  47. throw new TypeError("Cannot call a class as a function");
  48. }
  49. }
  50. function _possibleConstructorReturn(self, call) {
  51. if (!self) {
  52. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  53. }
  54. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  55. }
  56. function _inherits(subClass, superClass) {
  57. if (typeof superClass !== "function" && superClass !== null) {
  58. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  59. }
  60. subClass.prototype = Object.create(superClass && superClass.prototype, {
  61. constructor: {
  62. value: subClass,
  63. enumerable: false,
  64. writable: true,
  65. configurable: true
  66. }
  67. });
  68. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  69. superClass;
  70. } // Encoding documentation:
  71. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
  72. // Calculate the checksum digit
  73. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
  74. var checksum = function checksum(number) {
  75. var res = number.substr(0, 12).split('').map(function(n) {
  76. return +n;
  77. }).reduce(function(sum, a, idx) {
  78. return idx % 2 ? sum + a * 3 : sum + a;
  79. }, 0);
  80. return (10 - res % 10) % 10;
  81. };
  82. var EAN13 = function(_EAN) {
  83. _inherits(EAN13, _EAN);
  84. function EAN13(data, options) {
  85. _classCallCheck(this, EAN13);
  86. // Add checksum if it does not exist
  87. if (data.search(/^[0-9]{12}$/) !== -1) {
  88. data += checksum(data);
  89. }
  90. // Adds a last character to the end of the barcode
  91. var _this = _possibleConstructorReturn(this, (EAN13.__proto__ || Object.getPrototypeOf(EAN13)).call(this,
  92. data, options));
  93. _this.lastChar = options.lastChar;
  94. return _this;
  95. }
  96. _createClass(EAN13, [{
  97. key: 'valid',
  98. value: function valid() {
  99. return this.data.search(/^[0-9]{13}$/) !== -1 && +this.data[12] === checksum(this.data);
  100. }
  101. }, {
  102. key: 'leftText',
  103. value: function leftText() {
  104. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype),
  105. 'leftText', this).call(this, 1, 6);
  106. }
  107. }, {
  108. key: 'leftEncode',
  109. value: function leftEncode() {
  110. var data = this.data.substr(1, 6);
  111. var structure = _constants.EAN13_STRUCTURE[this.data[0]];
  112. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype),
  113. 'leftEncode', this).call(this, data, structure);
  114. }
  115. }, {
  116. key: 'rightText',
  117. value: function rightText() {
  118. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype),
  119. 'rightText', this).call(this, 7, 6);
  120. }
  121. }, {
  122. key: 'rightEncode',
  123. value: function rightEncode() {
  124. var data = this.data.substr(7, 6);
  125. return _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype),
  126. 'rightEncode', this).call(this, data, 'RRRRRR');
  127. }
  128. // The "standard" way of printing EAN13 barcodes with guard bars
  129. }, {
  130. key: 'encodeGuarded',
  131. value: function encodeGuarded() {
  132. var data = _get(EAN13.prototype.__proto__ || Object.getPrototypeOf(EAN13.prototype),
  133. 'encodeGuarded', this).call(this);
  134. // Extend data with left digit & last character
  135. if (this.options.displayValue) {
  136. data.unshift({
  137. data: '000000000000',
  138. text: this.text.substr(0, 1),
  139. options: {
  140. textAlign: 'left',
  141. fontSize: this.fontSize
  142. }
  143. });
  144. if (this.options.lastChar) {
  145. data.push({
  146. data: '00'
  147. });
  148. data.push({
  149. data: '00000',
  150. text: this.options.lastChar,
  151. options: {
  152. fontSize: this.fontSize
  153. }
  154. });
  155. }
  156. }
  157. return data;
  158. }
  159. }]);
  160. return EAN13;
  161. }(_EAN3);
  162. export default EAN13;