UPCE.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 _encoder2 from './encoder'
  19. import _Barcode3 from '../Barcode.js'
  20. import _UPC from './UPC.js'
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {
  23. default: obj
  24. };
  25. }
  26. function _classCallCheck(instance, Constructor) {
  27. if (!(instance instanceof Constructor)) {
  28. throw new TypeError("Cannot call a class as a function");
  29. }
  30. }
  31. function _possibleConstructorReturn(self, call) {
  32. if (!self) {
  33. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  34. }
  35. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  36. }
  37. function _inherits(subClass, superClass) {
  38. if (typeof superClass !== "function" && superClass !== null) {
  39. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  40. }
  41. subClass.prototype = Object.create(superClass && superClass.prototype, {
  42. constructor: {
  43. value: subClass,
  44. enumerable: false,
  45. writable: true,
  46. configurable: true
  47. }
  48. });
  49. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  50. superClass;
  51. } // Encoding documentation:
  52. // https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
  53. //
  54. // UPC-E documentation:
  55. // https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E
  56. var EXPANSIONS = ["XX00000XXX", "XX10000XXX", "XX20000XXX", "XXX00000XX", "XXXX00000X", "XXXXX00005", "XXXXX00006",
  57. "XXXXX00007", "XXXXX00008", "XXXXX00009"
  58. ];
  59. var PARITIES = [
  60. ["EEEOOO", "OOOEEE"],
  61. ["EEOEOO", "OOEOEE"],
  62. ["EEOOEO", "OOEEOE"],
  63. ["EEOOOE", "OOEEEO"],
  64. ["EOEEOO", "OEOOEE"],
  65. ["EOOEEO", "OEEOOE"],
  66. ["EOOOEE", "OEEEOO"],
  67. ["EOEOEO", "OEOEOE"],
  68. ["EOEOOE", "OEOEEO"],
  69. ["EOOEOE", "OEEOEO"]
  70. ];
  71. var UPCE = function(_Barcode) {
  72. _inherits(UPCE, _Barcode);
  73. function UPCE(data, options) {
  74. _classCallCheck(this, UPCE);
  75. var _this = _possibleConstructorReturn(this, (UPCE.__proto__ || Object.getPrototypeOf(UPCE)).call(this,
  76. data, options));
  77. // Code may be 6 or 8 digits;
  78. // A 7 digit code is ambiguous as to whether the extra digit
  79. // is a UPC-A check or number system digit.
  80. _this.isValid = false;
  81. if (data.search(/^[0-9]{6}$/) !== -1) {
  82. _this.middleDigits = data;
  83. _this.upcA = expandToUPCA(data, "0");
  84. _this.text = options.text || '' + _this.upcA[0] + data + _this.upcA[_this.upcA.length - 1];
  85. _this.isValid = true;
  86. } else if (data.search(/^[01][0-9]{7}$/) !== -1) {
  87. _this.middleDigits = data.substring(1, data.length - 1);
  88. _this.upcA = expandToUPCA(_this.middleDigits, data[0]);
  89. if (_this.upcA[_this.upcA.length - 1] === data[data.length - 1]) {
  90. _this.isValid = true;
  91. } else {
  92. // checksum mismatch
  93. return _possibleConstructorReturn(_this);
  94. }
  95. } else {
  96. return _possibleConstructorReturn(_this);
  97. }
  98. _this.displayValue = options.displayValue;
  99. // Make sure the font is not bigger than the space between the guard bars
  100. if (options.fontSize > options.width * 10) {
  101. _this.fontSize = options.width * 10;
  102. } else {
  103. _this.fontSize = options.fontSize;
  104. }
  105. // Make the guard bars go down half the way of the text
  106. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  107. return _this;
  108. }
  109. _createClass(UPCE, [{
  110. key: 'valid',
  111. value: function valid() {
  112. return this.isValid;
  113. }
  114. }, {
  115. key: 'encode',
  116. value: function encode() {
  117. if (this.options.flat) {
  118. return this.flatEncoding();
  119. } else {
  120. return this.guardedEncoding();
  121. }
  122. }
  123. }, {
  124. key: 'flatEncoding',
  125. value: function flatEncoding() {
  126. var result = "";
  127. result += "101";
  128. result += this.encodeMiddleDigits();
  129. result += "010101";
  130. return {
  131. data: result,
  132. text: this.text
  133. };
  134. }
  135. }, {
  136. key: 'guardedEncoding',
  137. value: function guardedEncoding() {
  138. var result = [];
  139. // Add the UPC-A number system digit beneath the quiet zone
  140. if (this.displayValue) {
  141. result.push({
  142. data: "00000000",
  143. text: this.text[0],
  144. options: {
  145. textAlign: "left",
  146. fontSize: this.fontSize
  147. }
  148. });
  149. }
  150. // Add the guard bars
  151. result.push({
  152. data: "101",
  153. options: {
  154. height: this.guardHeight
  155. }
  156. });
  157. // Add the 6 UPC-E digits
  158. result.push({
  159. data: this.encodeMiddleDigits(),
  160. text: this.text.substring(1, 7),
  161. options: {
  162. fontSize: this.fontSize
  163. }
  164. });
  165. // Add the end bits
  166. result.push({
  167. data: "010101",
  168. options: {
  169. height: this.guardHeight
  170. }
  171. });
  172. // Add the UPC-A check digit beneath the quiet zone
  173. if (this.displayValue) {
  174. result.push({
  175. data: "00000000",
  176. text: this.text[7],
  177. options: {
  178. textAlign: "right",
  179. fontSize: this.fontSize
  180. }
  181. });
  182. }
  183. return result;
  184. }
  185. }, {
  186. key: 'encodeMiddleDigits',
  187. value: function encodeMiddleDigits() {
  188. var numberSystem = this.upcA[0];
  189. var checkDigit = this.upcA[this.upcA.length - 1];
  190. var parity = PARITIES[parseInt(checkDigit)][parseInt(numberSystem)];
  191. return (0, _encoder2)(this.middleDigits, parity);
  192. }
  193. }]);
  194. return UPCE;
  195. }(_Barcode3);
  196. function expandToUPCA(middleDigits, numberSystem) {
  197. var lastUpcE = parseInt(middleDigits[middleDigits.length - 1]);
  198. var expansion = EXPANSIONS[lastUpcE];
  199. var result = "";
  200. var digitIndex = 0;
  201. for (var i = 0; i < expansion.length; i++) {
  202. var c = expansion[i];
  203. if (c === 'X') {
  204. result += middleDigits[digitIndex++];
  205. } else {
  206. result += c;
  207. }
  208. }
  209. result = '' + numberSystem + result;
  210. return '' + result + (0, _UPC.checksum)(result);
  211. }
  212. export default UPCE;