UPCE.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 _encoder = require('./encoder');
  7. var _encoder2 = _interopRequireDefault(_encoder);
  8. var _Barcode2 = require('../Barcode.js');
  9. var _Barcode3 = _interopRequireDefault(_Barcode2);
  10. var _UPC = require('./UPC.js');
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. 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; }
  14. 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:
  15. // https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
  16. //
  17. // UPC-E documentation:
  18. // https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E
  19. var EXPANSIONS = ["XX00000XXX", "XX10000XXX", "XX20000XXX", "XXX00000XX", "XXXX00000X", "XXXXX00005", "XXXXX00006", "XXXXX00007", "XXXXX00008", "XXXXX00009"];
  20. var PARITIES = [["EEEOOO", "OOOEEE"], ["EEOEOO", "OOEOEE"], ["EEOOEO", "OOEEOE"], ["EEOOOE", "OOEEEO"], ["EOEEOO", "OEOOEE"], ["EOOEEO", "OEEOOE"], ["EOOOEE", "OEEEOO"], ["EOEOEO", "OEOEOE"], ["EOEOOE", "OEOEEO"], ["EOOEOE", "OEEOEO"]];
  21. var UPCE = function (_Barcode) {
  22. _inherits(UPCE, _Barcode);
  23. function UPCE(data, options) {
  24. _classCallCheck(this, UPCE);
  25. var _this = _possibleConstructorReturn(this, (UPCE.__proto__ || Object.getPrototypeOf(UPCE)).call(this, data, options));
  26. // Code may be 6 or 8 digits;
  27. // A 7 digit code is ambiguous as to whether the extra digit
  28. // is a UPC-A check or number system digit.
  29. _this.isValid = false;
  30. if (data.search(/^[0-9]{6}$/) !== -1) {
  31. _this.middleDigits = data;
  32. _this.upcA = expandToUPCA(data, "0");
  33. _this.text = options.text || '' + _this.upcA[0] + data + _this.upcA[_this.upcA.length - 1];
  34. _this.isValid = true;
  35. } else if (data.search(/^[01][0-9]{7}$/) !== -1) {
  36. _this.middleDigits = data.substring(1, data.length - 1);
  37. _this.upcA = expandToUPCA(_this.middleDigits, data[0]);
  38. if (_this.upcA[_this.upcA.length - 1] === data[data.length - 1]) {
  39. _this.isValid = true;
  40. } else {
  41. // checksum mismatch
  42. return _possibleConstructorReturn(_this);
  43. }
  44. } else {
  45. return _possibleConstructorReturn(_this);
  46. }
  47. _this.displayValue = options.displayValue;
  48. // Make sure the font is not bigger than the space between the guard bars
  49. if (options.fontSize > options.width * 10) {
  50. _this.fontSize = options.width * 10;
  51. } else {
  52. _this.fontSize = options.fontSize;
  53. }
  54. // Make the guard bars go down half the way of the text
  55. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  56. return _this;
  57. }
  58. _createClass(UPCE, [{
  59. key: 'valid',
  60. value: function valid() {
  61. return this.isValid;
  62. }
  63. }, {
  64. key: 'encode',
  65. value: function encode() {
  66. if (this.options.flat) {
  67. return this.flatEncoding();
  68. } else {
  69. return this.guardedEncoding();
  70. }
  71. }
  72. }, {
  73. key: 'flatEncoding',
  74. value: function flatEncoding() {
  75. var result = "";
  76. result += "101";
  77. result += this.encodeMiddleDigits();
  78. result += "010101";
  79. return {
  80. data: result,
  81. text: this.text
  82. };
  83. }
  84. }, {
  85. key: 'guardedEncoding',
  86. value: function guardedEncoding() {
  87. var result = [];
  88. // Add the UPC-A number system digit beneath the quiet zone
  89. if (this.displayValue) {
  90. result.push({
  91. data: "00000000",
  92. text: this.text[0],
  93. options: { textAlign: "left", fontSize: this.fontSize }
  94. });
  95. }
  96. // Add the guard bars
  97. result.push({
  98. data: "101",
  99. options: { height: this.guardHeight }
  100. });
  101. // Add the 6 UPC-E digits
  102. result.push({
  103. data: this.encodeMiddleDigits(),
  104. text: this.text.substring(1, 7),
  105. options: { fontSize: this.fontSize }
  106. });
  107. // Add the end bits
  108. result.push({
  109. data: "010101",
  110. options: { height: this.guardHeight }
  111. });
  112. // Add the UPC-A check digit beneath the quiet zone
  113. if (this.displayValue) {
  114. result.push({
  115. data: "00000000",
  116. text: this.text[7],
  117. options: { textAlign: "right", fontSize: this.fontSize }
  118. });
  119. }
  120. return result;
  121. }
  122. }, {
  123. key: 'encodeMiddleDigits',
  124. value: function encodeMiddleDigits() {
  125. var numberSystem = this.upcA[0];
  126. var checkDigit = this.upcA[this.upcA.length - 1];
  127. var parity = PARITIES[parseInt(checkDigit)][parseInt(numberSystem)];
  128. return (0, _encoder2.default)(this.middleDigits, parity);
  129. }
  130. }]);
  131. return UPCE;
  132. }(_Barcode3.default);
  133. function expandToUPCA(middleDigits, numberSystem) {
  134. var lastUpcE = parseInt(middleDigits[middleDigits.length - 1]);
  135. var expansion = EXPANSIONS[lastUpcE];
  136. var result = "";
  137. var digitIndex = 0;
  138. for (var i = 0; i < expansion.length; i++) {
  139. var c = expansion[i];
  140. if (c === 'X') {
  141. result += middleDigits[digitIndex++];
  142. } else {
  143. result += c;
  144. }
  145. }
  146. result = '' + numberSystem + result;
  147. return '' + result + (0, _UPC.checksum)(result);
  148. }
  149. exports.default = UPCE;