CODE128.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. import _constants from './constants'
  20. function _interopRequireDefault(obj) {
  21. return obj && obj.__esModule ? obj : {
  22. default: obj
  23. };
  24. }
  25. function _classCallCheck(instance, Constructor) {
  26. if (!(instance instanceof Constructor)) {
  27. throw new TypeError("Cannot call a class as a function");
  28. }
  29. }
  30. function _possibleConstructorReturn(self, call) {
  31. if (!self) {
  32. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  33. }
  34. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  35. }
  36. function _inherits(subClass, superClass) {
  37. if (typeof superClass !== "function" && superClass !== null) {
  38. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  39. }
  40. subClass.prototype = Object.create(superClass && superClass.prototype, {
  41. constructor: {
  42. value: subClass,
  43. enumerable: false,
  44. writable: true,
  45. configurable: true
  46. }
  47. });
  48. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  49. superClass;
  50. }
  51. // This is the master class,
  52. // it does require the start code to be included in the string
  53. var CODE128 = function(_Barcode) {
  54. _inherits(CODE128, _Barcode);
  55. function CODE128(data, options) {
  56. _classCallCheck(this, CODE128);
  57. // Get array of ascii codes from data
  58. var _this = _possibleConstructorReturn(this, (CODE128.__proto__ || Object.getPrototypeOf(CODE128)).call(
  59. this, data.substring(1), options));
  60. _this.bytes = data.split('').map(function(char) {
  61. return char.charCodeAt(0);
  62. });
  63. return _this;
  64. }
  65. _createClass(CODE128, [{
  66. key: 'valid',
  67. value: function valid() {
  68. // ASCII value ranges 0-127, 200-211
  69. return (/^[\x00-\x7F\xC8-\xD3]+$/.test(this.data));
  70. }
  71. // The public encoding function
  72. }, {
  73. key: 'encode',
  74. value: function encode() {
  75. var bytes = this.bytes;
  76. // Remove the start code from the bytes and set its index
  77. var startIndex = bytes.shift() - 105;
  78. // Get start set by index
  79. var startSet = _constants.SET_BY_CODE[startIndex];
  80. if (startSet === undefined) {
  81. throw new RangeError('The encoding does not start with a start character.');
  82. }
  83. if (this.shouldEncodeAsEan128() === true) {
  84. bytes.unshift(_constants.FNC1);
  85. }
  86. // Start encode with the right type
  87. var encodingResult = CODE128.next(bytes, 1, startSet);
  88. return {
  89. text: this.text === this.data ? this.text.replace(/[^\x20-\x7E]/g, '') : this.text,
  90. data:
  91. // Add the start bits
  92. CODE128.getBar(startIndex) +
  93. // Add the encoded bits
  94. encodingResult.result +
  95. // Add the checksum
  96. CODE128.getBar((encodingResult.checksum + startIndex) % _constants.MODULO) +
  97. // Add the end bits
  98. CODE128.getBar(_constants.STOP)
  99. };
  100. }
  101. // GS1-128/EAN-128
  102. }, {
  103. key: 'shouldEncodeAsEan128',
  104. value: function shouldEncodeAsEan128() {
  105. var isEAN128 = this.options.ean128 || false;
  106. if (typeof isEAN128 === 'string') {
  107. isEAN128 = isEAN128.toLowerCase() === 'true';
  108. }
  109. return isEAN128;
  110. }
  111. // Get a bar symbol by index
  112. }], [{
  113. key: 'getBar',
  114. value: function getBar(index) {
  115. return _constants.BARS[index] ? _constants.BARS[index].toString() : '';
  116. }
  117. // Correct an index by a set and shift it from the bytes array
  118. }, {
  119. key: 'correctIndex',
  120. value: function correctIndex(bytes, set) {
  121. if (set === _constants.SET_A) {
  122. var charCode = bytes.shift();
  123. return charCode < 32 ? charCode + 64 : charCode - 32;
  124. } else if (set === _constants.SET_B) {
  125. return bytes.shift() - 32;
  126. } else {
  127. return (bytes.shift() - 48) * 10 + bytes.shift() - 48;
  128. }
  129. }
  130. }, {
  131. key: 'next',
  132. value: function next(bytes, pos, set) {
  133. if (!bytes.length) {
  134. return {
  135. result: '',
  136. checksum: 0
  137. };
  138. }
  139. var nextCode = void 0,
  140. index = void 0;
  141. // Special characters
  142. if (bytes[0] >= 200) {
  143. index = bytes.shift() - 105;
  144. var nextSet = _constants.SWAP[index];
  145. // Swap to other set
  146. if (nextSet !== undefined) {
  147. nextCode = CODE128.next(bytes, pos + 1, nextSet);
  148. }
  149. // Continue on current set but encode a special character
  150. else {
  151. // Shift
  152. if ((set === _constants.SET_A || set === _constants.SET_B) && index ===
  153. _constants.SHIFT) {
  154. // Convert the next character so that is encoded correctly
  155. bytes[0] = set === _constants.SET_A ? bytes[0] > 95 ? bytes[0] - 96 : bytes[
  156. 0] : bytes[0] < 32 ? bytes[0] + 96 : bytes[0];
  157. }
  158. nextCode = CODE128.next(bytes, pos + 1, set);
  159. }
  160. }
  161. // Continue encoding
  162. else {
  163. index = CODE128.correctIndex(bytes, set);
  164. nextCode = CODE128.next(bytes, pos + 1, set);
  165. }
  166. // Get the correct binary encoding and calculate the weight
  167. var enc = CODE128.getBar(index);
  168. var weight = index * pos;
  169. return {
  170. result: enc + nextCode.result,
  171. checksum: weight + nextCode.checksum
  172. };
  173. }
  174. }]);
  175. return CODE128;
  176. }(_Barcode3);
  177. export default CODE128;