Skip to content

Commit

Permalink
Forced mode CODE128 is now properly detecting invalid barcodes.
Browse files Browse the repository at this point in the history
Added tests for this as well
  • Loading branch information
lindell committed Mar 26, 2016
1 parent 2ff1cd2 commit d4f985d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
18 changes: 15 additions & 3 deletions barcodes/CODE128.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,25 @@ function CODE128AUTO(string) {
return new CODE128(string);
}
function CODE128A(string) {
return new CODE128(String.fromCharCode(208) + string);
var code128 = new CODE128(String.fromCharCode(208) + string);
code128.valid = function(){
return this.string.search(/^[\x00-\x5F\xC8-\xCF]+$/) !== -1;
}
return code128;
}
function CODE128B(string) {
return new CODE128(String.fromCharCode(209) + string);
var code128 = new CODE128(String.fromCharCode(209) + string);
code128.valid = function(){
return this.string.search(/^[\x20-\x7F\xC8-\xCF]+$/) !== -1;
}
return code128;
}
function CODE128C(string) {
return new CODE128(String.fromCharCode(210) + string);
var code128 = new CODE128(String.fromCharCode(210) + string);
code128.valid = function(str){
return this.string.search(/^(\xCF*[0-9]{2}\xCF*)+$/) !== -1;
}
return code128;
}

//Required to register for both browser and nodejs
Expand Down
2 changes: 1 addition & 1 deletion test/browser/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createTests(parent){
newTest(parent, "12345678", {format: "ITF", width: 1});
newTest(parent, "98765432109213", {format: "ITF14", width: 1});
newTest(parent, "12345", {format: "pharmacode", width: 1});
newTest(parent, "1337420", {format: "CODE128C", width: 1});
newTest(parent, "133742", {format: "CODE128C", width: 1});
newTest(parent, "12345674", {format: "MSI", width: 1});
newTest(parent, "Such customize!", {
width: 1,
Expand Down
26 changes: 26 additions & 0 deletions test/node/JsBarcode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,32 @@ describe('CODE128', function() {
it('should warn with invalid text', function () {
var enc = new CODE128("ABC" + String.fromCharCode(500));
assert.equal(false, enc.valid());

var enc = new CODE128A("Abc");
assert.equal(false, enc.valid());

var enc = new CODE128B("Abc\t123");
assert.equal(false, enc.valid());

var enc = new CODE128C("1234ab56");
assert.equal(false, enc.valid());

var enc = new CODE128C("12345");
assert.equal(false, enc.valid());
});

it('should pass valid text', function () {
var enc = new CODE128("ABC" + String.fromCharCode(207));
assert.equal(true, enc.valid());

var enc = new CODE128A("ABC\t\n123");
assert.equal(true, enc.valid());

var enc = new CODE128B("Abc123" + String.fromCharCode(202));
assert.equal(true, enc.valid());

var enc = new CODE128C("123456");
assert.equal(true, enc.valid());
});
});

Expand Down

0 comments on commit d4f985d

Please sign in to comment.