Skip to content

Commit

Permalink
Re-added ITF support
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed May 17, 2016
1 parent 7c39418 commit a10ea67
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions barcode-building.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"names": "ITF14",
"barcodeFile": "./ITF14"
},
{
"filename": "JsBarcode.itf.min.js",
"names": "ITF",
"barcodeFile": "./ITF"
},
{
"filename": "JsBarcode.msi.min.js",
"names": "MSI, MSI10, MSI11, MSI1010, MSI1110",
Expand Down
58 changes: 58 additions & 0 deletions src/barcodes/ITF/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class ITF{
constructor(string){
this.string = string;

this.binaryRepresentation = {
"0":"00110"
, "1":"10001"
, "2":"01001"
, "3":"11000"
, "4":"00101"
, "5":"10100"
, "6":"01100"
, "7":"00011"
, "8":"10010"
, "9":"01010"
};
}

valid(){
return this.string.search(/^([0-9]{2})+$/) !== -1;
}

encode(){
// Always add the same start bits
var result = "1010";

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

// Always add the same end bits
result += "11101";

return {
data: result,
text: this.string
};
}

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

var number1Struct = this.binaryRepresentation[numberPair[0]];
var number2Struct = this.binaryRepresentation[numberPair[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;
}
}

export {ITF};
2 changes: 2 additions & 0 deletions src/barcodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {CODE39} from './CODE39/';
import {CODE128, CODE128A, CODE128B, CODE128C} from './CODE128/';
import {EAN13, EAN8, EAN5, EAN2, UPC} from './EAN_UPC/';
import {ITF14} from './ITF14/';
import {ITF} from './ITF/';
import {MSI, MSI10, MSI11, MSI1010, MSI1110} from './MSI/';
import {pharmacode} from './pharmacode/';
import {GenericBarcode} from './GenericBarcode/';
Expand All @@ -11,6 +12,7 @@ export default {
CODE128, CODE128A, CODE128B, CODE128C,
EAN13, EAN8, EAN5, EAN2, UPC,
ITF14,
ITF,
MSI, MSI10, MSI11, MSI1010, MSI1110,
pharmacode,
GenericBarcode
Expand Down
30 changes: 30 additions & 0 deletions test/node/ITF.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var assert = require('assert');
var JsBarcode = require('../../bin/node/JsBarcode.js');
var Canvas = require("canvas");


describe('ITF', function() {
it('should be able to include the encoder(s)', function () {
ITF = JsBarcode.getModule("ITF");
console.log(ITF);
});

it('should be able to encode normal text', function () {
var enc = new ITF("123456");
assert.equal("101011101000101011100011101110100010100011101000111000101011101"
, enc.encode().data);
});

it('should return getText correct', function () {
var enc = new ITF("123456");
assert.equal("123456", enc.encode().text);
});

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

var enc = new ITF("1234AB");
assert.equal(false, enc.valid());
});
});

0 comments on commit a10ea67

Please sign in to comment.