All files / util index.js

100% Statements 16/16
100% Branches 2/2
100% Functions 5/5
100% Lines 14/14

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          3x 1x 1x                 3x 3x         4x                 3x 1x 1x 1x 32x 32x   1x       3x          
/**
 * Sleep n milliseconds.
 * @param {number} ms
 * @return {Promise}
 */
const sleep = async (ms) => {
    return new Promise(resolve => {
        setTimeout(resolve, ms);
    });
};
 
/**
 * Converts base64 string to URL safe base64
 * @param {String} base64String
 * @return {String} url safe base64
 */
const base64ToURLSafe = (base64String) => {
    const INVALID_CHARS = {
        '+': '-',
        '/': '_',
        '=': '',
    };
    return base64String.replace(/[+/=]/g, (m) => INVALID_CHARS[m]);
};
 
/**
 * Convert given Base64 string to hex and
 * return result.
 * @param {string} base64String
 * @return {string} in hex
 */
const base64ToHex = (base64String) => {
    const proc = atob(base64String);
    let result = '';
    for (let i = 0; i < proc.length; i++) {
        const hex = proc.charCodeAt(i).toString(16);
        result += (hex.length === 2 ? hex : '0' + hex);
    }
    return result;
};
 
 
module.exports = {
    sleep,
    base64ToURLSafe,
    base64ToHex,
};