UPC.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Encoding documentation:
  2. // https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
  3. import encode from './encoder';
  4. import Barcode from "../Barcode.js";
  5. class UPC extends Barcode{
  6. constructor(data, options){
  7. // Add checksum if it does not exist
  8. if(data.search(/^[0-9]{11}$/) !== -1){
  9. data += checksum(data);
  10. }
  11. super(data, options);
  12. this.displayValue = options.displayValue;
  13. // Make sure the font is not bigger than the space between the guard bars
  14. if(options.fontSize > options.width * 10){
  15. this.fontSize = options.width * 10;
  16. }
  17. else{
  18. this.fontSize = options.fontSize;
  19. }
  20. // Make the guard bars go down half the way of the text
  21. this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
  22. }
  23. valid(){
  24. return this.data.search(/^[0-9]{12}$/) !== -1 &&
  25. this.data[11] == checksum(this.data);
  26. }
  27. encode(){
  28. if(this.options.flat){
  29. return this.flatEncoding();
  30. }
  31. else{
  32. return this.guardedEncoding();
  33. }
  34. }
  35. flatEncoding(){
  36. var result = "";
  37. result += "101";
  38. result += encode(this.data.substr(0, 6), "LLLLLL");
  39. result += "01010";
  40. result += encode(this.data.substr(6, 6), "RRRRRR");
  41. result += "101";
  42. return {
  43. data: result,
  44. text: this.text
  45. };
  46. }
  47. guardedEncoding(){
  48. var result = [];
  49. // Add the first digit
  50. if(this.displayValue){
  51. result.push({
  52. data: "00000000",
  53. text: this.text.substr(0, 1),
  54. options: {textAlign: "left", fontSize: this.fontSize}
  55. });
  56. }
  57. // Add the guard bars
  58. result.push({
  59. data: "101" + encode(this.data[0], "L"),
  60. options: {height: this.guardHeight}
  61. });
  62. // Add the left side
  63. result.push({
  64. data: encode(this.data.substr(1, 5), "LLLLL"),
  65. text: this.text.substr(1, 5),
  66. options: {fontSize: this.fontSize}
  67. });
  68. // Add the middle bits
  69. result.push({
  70. data: "01010",
  71. options: {height: this.guardHeight}
  72. });
  73. // Add the right side
  74. result.push({
  75. data: encode(this.data.substr(6, 5), "RRRRR"),
  76. text: this.text.substr(6, 5),
  77. options: {fontSize: this.fontSize}
  78. });
  79. // Add the end bits
  80. result.push({
  81. data: encode(this.data[11], "R") + "101",
  82. options: {height: this.guardHeight}
  83. });
  84. // Add the last digit
  85. if(this.displayValue){
  86. result.push({
  87. data: "00000000",
  88. text: this.text.substr(11, 1),
  89. options: {textAlign: "right", fontSize: this.fontSize}
  90. });
  91. }
  92. return result;
  93. }
  94. }
  95. // Calulate the checksum digit
  96. // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
  97. export function checksum(number){
  98. var result = 0;
  99. var i;
  100. for(i = 1; i < 11; i += 2){
  101. result += parseInt(number[i]);
  102. }
  103. for(i = 0; i < 11; i += 2){
  104. result += parseInt(number[i]) * 3;
  105. }
  106. return (10 - (result % 10)) % 10;
  107. }
  108. export default UPC;