All files / lib/Service CustomMathsService.js

45.71% Statements 16/35
0% Branches 0/29
40% Functions 2/5
45.71% Lines 16/35

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  1x 1x   1x 2x   1x                                                   1x     1x                 1x   3x 3x 3x   3x 3x 3x   3x     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomMathsService = void 0;
class CustomMathsService {
    static round = (num, digits) => {
        return parseFloat(num).toFixed(digits);
    };
    static calculateFromByte = (byte, roundDigits = 2) => {
        byte = parseFloat(byte);
        let result = '';
        if (byte < 1024) {
            result = this.round(byte, roundDigits) + ' Byte';
        }
        else if (byte >= 1024 && byte < Math.pow(1024, 2)) {
            result = this.round(byte / 1024, roundDigits) + ' KB';
        }
        else if (byte >= Math.pow(1024, 2) && byte < Math.pow(1024, 3)) {
            result = this.round(byte / Math.pow(1024, 2), roundDigits) + ' MB';
        }
        else if (byte >= Math.pow(1024, 3) && byte < Math.pow(1024, 4)) {
            result = this.round(byte / Math.pow(1024, 3), roundDigits) + ' GB';
        }
        else if (byte >= Math.pow(1024, 4) && byte < Math.pow(1024, 5)) {
            result = this.round(byte / Math.pow(1024, 4), roundDigits) + ' TB';
        }
        else if (byte >= Math.pow(1024, 5) && byte < Math.pow(1024, 6)) {
            result = this.round(byte / Math.pow(1024, 5), roundDigits) + ' PB';
        }
        else if (byte >= Math.pow(1024, 6) && byte < Math.pow(1024, 7)) {
            result = this.round(byte / Math.pow(1024, 6), roundDigits) + ' EB';
        }
        return result;
    };
    static byteToGb = (byte, roundDigits = 2) => {
        return this.round(byte / Math.pow(1024, 3), roundDigits);
    };
    static byteToMb = (byte, roundDigits = 2) => {
        return this.round(byte / Math.pow(1024, 2), roundDigits);
    };
    /**
     * Returns a securely generated random int between the given borders.
     * Maximum supported (and enforced) range between min and max is 256 (0...255).
     * @param min exclusive lower border
     * @param max exclusive upper border
     */
    static getSecureRandomInt = (min, max) => {
        // Create byte array and fill with 1 random number
        const byteArray = new Uint8Array(1);
        const max_range = 256;
        const range = Math.min(max - min + 1, max_range);
        let randomNumber;
        do {
            crypto.getRandomValues(byteArray);
            randomNumber = byteArray[0];
        } while (randomNumber >= Math.floor(max_range / range) * range);
        return min + (randomNumber % range);
    };
}
exports.CustomMathsService = CustomMathsService;