All files utils.ts

100% Statements 42/42
100% Branches 27/27
100% Functions 5/5
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 831x   1x 31x   31x 2x     31x 62x 62x   31x     1x 66x   66x 66x 66x   66x                       1x       15x 15x 15x 15x 59x 59x 59x   57x 2x   1x 1x     59x   1x 1x 1x           15x     65x 33x 1x     32x 32x 325x 325x 2600x     32x      
export module Util {
 
    export function toByteArray(hexString: string): number[] {
        var result = [];
 
        while (hexString.length % 2 != 0) {
            hexString = '0' + hexString;
        }
 
        while (hexString.length >= 2) {
            result.push(parseInt(hexString.substring(0, 2), 16));
            hexString = hexString.substring(2, hexString.length);
        }
        return result;
    }
 
    export function intTo2Bytes(lenght: number): number[] {
        let bytes = new Array(1);
 
        bytes[1] = lenght & (255);
        lenght = lenght >> 8
        bytes[0] = lenght & (255);
 
        return bytes;
    }
 
    // http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
 
    /* utf.js - UTF-8 <=> UTF-16 convertion
     *
     * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
     * Version: 1.0
     * LastModified: Dec 25 1999
     * This library is free.  You can redistribute it and/or modify it.
     */
    export function Utf8ArrayToStr(array: Uint8Array): string {
        var out, i, len, c;
        var char2, char3;
 
        out = "";
        len = array.length;
        i = 0;
        while (i < len) {
            c = array[i++];
            let c4 = c >> 4;
            if (c4 === 0 || c4 === 1 || c4 === 2 || c4 === 3 || c4 === 4 || c4 === 5 || c4 === 6 || c4 === 7) {
                // 0xxxxxxx
                out += String.fromCharCode(c);
            } else if (c4 === 12 || c4 === 13) {
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
            }
 
            if (c4 === 14) {
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0F) << 12) |
                    ((char2 & 0x3F) << 6) |
                    ((char3 & 0x3F) << 0));
            }
        }
 
        return out;
    }
 
    export function crc16(data: Buffer, offset: number = 0): number {
        if (data == null || offset < 0 || offset > data.length - 1) {
            return 0;
        }
 
        let crc = 0xFFFF;
        for (let i = 0; i < data.length; ++i) {
            crc ^= data[offset + i] << 8;
            for (let j = 0; j < 8; ++j) {
                crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
            }
        }
        return crc & 0xFFFF;
    }
 
}