MSI.js 909 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Encoding documentation
  2. // https://en.wikipedia.org/wiki/MSI_Barcode#Character_set_and_binary_lookup
  3. import Barcode from "../Barcode.js";
  4. class MSI extends Barcode{
  5. constructor(data, options){
  6. super(data, options);
  7. }
  8. encode(){
  9. // Start bits
  10. var ret = "110";
  11. for(var i = 0; i < this.data.length; i++){
  12. // Convert the character to binary (always 4 binary digits)
  13. var digit = parseInt(this.data[i]);
  14. var bin = digit.toString(2);
  15. bin = addZeroes(bin, 4 - bin.length);
  16. // Add 100 for every zero and 110 for every 1
  17. for(var b = 0; b < bin.length; b++){
  18. ret += bin[b] == "0" ? "100" : "110";
  19. }
  20. }
  21. // End bits
  22. ret += "1001";
  23. return {
  24. data: ret,
  25. text: this.text
  26. };
  27. }
  28. valid(){
  29. return this.data.search(/^[0-9]+$/) !== -1;
  30. }
  31. }
  32. function addZeroes(number, n){
  33. for(var i = 0; i < n; i++){
  34. number = "0" + number;
  35. }
  36. return number;
  37. }
  38. export default MSI;