MSI.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {
  21. default: obj
  22. };
  23. }
  24. function _classCallCheck(instance, Constructor) {
  25. if (!(instance instanceof Constructor)) {
  26. throw new TypeError("Cannot call a class as a function");
  27. }
  28. }
  29. function _possibleConstructorReturn(self, call) {
  30. if (!self) {
  31. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  32. }
  33. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  34. }
  35. function _inherits(subClass, superClass) {
  36. if (typeof superClass !== "function" && superClass !== null) {
  37. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  38. }
  39. subClass.prototype = Object.create(superClass && superClass.prototype, {
  40. constructor: {
  41. value: subClass,
  42. enumerable: false,
  43. writable: true,
  44. configurable: true
  45. }
  46. });
  47. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  48. superClass;
  49. } // Encoding documentation
  50. // https://en.wikipedia.org/wiki/MSI_Barcode#Character_set_and_binary_lookup
  51. var MSI = function(_Barcode) {
  52. _inherits(MSI, _Barcode);
  53. function MSI(data, options) {
  54. _classCallCheck(this, MSI);
  55. return _possibleConstructorReturn(this, (MSI.__proto__ || Object.getPrototypeOf(MSI)).call(this, data,
  56. options));
  57. }
  58. _createClass(MSI, [{
  59. key: "encode",
  60. value: function encode() {
  61. // Start bits
  62. var ret = "110";
  63. for (var i = 0; i < this.data.length; i++) {
  64. // Convert the character to binary (always 4 binary digits)
  65. var digit = parseInt(this.data[i]);
  66. var bin = digit.toString(2);
  67. bin = addZeroes(bin, 4 - bin.length);
  68. // Add 100 for every zero and 110 for every 1
  69. for (var b = 0; b < bin.length; b++) {
  70. ret += bin[b] == "0" ? "100" : "110";
  71. }
  72. }
  73. // End bits
  74. ret += "1001";
  75. return {
  76. data: ret,
  77. text: this.text
  78. };
  79. }
  80. }, {
  81. key: "valid",
  82. value: function valid() {
  83. return this.data.search(/^[0-9]+$/) !== -1;
  84. }
  85. }]);
  86. return MSI;
  87. }(_Barcode3);
  88. function addZeroes(number, n) {
  89. for (var i = 0; i < n; i++) {
  90. number = "0" + number;
  91. }
  92. return number;
  93. }
  94. export default MSI;