index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _Barcode3 from '../Barcode.js'
  19. function _classCallCheck(instance, Constructor) {
  20. if (!(instance instanceof Constructor)) {
  21. throw new TypeError("Cannot call a class as a function");
  22. }
  23. }
  24. function _possibleConstructorReturn(self, call) {
  25. if (!self) {
  26. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  27. }
  28. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  29. }
  30. function _inherits(subClass, superClass) {
  31. if (typeof superClass !== "function" && superClass !== null) {
  32. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  33. }
  34. subClass.prototype = Object.create(superClass && superClass.prototype, {
  35. constructor: {
  36. value: subClass,
  37. enumerable: false,
  38. writable: true,
  39. configurable: true
  40. }
  41. });
  42. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  43. superClass;
  44. } // Encoding documentation:
  45. // https://en.wikipedia.org/wiki/Code_39#Encoding
  46. var CODE39 = function(_Barcode) {
  47. _inherits(CODE39, _Barcode);
  48. function CODE39(data, options) {
  49. _classCallCheck(this, CODE39);
  50. data = data.toUpperCase();
  51. // Calculate mod43 checksum if enabled
  52. if (options.mod43) {
  53. data += getCharacter(mod43checksum(data));
  54. }
  55. return _possibleConstructorReturn(this, (CODE39.__proto__ || Object.getPrototypeOf(CODE39)).call(this, data,
  56. options));
  57. }
  58. _createClass(CODE39, [{
  59. key: "encode",
  60. value: function encode() {
  61. // First character is always a *
  62. var result = getEncoding("*");
  63. // Take every character and add the binary representation to the result
  64. for (var i = 0; i < this.data.length; i++) {
  65. result += getEncoding(this.data[i]) + "0";
  66. }
  67. // Last character is always a *
  68. result += getEncoding("*");
  69. return {
  70. data: result,
  71. text: this.text
  72. };
  73. }
  74. }, {
  75. key: "valid",
  76. value: function valid() {
  77. return this.data.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1;
  78. }
  79. }]);
  80. return CODE39;
  81. }(_Barcode3);
  82. // All characters. The position in the array is the (checksum) value
  83. var characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
  84. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "-", ".", " ", "$", "/", "+",
  85. "%", "*"
  86. ];
  87. // The decimal representation of the characters, is converted to the
  88. // corresponding binary with the getEncoding function
  89. var encodings = [20957, 29783, 23639, 30485, 20951, 29813, 23669, 20855, 29789, 23645, 29975, 23831, 30533, 22295,
  90. 30149, 24005, 21623, 29981, 23837, 22301, 30023, 23879, 30545, 22343, 30161, 24017, 21959, 30065, 23921, 22385,
  91. 29015, 18263, 29141, 17879, 29045, 18293, 17783, 29021, 18269, 17477, 17489, 17681, 20753, 35770
  92. ];
  93. // Get the binary representation of a character by converting the encodings
  94. // from decimal to binary
  95. function getEncoding(character) {
  96. return getBinary(characterValue(character));
  97. }
  98. function getBinary(characterValue) {
  99. return encodings[characterValue].toString(2);
  100. }
  101. function getCharacter(characterValue) {
  102. return characters[characterValue];
  103. }
  104. function characterValue(character) {
  105. return characters.indexOf(character);
  106. }
  107. function mod43checksum(data) {
  108. var checksum = 0;
  109. for (var i = 0; i < data.length; i++) {
  110. checksum += characterValue(data[i]);
  111. }
  112. checksum = checksum % 43;
  113. return checksum;
  114. }
  115. export default CODE39;