Skip to content

Commit

Permalink
Added support for ITF-14 barcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Dec 3, 2013
1 parent f5196ae commit 2892057
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
95 changes: 95 additions & 0 deletions ITF14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function ITF14(ITF14number){

this.ITF14number = ITF14number+"";

this.valid = function(){
return valid(this.ITF14number);
};

this.encoded = function (){
if(valid(this.ITF14number)){
return encode(this.ITF14number);
}
return "";
}

//The structure for the all digits, 1 is wide and 0 is narrow
var digitStructure = {
"0":"00110"
,"1":"10001"
,"2":"01001"
,"3":"11000"
,"4":"00101"
,"5":"10100"
,"6":"01100"
,"7":"00011"
,"8":"10010"
,"9":"01010"}

//The start bits
var startBin = "1010";
//The end bits
var endBin = "11101";

//Regexp for a valid ITF14 code
var regexp = /^[0-9]{13,14}$/;

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

//If checksum is not already calculated, do it
if(number.length == 13){
number += checksum(number);
}

//Always add the same start bits
result += startBin;

//Calculate all the digit pairs
for(var i=0;i<14;i+=2){
result += calculatePair(number.substr(i,2));
}

//Always add the same end bits
result += endBin;

return result;
}

//Calculate the data of a number pair
function calculatePair(twoNumbers){
var result = "";

var number1Struct = digitStructure[twoNumbers[0]];
var number2Struct = digitStructure[twoNumbers[1]];

//Take every second bit and add to the result
for(var i=0;i<5;i++){
result += (number1Struct[i]=="1") ? "111" : "1";
result += (number2Struct[i]=="1") ? "000" : "0";
}
return result;
}

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

for(var i=0;i<13;i++){result+=parseInt(numberString[i])}

return result % 10;
}

function valid(number){
if(number.search(regexp)==-1){
return false;
}
//Check checksum if it is already calculated
else if(number.length==14){
return number[13] == checksum(number);
}
return true;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The plugin uses Html5Canvas to generate draw the barcodes
* EAN (13)
* UPC-A
* CODE39
* ITF14

Use
----
Expand Down

0 comments on commit 2892057

Please sign in to comment.