EAN8.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 _EAN3 from './EAN'
  39. function _interopRequireDefault(obj) {
  40. return obj && obj.__esModule ? obj : {
  41. default: obj
  42. };
  43. }
  44. function _classCallCheck(instance, Constructor) {
  45. if (!(instance instanceof Constructor)) {
  46. throw new TypeError("Cannot call a class as a function");
  47. }
  48. }
  49. function _possibleConstructorReturn(self, call) {
  50. if (!self) {
  51. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  52. }
  53. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  54. }
  55. function _inherits(subClass, superClass) {
  56. if (typeof superClass !== "function" && superClass !== null) {
  57. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  58. }
  59. subClass.prototype = Object.create(superClass && superClass.prototype, {
  60. constructor: {
  61. value: subClass,
  62. enumerable: false,
  63. writable: true,
  64. configurable: true
  65. }
  66. });
  67. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  68. superClass;
  69. } // Encoding documentation:
  70. // http://www.barcodeisland.com/ean8.phtml
  71. // Calculate the checksum digit
  72. var checksum = function checksum(number) {
  73. var res = number.substr(0, 7).split('').map(function(n) {
  74. return +n;
  75. }).reduce(function(sum, a, idx) {
  76. return idx % 2 ? sum + a : sum + a * 3;
  77. }, 0);
  78. return (10 - res % 10) % 10;
  79. };
  80. var EAN8 = function(_EAN) {
  81. _inherits(EAN8, _EAN);
  82. function EAN8(data, options) {
  83. _classCallCheck(this, EAN8);
  84. // Add checksum if it does not exist
  85. if (data.search(/^[0-9]{7}$/) !== -1) {
  86. data += checksum(data);
  87. }
  88. return _possibleConstructorReturn(this, (EAN8.__proto__ || Object.getPrototypeOf(EAN8)).call(this, data,
  89. options));
  90. }
  91. _createClass(EAN8, [{
  92. key: 'valid',
  93. value: function valid() {
  94. return this.data.search(/^[0-9]{8}$/) !== -1 && +this.data[7] === checksum(this.data);
  95. }
  96. }, {
  97. key: 'leftText',
  98. value: function leftText() {
  99. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype),
  100. 'leftText', this).call(this, 0, 4);
  101. }
  102. }, {
  103. key: 'leftEncode',
  104. value: function leftEncode() {
  105. var data = this.data.substr(0, 4);
  106. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype),
  107. 'leftEncode', this).call(this, data, 'LLLL');
  108. }
  109. }, {
  110. key: 'rightText',
  111. value: function rightText() {
  112. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype),
  113. 'rightText', this).call(this, 4, 4);
  114. }
  115. }, {
  116. key: 'rightEncode',
  117. value: function rightEncode() {
  118. var data = this.data.substr(4, 4);
  119. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype),
  120. 'rightEncode', this).call(this, data, 'RRRR');
  121. }
  122. }]);
  123. return EAN8;
  124. }(_EAN3);
  125. export default EAN8;