All files converters.ts

100% Statements 58/58
100% Branches 24/24
100% Functions 7/7
100% Lines 58/58

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 591x 1x 6x 6x 4x 4x 2x 2x 2x 1x 5x 5x 2x 2x 3x 3x 3x 1x 6x 6x 4x 4x 2x 2x 2x 1x 5x 5x 3x 3x 2x 2x 2x 1x 6x 6x 4x 4x 2x 2x 2x 2x 1x 6x 6x 3x 3x 3x 3x 3x 1x 6x 6x 3x 3x 3x 3x 3x  
import { is_valid_nibble, is_valid_u8, is_valid_u16, is_valid_u32 } from "./validators";
 
export function two_nibble_to_one_u8(nibble_low: number, nibble_high: number): number {
    if (! is_valid_nibble(nibble_low) || ! is_valid_nibble(nibble_high)) {
        throw new Error("util::two_nibble_to_one_byte::is_valid_nibble::false");
    }
 
    return nibble_low << 4 | nibble_high;
}
 
export function one_u8_to_two_nibbles(u8: number): [number, number] {
    if (! is_valid_u8(u8)) {
        throw new Error("util::one_u8_to_two_nibbles::is_valid_u8::false");
    }
 
    return [u8 & 0xF, u8 >> 4];
}
 
export function two_u8_to_u16(byte_low: number, byte_high: number): number  {
    if (! is_valid_u8(byte_low) || ! is_valid_u8(byte_high)) {
        throw new Error(`util::two_u8_to_u16::is_valid_u8::false`);
    }
 
    return byte_high << 8 | byte_low;
}
 
export function one_u16_to_two_u8(u16: number): [number, number] {
    if (! is_valid_u16(u16)) {
        throw new Error("util::one_u16_to_two_u8::is_valid_u16::false");
    }
 
    return [u16 & 0xFF, u16 >> 8];
}
 
export function two_u16_to_one_u32(low: number, high: number): number {
    if (! is_valid_u16(low) || ! is_valid_u16(high)) {
        throw new Error("util::two_u16_to_one_u32::is_valid_u16::false");
    }
 
    // The >>> 0 simply converts a signed number to an unsigned number.
    return (high << 16 >>> 0  | low) >>> 0;
}
 
export function one_u32_to_two_u16(u32: number): [number, number] {
    if (! is_valid_u32(u32)) {
        throw new Error("util::one_u32_to_two_u16::is_valid_u32::false");
    }
 
    return [u32 & 0xFFFF, u32 >>> 16];
}
 
export function three_u8_to_u32(byte_low: number, byte_mid: number, byte_high: number): number {
    if (! is_valid_u8(byte_low) || ! is_valid_u8(byte_mid) || ! is_valid_u8(byte_high)) {
        throw new Error("util::three_u8_to_u32::is_valid_u8::false");
    }
 
    return (byte_high << 16) | (byte_mid << 8 | byte_low);
}