Skip to content

Commit

Permalink
Added support for EAN-5
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Mar 29, 2016
1 parent a7ddc13 commit 4f598d5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
78 changes: 67 additions & 11 deletions barcodes/EAN_UPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,14 @@ function EAN8(EAN8number){
}

this.valid = function(){
return valid(this.EAN8number);
return this.EAN8number.search(fullEanRegexp) !== -1
&& this.EAN8number[7] == checksum(this.EAN8number);
};

this.encoded = function (){
return createEAN8(this.EAN8number);
}

function valid(number){
if(number.search(fullEanRegexp)!=-1){
return number[7] == checksum(number);
}
else{
return false;
}
}

//Calulate the checksum digit
function checksum(number){
var result = 0;
Expand Down Expand Up @@ -163,6 +155,62 @@ function EAN8(EAN8number){
}
}

function EAN5(EAN5number){
this.EAN5number = EAN5number+"";

//Regexp to test if the EAN code is correct formated
var fullEanRegexp = /^[0-9]{5}$/;

this.getText = function(){
return this.EAN5number;
}

this.valid = function(){
return this.EAN5number.search(fullEanRegexp)!==-1;
};

this.encoded = function (){
return createEAN5(this.EAN5number);
}

//Calulate the checksum digit
function checksum(number){
var result = 0;

for(var i=0;i<5;i+=2){result+=parseInt(number[i])*3}
for(var i=1;i<5;i+=2){result+=parseInt(number[i])*9}

return result % 10;
}

var EAN5structure = [
"GGLLL",
"GLGLL",
"GLLGL",
"GLLLG",
"LGGLL",
"LLGGL",
"LLLGG",
"LGLGL",
"LGLLG",
"LLGLG"
];

function createEAN5(number){
var encoder = new EANencoder();

//Create the return variable
var result = "";

//Add the start bits
result += encoder.startBin;

// Add the encodings
result += encoder.encode(number, EAN5structure[checksum(number)], "01");

return result;
}
}

function UPC(UPCnumber){
this.ean = new EAN("0"+UPCnumber);
Expand Down Expand Up @@ -234,10 +282,12 @@ function EANencoder(){
];

//Convert a numberarray to the representing
this.encode = function(number,structure){
this.encode = function(number, structure, separator){
//Create the variable that should be returned at the end of the function
var result = "";

var separator = typeof separator === "undefined" ? "" : separator;

//Loop all the numbers
for(var i = 0;i<number.length;i++){
//Using the L, G or R encoding and add it to the returning variable
Expand All @@ -250,6 +300,11 @@ function EANencoder(){
else if(structure[i]=="R"){
result += Rbinary[number[i]];
}

// Add separator
if(i < number.length - 1){
result += separator;
}
}
return result;
};
Expand All @@ -260,6 +315,7 @@ function EANencoder(){
var register = function(core){
core.register(EAN, /^EAN(.?13)?$/i, 8);
core.register(EAN8, /^EAN.?8$/i, 8);
core.register(EAN5, /^EAN.?5$/i, 5);
core.register(UPC, /^UPC(.?A)?$/i, 8);
}
try{register(JsBarcode)} catch(e){}
Expand Down
2 changes: 2 additions & 0 deletions test/browser/tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
function createTests(parent){
newTest(parent, "This has a \nnewline", {width: 1});
newTest(parent, "\tHi\nHI", {width: 1});
newTest(parent, "A little test!", {format: "CODE128", width: 1});
newTest(parent, "A LITTLE TEST", {format: "CODE39", width: 1});
newTest(parent, "A little test", {format: "CODE39", width: 1});
newTest(parent, "12345", {format: "EAN5", width: 1});
newTest(parent, "123456789999", {format: "UPC", width: 1});
newTest(parent, "5901234123457", {format: "EAN", width: 1});
newTest(parent, "590123412345", {format: "EAN", width: 1});
Expand Down
21 changes: 21 additions & 0 deletions test/node/JsBarcode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Encoders', function() {
EAN = JsBarcode.getModule("EAN");
UPC = JsBarcode.getModule("UPC");
EAN8 = JsBarcode.getModule("EAN8");
EAN5 = JsBarcode.getModule("EAN5");
ITF14 = JsBarcode.getModule("ITF14");
ITF = JsBarcode.getModule("ITF");
MSI = JsBarcode.getModule("MSI");
Expand Down Expand Up @@ -296,6 +297,26 @@ describe('EAN-8', function() {
});
});

describe('EAN-5', function() {
it('should be able to encode normal text', function () {
var enc = new EAN5("54495");
assert.equal(true, enc.valid());
assert.equal(enc.encoded(), "1010110001010100011010011101010001011010111001");

var enc = new EAN5("12345");
assert.equal(true, enc.valid());
assert.equal(enc.encoded(), "1010110011010010011010100001010100011010110001");
});

it('should warn with invalid text', function () {
var enc = new EAN5("1234");
assert.equal(false, enc.valid());

var enc = new EAN5("123a5");
assert.equal(false, enc.valid());
});
});

describe('ITF-14', function() {
it('should be able to encode normal text', function () {
var enc = new ITF14("98765432109213");
Expand Down

0 comments on commit 4f598d5

Please sign in to comment.