UPC.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. function _classCallCheck(instance, Constructor) {
  21. if (!(instance instanceof Constructor)) {
  22. throw new TypeError("Cannot call a class as a function");
  23. }
  24. }
  25. function _possibleConstructorReturn(self, call) {
  26. if (!self) {
  27. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  28. }
  29. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  30. }
  31. function _inherits(subClass, superClass) {
  32. if (typeof superClass !== "function" && superClass !== null) {
  33. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  34. }
  35. subClass.prototype = Object.create(superClass && superClass.prototype, {
  36. constructor: {
  37. value: subClass,
  38. enumerable: false,
  39. writable: true,
  40. configurable: true
  41. }
  42. });
  43. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  44. superClass;
  45. } // Encoding documentation:
  46. // https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
  47. var UPC = function(_Barcode) {
  48. function UPC(data, options) {
  49. _classCallCheck(this, UPC);
  50. // Add checksum if it does not exist
  51. if (data.search(/^[0-9]{11}$/) !== -1) {
  52. data += checksum(data);
  53. }
  54. var _this = _possibleConstructorReturn(this, (UPC.__proto__ || Object.getPrototypeOf(UPC)).call(this, data,
  55. options));
  56. _this.displayValue = options.displayValue;
  57. // Make sure the font is not bigger than the space between the guard bars
  58. if (options.fontSize > options.width * 10) {
  59. _this.fontSize = options.width * 10;
  60. } else {
  61. _this.fontSize = options.fontSize;
  62. }
  63. // Make the guard bars go down half the way of the text
  64. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  65. return _this;
  66. }
  67. _createClass(UPC, [{
  68. key: "valid",
  69. value: function valid() {
  70. return this.data.search(/^[0-9]{12}$/) !== -1 && this.data[11] == checksum(this.data);
  71. }
  72. }, {
  73. key: "encode",
  74. value: function encode() {
  75. if (this.options.flat) {
  76. return this.flatEncoding();
  77. } else {
  78. return this.guardedEncoding();
  79. }
  80. }
  81. }, {
  82. key: "flatEncoding",
  83. value: function flatEncoding() {
  84. var result = "";
  85. result += "101";
  86. result += (0, _encoder2)(this.data.substr(0, 6), "LLLLLL");
  87. result += "01010";
  88. result += (0, _encoder2)(this.data.substr(6, 6), "RRRRRR");
  89. result += "101";
  90. return {
  91. data: result,
  92. text: this.text
  93. };
  94. }
  95. }, {
  96. key: "guardedEncoding",
  97. value: function guardedEncoding() {
  98. var result = [];
  99. // Add the first digit
  100. if (this.displayValue) {
  101. result.push({
  102. data: "00000000",
  103. text: this.text.substr(0, 1),
  104. options: {
  105. textAlign: "left",
  106. fontSize: this.fontSize
  107. }
  108. });
  109. }
  110. // Add the guard bars
  111. result.push({
  112. data: "101" + (0, _encoder2)(this.data[0], "L"),
  113. options: {
  114. height: this.guardHeight
  115. }
  116. });
  117. // Add the left side
  118. result.push({
  119. data: (0, _encoder2)(this.data.substr(1, 5), "LLLLL"),
  120. text: this.text.substr(1, 5),
  121. options: {
  122. fontSize: this.fontSize
  123. }
  124. });
  125. // Add the middle bits
  126. result.push({
  127. data: "01010",
  128. options: {
  129. height: this.guardHeight
  130. }
  131. });
  132. // Add the right side
  133. result.push({
  134. data: (0, _encoder2)(this.data.substr(6, 5), "RRRRR"),
  135. text: this.text.substr(6, 5),
  136. options: {
  137. fontSize: this.fontSize
  138. }
  139. });
  140. // Add the end bits
  141. result.push({
  142. data: (0, _encoder2)(this.data[11], "R") + "101",
  143. options: {
  144. height: this.guardHeight
  145. }
  146. });
  147. // Add the last digit
  148. if (this.displayValue) {
  149. result.push({
  150. data: "00000000",
  151. text: this.text.substr(11, 1),
  152. options: {
  153. textAlign: "right",
  154. fontSize: this.fontSize
  155. }
  156. });
  157. }
  158. return result;
  159. }
  160. }]);
  161. return UPC;
  162. }(_Barcode3);
  163. // Calulate the checksum digit
  164. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
  165. function checksum(number) {
  166. var result = 0;
  167. var i;
  168. for (i = 1; i < 11; i += 2) {
  169. result += parseInt(number[i]);
  170. }
  171. for (i = 0; i < 11; i += 2) {
  172. result += parseInt(number[i]) * 3;
  173. }
  174. return (10 - result % 10) % 10;
  175. }
  176. export default {
  177. UPC,
  178. checksum
  179. };