All files / lib Common.js

95.45% Statements 84/88
95.45% Branches 21/22
70.58% Functions 12/17
95.45% Lines 84/88

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 83 84 85 86 87 881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 1x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 15x 15x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 7x     7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x
import path from 'node:path';
import fs from 'node:fs';
import process from "node:process";
import dayjs from "dayjs";
 
import utc from "dayjs/plugin/utc.js"
import timezone from "dayjs/plugin/timezone.js"
import axios from "axios"; // dependent on utc plugin
 
dayjs.extend(utc)
dayjs.extend(timezone)
 
export const DEFAULT_TZ = 'Europe/Paris'
export const BES_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss";
const __dirname = path.resolve();
 
export const readSync = projectFilePath => {
    const finalFilename = projectFilePath.startsWith('./') ?
        __dirname + '/' + projectFilePath.substring(2) :
        projectFilePath;
    return fs.readFileSync(finalFilename, 'utf8');
}
export const loadJsonResource = jsonProjectFilePath => {
    const fileContent = readSync(jsonProjectFilePath);
    return JSON.parse(fileContent);
}
 
// please use it through MemoryCache
export const getPackageJsonContent = () => loadJsonResource('./package.json');
 
export const clone = obj => JSON.parse(JSON.stringify(obj));
 
export function getEnv(varName, defaultValue = null) {
    return process.env[varName] || defaultValue;
}
 
export function getEnvInt(varName, defaultValue = null) {
    return process.env[varName] | 0 || defaultValue; // | 0 convert to int
}
 
export const isSet = value => value !== undefined && value !== null && value !== "";
 
export const arrayIsNotEmpty = arr => isSet(arr) && arr.length > 0;
 
export const assumePropertyIsSet = (expectedValue, name) => {
    if (!isSet(expectedValue)) {
        throw new Error(`application properties - expect following '${name}' value to be set`);
    }
    return expectedValue;
}
 
export const nowISO8601 = () => dayjs().toISOString(); // '2019-01-25T02:00:00.000Z'
// dayjs doc: https://day.js.org/docs/en/manipulate/subtract
export const nowMinusHoursUTCISO = (nbHours = 1) => dayjs.utc().subtract(nbHours, 'hour').toISOString()
export const nowHuman = (tz = DEFAULT_TZ) => dayjs().tz(tz).format(BES_DATE_FORMAT);
export const toHuman = (utcDateTimeString, tz= DEFAULT_TZ) => {
    return dayjs.utc(utcDateTimeString).tz(tz).format(BES_DATE_FORMAT);
};
export const toHumanDay = (utcDateTimeString, tz= DEFAULT_TZ) => {
    return dayjs.utc(utcDateTimeString).tz(tz).format("YYYY-MM-DD");
};
export const toHumanTime = (utcDateTimeString, tz= DEFAULT_TZ) => {
    return dayjs.utc(utcDateTimeString).tz(tz).format("HH:mm:ss");
};
/*
  getParisNowDate() {
    return new Date().toLocaleString('fr-FR', {
       timeZone: 'Europe/Paris'
    });
 */
 
export const generateErrorId = () => "ERR_" + dayjs().format("YYYYMMDDHHmmss");
 
export const getEncodingBufferAndBase64FromUri = imageUri => {
    return new Promise((resolve, reject) => {
        axios
            .get(imageUri, {
                responseType: 'arraybuffer'
            })
            .then(response => {
                const encoding = response.headers["content-type"];
                const buffer = Buffer.from(response.data, 'binary');/* incoming data are binary */
                const base64 = buffer.toString('base64');
                resolve({encoding, buffer, base64});
            })
            .catch(reject)
    });
};