CODE128.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. var _constants = require('./constants');
  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; }
  13. // This is the master class,
  14. // it does require the start code to be included in the string
  15. var CODE128 = function (_Barcode) {
  16. _inherits(CODE128, _Barcode);
  17. function CODE128(data, options) {
  18. _classCallCheck(this, CODE128);
  19. // Get array of ascii codes from data
  20. var _this = _possibleConstructorReturn(this, (CODE128.__proto__ || Object.getPrototypeOf(CODE128)).call(this, data.substring(1), options));
  21. _this.bytes = data.split('').map(function (char) {
  22. return char.charCodeAt(0);
  23. });
  24. return _this;
  25. }
  26. _createClass(CODE128, [{
  27. key: 'valid',
  28. value: function valid() {
  29. // ASCII value ranges 0-127, 200-211
  30. return (/^[\x00-\x7F\xC8-\xD3]+$/.test(this.data)
  31. );
  32. }
  33. // The public encoding function
  34. }, {
  35. key: 'encode',
  36. value: function encode() {
  37. var bytes = this.bytes;
  38. // Remove the start code from the bytes and set its index
  39. var startIndex = bytes.shift() - 105;
  40. // Get start set by index
  41. var startSet = _constants.SET_BY_CODE[startIndex];
  42. if (startSet === undefined) {
  43. throw new RangeError('The encoding does not start with a start character.');
  44. }
  45. if (this.shouldEncodeAsEan128() === true) {
  46. bytes.unshift(_constants.FNC1);
  47. }
  48. // Start encode with the right type
  49. var encodingResult = CODE128.next(bytes, 1, startSet);
  50. return {
  51. text: this.text === this.data ? this.text.replace(/[^\x20-\x7E]/g, '') : this.text,
  52. data:
  53. // Add the start bits
  54. CODE128.getBar(startIndex) +
  55. // Add the encoded bits
  56. encodingResult.result +
  57. // Add the checksum
  58. CODE128.getBar((encodingResult.checksum + startIndex) % _constants.MODULO) +
  59. // Add the end bits
  60. CODE128.getBar(_constants.STOP)
  61. };
  62. }
  63. // GS1-128/EAN-128
  64. }, {
  65. key: 'shouldEncodeAsEan128',
  66. value: function shouldEncodeAsEan128() {
  67. var isEAN128 = this.options.ean128 || false;
  68. if (typeof isEAN128 === 'string') {
  69. isEAN128 = isEAN128.toLowerCase() === 'true';
  70. }
  71. return isEAN128;
  72. }
  73. // Get a bar symbol by index
  74. }], [{
  75. key: 'getBar',
  76. value: function getBar(index) {
  77. return _constants.BARS[index] ? _constants.BARS[index].toString() : '';
  78. }
  79. // Correct an index by a set and shift it from the bytes array
  80. }, {
  81. key: 'correctIndex',
  82. value: function correctIndex(bytes, set) {
  83. if (set === _constants.SET_A) {
  84. var charCode = bytes.shift();
  85. return charCode < 32 ? charCode + 64 : charCode - 32;
  86. } else if (set === _constants.SET_B) {
  87. return bytes.shift() - 32;
  88. } else {
  89. return (bytes.shift() - 48) * 10 + bytes.shift() - 48;
  90. }
  91. }
  92. }, {
  93. key: 'next',
  94. value: function next(bytes, pos, set) {
  95. if (!bytes.length) {
  96. return { result: '', checksum: 0 };
  97. }
  98. var nextCode = void 0,
  99. index = void 0;
  100. // Special characters
  101. if (bytes[0] >= 200) {
  102. index = bytes.shift() - 105;
  103. var nextSet = _constants.SWAP[index];
  104. // Swap to other set
  105. if (nextSet !== undefined) {
  106. nextCode = CODE128.next(bytes, pos + 1, nextSet);
  107. }
  108. // Continue on current set but encode a special character
  109. else {
  110. // Shift
  111. if ((set === _constants.SET_A || set === _constants.SET_B) && index === _constants.SHIFT) {
  112. // Convert the next character so that is encoded correctly
  113. bytes[0] = set === _constants.SET_A ? bytes[0] > 95 ? bytes[0] - 96 : bytes[0] : bytes[0] < 32 ? bytes[0] + 96 : bytes[0];
  114. }
  115. nextCode = CODE128.next(bytes, pos + 1, set);
  116. }
  117. }
  118. // Continue encoding
  119. else {
  120. index = CODE128.correctIndex(bytes, set);
  121. nextCode = CODE128.next(bytes, pos + 1, set);
  122. }
  123. // Get the correct binary encoding and calculate the weight
  124. var enc = CODE128.getBar(index);
  125. var weight = index * pos;
  126. return {
  127. result: enc + nextCode.result,
  128. checksum: weight + nextCode.checksum
  129. };
  130. }
  131. }]);
  132. return CODE128;
  133. }(_Barcode3.default);
  134. exports.default = CODE128;