index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CODE39 = undefined;
  6. 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; }; }();
  7. var _Barcode2 = require("../Barcode.js");
  8. var _Barcode3 = _interopRequireDefault(_Barcode2);
  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. // https://en.wikipedia.org/wiki/Code_39#Encoding
  14. var CODE39 = function (_Barcode) {
  15. _inherits(CODE39, _Barcode);
  16. function CODE39(data, options) {
  17. _classCallCheck(this, CODE39);
  18. data = data.toUpperCase();
  19. // Calculate mod43 checksum if enabled
  20. if (options.mod43) {
  21. data += getCharacter(mod43checksum(data));
  22. }
  23. return _possibleConstructorReturn(this, (CODE39.__proto__ || Object.getPrototypeOf(CODE39)).call(this, data, options));
  24. }
  25. _createClass(CODE39, [{
  26. key: "encode",
  27. value: function encode() {
  28. // First character is always a *
  29. var result = getEncoding("*");
  30. // Take every character and add the binary representation to the result
  31. for (var i = 0; i < this.data.length; i++) {
  32. result += getEncoding(this.data[i]) + "0";
  33. }
  34. // Last character is always a *
  35. result += getEncoding("*");
  36. return {
  37. data: result,
  38. text: this.text
  39. };
  40. }
  41. }, {
  42. key: "valid",
  43. value: function valid() {
  44. return this.data.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1;
  45. }
  46. }]);
  47. return CODE39;
  48. }(_Barcode3.default);
  49. // All characters. The position in the array is the (checksum) value
  50. var characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "-", ".", " ", "$", "/", "+", "%", "*"];
  51. // The decimal representation of the characters, is converted to the
  52. // corresponding binary with the getEncoding function
  53. var encodings = [20957, 29783, 23639, 30485, 20951, 29813, 23669, 20855, 29789, 23645, 29975, 23831, 30533, 22295, 30149, 24005, 21623, 29981, 23837, 22301, 30023, 23879, 30545, 22343, 30161, 24017, 21959, 30065, 23921, 22385, 29015, 18263, 29141, 17879, 29045, 18293, 17783, 29021, 18269, 17477, 17489, 17681, 20753, 35770];
  54. // Get the binary representation of a character by converting the encodings
  55. // from decimal to binary
  56. function getEncoding(character) {
  57. return getBinary(characterValue(character));
  58. }
  59. function getBinary(characterValue) {
  60. return encodings[characterValue].toString(2);
  61. }
  62. function getCharacter(characterValue) {
  63. return characters[characterValue];
  64. }
  65. function characterValue(character) {
  66. return characters.indexOf(character);
  67. }
  68. function mod43checksum(data) {
  69. var checksum = 0;
  70. for (var i = 0; i < data.length; i++) {
  71. checksum += characterValue(data[i]);
  72. }
  73. checksum = checksum % 43;
  74. return checksum;
  75. }
  76. exports.CODE39 = CODE39;