mirror of https://github.com/espruino/BangleApps
89 lines
2.5 KiB
JavaScript
89 lines
2.5 KiB
JavaScript
// Encoding specification:
|
|
// http://www.barcodeisland.com/codabar.phtml
|
|
/*
|
|
* JS source adapted from https://github.com/lindell/JsBarcode
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2016 Johan Lindell (johan@lindell.me)
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to
|
|
* deal in the Software without restriction, including without limitation the
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
* IN THE SOFTWARE.
|
|
*/
|
|
|
|
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
|