-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); |