index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Encoding specification:
  2. // http://www.barcodeisland.com/codabar.phtml
  3. import Barcode from "../Barcode.js";
  4. class codabar extends Barcode{
  5. constructor(data, options){
  6. if (data.search(/^[0-9\-\$\:\.\+\/]+$/) === 0) {
  7. data = "A" + data + "A";
  8. }
  9. super(data.toUpperCase(), options);
  10. this.text = this.options.text || this.text.replace(/[A-D]/g, '');
  11. }
  12. valid(){
  13. return this.data.search(/^[A-D][0-9\-\$\:\.\+\/]+[A-D]$/) !== -1;
  14. }
  15. encode(){
  16. var result = [];
  17. var encodings = this.getEncodings();
  18. for(var i = 0; i < this.data.length; i++){
  19. result.push(encodings[this.data.charAt(i)]);
  20. // for all characters except the last, append a narrow-space ("0")
  21. if (i !== this.data.length - 1) {
  22. result.push("0");
  23. }
  24. }
  25. return {
  26. text: this.text,
  27. data: result.join('')
  28. };
  29. }
  30. getEncodings(){
  31. return {
  32. "0": "101010011",
  33. "1": "101011001",
  34. "2": "101001011",
  35. "3": "110010101",
  36. "4": "101101001",
  37. "5": "110101001",
  38. "6": "100101011",
  39. "7": "100101101",
  40. "8": "100110101",
  41. "9": "110100101",
  42. "-": "101001101",
  43. "$": "101100101",
  44. ":": "1101011011",
  45. "/": "1101101011",
  46. ".": "1101101101",
  47. "+": "1011011011",
  48. "A": "1011001001",
  49. "B": "1001001011",
  50. "C": "1010010011",
  51. "D": "1010011001"
  52. };
  53. }
  54. }
  55. export {codabar};