UPC.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. exports.checksum = checksum;
  7. var _encoder = require("./encoder");
  8. var _encoder2 = _interopRequireDefault(_encoder);
  9. var _Barcode2 = require("../Barcode.js");
  10. var _Barcode3 = _interopRequireDefault(_Barcode2);
  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. var UPC = function (_Barcode) {
  17. _inherits(UPC, _Barcode);
  18. function UPC(data, options) {
  19. _classCallCheck(this, UPC);
  20. // Add checksum if it does not exist
  21. if (data.search(/^[0-9]{11}$/) !== -1) {
  22. data += checksum(data);
  23. }
  24. var _this = _possibleConstructorReturn(this, (UPC.__proto__ || Object.getPrototypeOf(UPC)).call(this, data, options));
  25. _this.displayValue = options.displayValue;
  26. // Make sure the font is not bigger than the space between the guard bars
  27. if (options.fontSize > options.width * 10) {
  28. _this.fontSize = options.width * 10;
  29. } else {
  30. _this.fontSize = options.fontSize;
  31. }
  32. // Make the guard bars go down half the way of the text
  33. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  34. return _this;
  35. }
  36. _createClass(UPC, [{
  37. key: "valid",
  38. value: function valid() {
  39. return this.data.search(/^[0-9]{12}$/) !== -1 && this.data[11] == checksum(this.data);
  40. }
  41. }, {
  42. key: "encode",
  43. value: function encode() {
  44. if (this.options.flat) {
  45. return this.flatEncoding();
  46. } else {
  47. return this.guardedEncoding();
  48. }
  49. }
  50. }, {
  51. key: "flatEncoding",
  52. value: function flatEncoding() {
  53. var result = "";
  54. result += "101";
  55. result += (0, _encoder2.default)(this.data.substr(0, 6), "LLLLLL");
  56. result += "01010";
  57. result += (0, _encoder2.default)(this.data.substr(6, 6), "RRRRRR");
  58. result += "101";
  59. return {
  60. data: result,
  61. text: this.text
  62. };
  63. }
  64. }, {
  65. key: "guardedEncoding",
  66. value: function guardedEncoding() {
  67. var result = [];
  68. // Add the first digit
  69. if (this.displayValue) {
  70. result.push({
  71. data: "00000000",
  72. text: this.text.substr(0, 1),
  73. options: { textAlign: "left", fontSize: this.fontSize }
  74. });
  75. }
  76. // Add the guard bars
  77. result.push({
  78. data: "101" + (0, _encoder2.default)(this.data[0], "L"),
  79. options: { height: this.guardHeight }
  80. });
  81. // Add the left side
  82. result.push({
  83. data: (0, _encoder2.default)(this.data.substr(1, 5), "LLLLL"),
  84. text: this.text.substr(1, 5),
  85. options: { fontSize: this.fontSize }
  86. });
  87. // Add the middle bits
  88. result.push({
  89. data: "01010",
  90. options: { height: this.guardHeight }
  91. });
  92. // Add the right side
  93. result.push({
  94. data: (0, _encoder2.default)(this.data.substr(6, 5), "RRRRR"),
  95. text: this.text.substr(6, 5),
  96. options: { fontSize: this.fontSize }
  97. });
  98. // Add the end bits
  99. result.push({
  100. data: (0, _encoder2.default)(this.data[11], "R") + "101",
  101. options: { height: this.guardHeight }
  102. });
  103. // Add the last digit
  104. if (this.displayValue) {
  105. result.push({
  106. data: "00000000",
  107. text: this.text.substr(11, 1),
  108. options: { textAlign: "right", fontSize: this.fontSize }
  109. });
  110. }
  111. return result;
  112. }
  113. }]);
  114. return UPC;
  115. }(_Barcode3.default);
  116. // Calulate the checksum digit
  117. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
  118. function checksum(number) {
  119. var result = 0;
  120. var i;
  121. for (i = 1; i < 11; i += 2) {
  122. result += parseInt(number[i]);
  123. }
  124. for (i = 0; i < 11; i += 2) {
  125. result += parseInt(number[i]) * 3;
  126. }
  127. return (10 - result % 10) % 10;
  128. }
  129. exports.default = UPC;