From a10ea677fd3ef0e00f54003250dbf6d59acb63ad Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 17 May 2016 10:48:55 +0200 Subject: [PATCH] Re-added ITF support --- barcode-building.json | 5 ++++ src/barcodes/ITF/index.js | 58 +++++++++++++++++++++++++++++++++++++++ src/barcodes/index.js | 2 ++ test/node/ITF.test.js | 30 ++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 src/barcodes/ITF/index.js create mode 100644 test/node/ITF.test.js diff --git a/barcode-building.json b/barcode-building.json index 05d0c7cc..a038082c 100644 --- a/barcode-building.json +++ b/barcode-building.json @@ -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", diff --git a/src/barcodes/ITF/index.js b/src/barcodes/ITF/index.js new file mode 100644 index 00000000..8f6d9eac --- /dev/null +++ b/src/barcodes/ITF/index.js @@ -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}; diff --git a/src/barcodes/index.js b/src/barcodes/index.js index 6c9c5020..638f9b77 100644 --- a/src/barcodes/index.js +++ b/src/barcodes/index.js @@ -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/'; @@ -11,6 +12,7 @@ export default { CODE128, CODE128A, CODE128B, CODE128C, EAN13, EAN8, EAN5, EAN2, UPC, ITF14, + ITF, MSI, MSI10, MSI11, MSI1010, MSI1110, pharmacode, GenericBarcode diff --git a/test/node/ITF.test.js b/test/node/ITF.test.js new file mode 100644 index 00000000..2a7ad2ed --- /dev/null +++ b/test/node/ITF.test.js @@ -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()); + }); +});