MSI.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 _Barcode2 = require("../Barcode.js");
  7. var _Barcode3 = _interopRequireDefault(_Barcode2);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. 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; }
  11. 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
  12. // https://en.wikipedia.org/wiki/MSI_Barcode#Character_set_and_binary_lookup
  13. var MSI = function (_Barcode) {
  14. _inherits(MSI, _Barcode);
  15. function MSI(data, options) {
  16. _classCallCheck(this, MSI);
  17. return _possibleConstructorReturn(this, (MSI.__proto__ || Object.getPrototypeOf(MSI)).call(this, data, options));
  18. }
  19. _createClass(MSI, [{
  20. key: "encode",
  21. value: function encode() {
  22. // Start bits
  23. var ret = "110";
  24. for (var i = 0; i < this.data.length; i++) {
  25. // Convert the character to binary (always 4 binary digits)
  26. var digit = parseInt(this.data[i]);
  27. var bin = digit.toString(2);
  28. bin = addZeroes(bin, 4 - bin.length);
  29. // Add 100 for every zero and 110 for every 1
  30. for (var b = 0; b < bin.length; b++) {
  31. ret += bin[b] == "0" ? "100" : "110";
  32. }
  33. }
  34. // End bits
  35. ret += "1001";
  36. return {
  37. data: ret,
  38. text: this.text
  39. };
  40. }
  41. }, {
  42. key: "valid",
  43. value: function valid() {
  44. return this.data.search(/^[0-9]+$/) !== -1;
  45. }
  46. }]);
  47. return MSI;
  48. }(_Barcode3.default);
  49. function addZeroes(number, n) {
  50. for (var i = 0; i < n; i++) {
  51. number = "0" + number;
  52. }
  53. return number;
  54. }
  55. exports.default = MSI;