BangleApps/apps/cards/codabar.js

64 lines
1.3 KiB
JavaScript

// Encoding specification:
// http://www.barcodeisland.com/codabar.phtml
const Barcode = require("cards.Barcode.js");
class codabar extends Barcode{
constructor(data, options){
if (/^[0-9\-\$\:\.\+\/]+$/.test(data)) {
data = "A" + data + "A";
}
super(data.toUpperCase(), options);
this.text = this.options.text || this.text.replace(/[A-D]/g, '');
}
valid(){
return /^[A-D][0-9\-\$\:\.\+\/]+[A-D]$/.test(this.data)
}
encode(){
var result = [];
var encodings = this.getEncodings();
for(var i = 0; i < this.data.length; i++){
result.push(encodings[this.data.charAt(i)]);
// for all characters except the last, append a narrow-space ("0")
if (i !== this.data.length - 1) {
result.push("0");
}
}
return {
text: this.text,
data: result.join('')
};
}
getEncodings(){
return {
"0": "101010011",
"1": "101011001",
"2": "101001011",
"3": "110010101",
"4": "101101001",
"5": "110101001",
"6": "100101011",
"7": "100101101",
"8": "100110101",
"9": "110100101",
"-": "101001101",
"$": "101100101",
":": "1101011011",
"/": "1101101011",
".": "1101101101",
"+": "1011011011",
"A": "1011001001",
"B": "1001001011",
"C": "1010010011",
"D": "1010011001"
};
}
}
module.exports = codabar