1
0
Fork 0
BangleApps/apps/cards/EAN2.js

31 lines
678 B
JavaScript
Raw Normal View History

2024-04-08 19:40:53 +00:00
// Encoding documentation:
// https://en.wikipedia.org/wiki/EAN_2#Encoding
const constants = require("cards.constants.js");
const encode = require("cards.encode.js");
const Barcode = require("cards.Barcode.js");
class EAN2 extends Barcode {
constructor(data, options) {
super(data, options);
}
valid() {
return this.data.search(/^[0-9]{2}$/) !== -1;
}
encode(){
// Choose the structure based on the number mod 4
const structure = constants.EAN2_STRUCTURE[parseInt(this.data) % 4];
return {
// Start bits + Encode the two digits with 01 in between
data: '1011' + encode(this.data, structure, '01'),
text: this.text
};
}
}
module.exports = EAN2;