all files / barcode/one-dimension/ code128.js

99.22% Statements 128/129
87.1% Branches 54/62
100% Functions 26/26
99.21% Lines 125/126
11 statements, 6 functions, 9 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207          34×           394×                                           394×   30× 30× 380×   30×   24× 24× 12×   12×     24×   13×                             30× 30× 30× 30× 30× 30× 12×   18×     10×   30× 30× 30× 30× 30×   334× 30×   304× 304× 304×       296× 296×   304× 304× 304×                   30× 30× 30× 30× 30×   30× 30×   296× 115× 115×   181× 135×     46×     394× 394×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../one-dimension"], function (require, exports, one_dimension_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Code128 = (function (_super) {
        __extends(Code128, _super);
        function Code128() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        Code128.prototype.validateInput = function (char) {
            if (char.search(/^[0-9A-Za-z\-\.\ \@\$\/\+\%\!\@\#\$\%\&\*\^\(\)\_\+\=\<\>\?\{\}\[\]\~\-\Ê]+$/) === -1) {
                return 'Supports only 128 characters of ASCII.';
            }
            else {
                return undefined;
            }
        };
        Code128.prototype.getCodeValue = function () {
            var codes = [11011001100, 11001101100, 11001100110, 10010011000, 10010001100,
                10001001100, 10011001000, 10011000100, 10001100100, 11001001000,
                11001000100, 11000100100, 10110011100, 10011011100, 10011001110,
                10111001100, 10011101100, 10011100110, 11001110010, 11001011100,
                11001001110, 11011100100, 11001110100, 11101101110, 11101001100,
                11100101100, 11100100110, 11101100100, 11100110100, 11100110010,
                11011011000, 11011000110, 11000110110, 10100011000, 10001011000,
                10001000110, 10110001000, 10001101000, 10001100010, 11010001000,
                11000101000, 11000100010, 10110111000, 10110001110, 10001101110,
                10111011000, 10111000110, 10001110110, 11101110110, 11010001110,
                11000101110, 11011101000, 11011100010, 11011101110, 11101011000,
                11101000110, 11100010110, 11101101000, 11101100010, 11100011010,
                11101111010, 11001000010, 11110001010, 10100110000, 10100001100,
                10010110000, 10010000110, 10000101100, 10000100110, 10110010000,
                10110000100, 10011010000, 10011000010, 10000110100, 10000110010,
                11000010010, 11001010000, 11110111010, 11000010100, 10001111010,
                10100111100, 10010111100, 10010011110, 10111100100, 10011110100,
                10011110010, 11110100100, 11110010100, 11110010010, 11011011110,
                11011110110, 11110110110, 10101111000, 10100011110, 10001011110,
                10111101000, 10111100010, 11110101000, 11110100010, 10111011110,
                10111101110, 11101011110, 11110101110, 11010000100, 11010010000,
                11010011100, 1100011101011];
            return codes;
        };
        Code128.prototype.getBytes = function (givenWord) {
            var bytes = [];
            for (var i = 0; i < givenWord.length; i++) {
                bytes.push(givenWord[parseInt(i.toString(), 10)].charCodeAt(0));
            }
            return bytes;
        };
        Code128.prototype.appendStartStopCharacters = function (char) {
            var startChararcter;
            if (this.type === 'Code128A') {
                startChararcter = String.fromCharCode(208);
            }
            else if (this.type === 'Code128B') {
                startChararcter = String.fromCharCode(209);
            }
            else Eif (this.type === 'Code128C') {
                startChararcter = String.fromCharCode(210);
            }
            return startChararcter + char;
        };
        Code128.prototype.check128C = function (value) {
            return value.match(new RegExp('^' + '(\xCF*[0-9]{2}\xCF*)' + '*'))[0];
        };
        Code128.prototype.check128A = function (value) {
            return value.match(new RegExp('^' + '[\x00-\x5F\xC8-\xCF]' + '*'))[0];
        };
        Code128.prototype.check128B = function (value) {
            return value.match(new RegExp('^' + '[\x20-\x7F\xC8-\xCF]' + '*'))[0];
        };
        Code128.prototype.clipAB = function (value, code128A) {
            var ranges = code128A ? '[\x00-\x5F\xC8-\xCF]' : '[\x20-\x7F\xC8-\xCF]';
            var untilC = value.match(new RegExp('^(' + ranges + '+?)(([0-9]{2}){2,})([^0-9]|$)'));
            if (untilC) {
                return untilC[1] + String.fromCharCode(204) + this.clipC(value.substring(untilC[1].length));
            }
            var chars = value.match(new RegExp('^' + ranges + '+'))[0];
            Eif (chars.length === value.length) {
                return value;
            }
            return value;
        };
        Code128.prototype.code128Clip = function () {
            var newString;
            var check128C = this.check128C(this.value).length;
            if (check128C >= 2) {
                return newString = String.fromCharCode(210) + this.clipC(this.value);
            }
            else {
                var code128A = this.check128A(this.value) > this.check128B(this.value);
                return newString = (code128A ? String.fromCharCode(208) : String.fromCharCode(209)) + this.clipAB(this.value, code128A);
            }
        };
        Code128.prototype.clipC = function (string) {
            var cMatch = this.check128C(string);
            var length = cMatch.length;
            if (length === string.length) {
                return string;
            }
            string = string.substring(length);
            var code128A = this.check128A(string) >= this.check128B(string);
            return cMatch + String.fromCharCode(code128A ? 206 : 205) + this.clipAB(string, code128A);
        };
        Code128.prototype.draw = function (canvas) {
            this.code128(canvas);
        };
        Code128.prototype.code128 = function (canvas) {
            var givenCharacter = this.value;
            givenCharacter = this.type !== 'Code128' ? this.appendStartStopCharacters(givenCharacter) : this.code128Clip();
            var bytes = this.getBytes(givenCharacter);
            var startCharacterValue = bytes.shift() - 105;
            var set;
            if (startCharacterValue === 103) {
                set = '0';
            }
            else if (startCharacterValue === 104) {
                set = '1';
            }
            else {
                set = '2';
            }
            var encodingResult = this.encodeData(bytes, 1, set);
            var encodedData = this.encode(startCharacterValue, encodingResult);
            var code = [];
            code.push(encodedData);
            this.calculateBarCodeAttributes(code, canvas);
        };
        Code128.prototype.encodeData = function (byteValue, textPosition, set) {
            if (!byteValue.length) {
                return { result: '', checksum: 0 };
            }
            var nextCode;
            var index;
            if (byteValue[0] >= 200) {
                index = byteValue.shift() - 105;
                var nextSet = this.swap(index);
                Eif (nextSet !== undefined) {
                    nextCode = this.encodeData(byteValue, textPosition + 1, nextSet);
                }
            }
            else {
                index = this.correctIndex(byteValue, set);
                nextCode = this.encodeData(byteValue, textPosition + 1, set);
            }
            var encodingValues = this.getCodes(index);
            var weight = index * textPosition;
            return {
                result: encodingValues + nextCode.result,
                checksum: weight + nextCode.checksum
            };
        };
        Code128.prototype.swap = function (index) {
            if (index === 99) {
                return '2';
            }
            else if (index === 100) {
                return '1';
            }
            else {
                return '0';
            }
        };
        Code128.prototype.encode = function (startIndex, encodingResult) {
            var moduloValue = 103;
            var stopvalue = 106;
            var encodeValue = this.getCodes(startIndex) + encodingResult.result;
            Eif (this.enableCheckSum) {
                encodeValue += this.getCodes((encodingResult.checksum + startIndex) % moduloValue);
            }
            encodeValue += this.getCodes(stopvalue);
            return encodeValue;
        };
        Code128.prototype.correctIndex = function (bytes, set) {
            if (set === '0') {
                var charCode = bytes.shift();
                return charCode < 32 ? charCode + 64 : charCode - 32;
            }
            else if (set === '1') {
                return bytes.shift() - 32;
            }
            else {
                return (bytes.shift() - 48) * 10 + bytes.shift() - 48;
            }
        };
        Code128.prototype.getCodes = function (index) {
            var codes = this.getCodeValue();
            return codes[parseInt(index.toString(), 10)] ? codes[parseInt(index.toString(), 10)].toString() : '';
        };
        return Code128;
    }(one_dimension_1.OneDimension));
    exports.Code128 = Code128;
});