Skip to content

Commit

Permalink
Added EAN-2 and fixed startcode of EAN-5
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Mar 29, 2016
1 parent 30aa28c commit 35c8c87
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
39 changes: 35 additions & 4 deletions barcodes/EAN_UPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ function EAN5(EAN5number){
var encoder = new EANencoder();

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

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

// Add the encodings
result += encoder.encode(number, EAN5structure[checksum(number)], "01");
Expand All @@ -212,6 +209,39 @@ function EAN5(EAN5number){
}
}

function EAN2(EAN2number){
this.EAN2number = EAN2number+"";

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

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

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

this.encoded = function (){
return createEAN2(this.EAN2number);
}

var EAN2structure = ["LL", "LG", "GL", "GG"]

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

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

// Add the encodings
result += encoder.encode(number, EAN2structure[parseInt(number) % 4], "01");

return result;
}
}

function UPC(UPCnumber){
this.ean = new EAN("0"+UPCnumber);

Expand Down Expand Up @@ -316,6 +346,7 @@ 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(EAN2, /^EAN.?2$/i, 5);
core.register(UPC, /^UPC(.?A)?$/i, 8);
}
try{register(JsBarcode)} catch(e){}
Expand Down
1 change: 1 addition & 0 deletions test/browser/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function createTests(parent){
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, "52", {format: "EAN2", 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
25 changes: 23 additions & 2 deletions test/node/JsBarcode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Encoders', function() {
UPC = JsBarcode.getModule("UPC");
EAN8 = JsBarcode.getModule("EAN8");
EAN5 = JsBarcode.getModule("EAN5");
EAN2 = JsBarcode.getModule("EAN2");
ITF14 = JsBarcode.getModule("ITF14");
ITF = JsBarcode.getModule("ITF");
MSI = JsBarcode.getModule("MSI");
Expand Down Expand Up @@ -301,11 +302,11 @@ 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");
assert.equal(enc.encoded(), "10110110001010100011010011101010001011010111001");

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

it('should warn with invalid text', function () {
Expand All @@ -317,6 +318,26 @@ describe('EAN-5', function() {
});
});

describe('EAN-2', function() {
it('should be able to encode normal text', function () {
var enc = new EAN2("53");
assert.equal(true, enc.valid());
assert.equal(enc.encoded(), "10110110001010100001");

var enc = new EAN2("12");
assert.equal(true, enc.valid());
assert.equal(enc.encoded(), "10110011001010010011");
});

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

var enc = new EAN2("a2");
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 35c8c87

Please sign in to comment.