EAN8.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 _EAN2 = require('./EAN');
  8. var _EAN3 = _interopRequireDefault(_EAN2);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. 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; }
  12. 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:
  13. // http://www.barcodeisland.com/ean8.phtml
  14. // Calculate the checksum digit
  15. var checksum = function checksum(number) {
  16. var res = number.substr(0, 7).split('').map(function (n) {
  17. return +n;
  18. }).reduce(function (sum, a, idx) {
  19. return idx % 2 ? sum + a : sum + a * 3;
  20. }, 0);
  21. return (10 - res % 10) % 10;
  22. };
  23. var EAN8 = function (_EAN) {
  24. _inherits(EAN8, _EAN);
  25. function EAN8(data, options) {
  26. _classCallCheck(this, EAN8);
  27. // Add checksum if it does not exist
  28. if (data.search(/^[0-9]{7}$/) !== -1) {
  29. data += checksum(data);
  30. }
  31. return _possibleConstructorReturn(this, (EAN8.__proto__ || Object.getPrototypeOf(EAN8)).call(this, data, options));
  32. }
  33. _createClass(EAN8, [{
  34. key: 'valid',
  35. value: function valid() {
  36. return this.data.search(/^[0-9]{8}$/) !== -1 && +this.data[7] === checksum(this.data);
  37. }
  38. }, {
  39. key: 'leftText',
  40. value: function leftText() {
  41. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype), 'leftText', this).call(this, 0, 4);
  42. }
  43. }, {
  44. key: 'leftEncode',
  45. value: function leftEncode() {
  46. var data = this.data.substr(0, 4);
  47. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype), 'leftEncode', this).call(this, data, 'LLLL');
  48. }
  49. }, {
  50. key: 'rightText',
  51. value: function rightText() {
  52. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype), 'rightText', this).call(this, 4, 4);
  53. }
  54. }, {
  55. key: 'rightEncode',
  56. value: function rightEncode() {
  57. var data = this.data.substr(4, 4);
  58. return _get(EAN8.prototype.__proto__ || Object.getPrototypeOf(EAN8.prototype), 'rightEncode', this).call(this, data, 'RRRR');
  59. }
  60. }]);
  61. return EAN8;
  62. }(_EAN3.default);
  63. exports.default = EAN8;