EAN2.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import _constants from './constants'
  19. import _encoder2 from './encoder'
  20. import _Barcode3 from '../Barcode.js'
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {
  23. default: obj
  24. };
  25. }
  26. function _classCallCheck(instance, Constructor) {
  27. if (!(instance instanceof Constructor)) {
  28. throw new TypeError("Cannot call a class as a function");
  29. }
  30. }
  31. function _possibleConstructorReturn(self, call) {
  32. if (!self) {
  33. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  34. }
  35. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  36. }
  37. function _inherits(subClass, superClass) {
  38. if (typeof superClass !== "function" && superClass !== null) {
  39. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  40. }
  41. subClass.prototype = Object.create(superClass && superClass.prototype, {
  42. constructor: {
  43. value: subClass,
  44. enumerable: false,
  45. writable: true,
  46. configurable: true
  47. }
  48. });
  49. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  50. superClass;
  51. } // Encoding documentation:
  52. // https://en.wikipedia.org/wiki/EAN_2#Encoding
  53. var EAN2 = function(_Barcode) {
  54. _inherits(EAN2, _Barcode);
  55. function EAN2(data, options) {
  56. _classCallCheck(this, EAN2);
  57. return _possibleConstructorReturn(this, (EAN2.__proto__ || Object.getPrototypeOf(EAN2)).call(this, data,
  58. options));
  59. }
  60. _createClass(EAN2, [{
  61. key: 'valid',
  62. value: function valid() {
  63. return this.data.search(/^[0-9]{2}$/) !== -1;
  64. }
  65. }, {
  66. key: 'encode',
  67. value: function encode() {
  68. // Choose the structure based on the number mod 4
  69. var structure = _constants.EAN2_STRUCTURE[parseInt(this.data) % 4];
  70. return {
  71. // Start bits + Encode the two digits with 01 in between
  72. data: '1011' + (0, _encoder2)(this.data, structure, '01'),
  73. text: this.text
  74. };
  75. }
  76. }]);
  77. return EAN2;
  78. }(_Barcode3);
  79. export default EAN2;