All files / lib/services/mapper reportMappers.js

100% Statements 47/47
87.5% Branches 21/24
100% Functions 6/6
100% Lines 47/47

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 491x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 1x 1x 52x 52x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 1x 140x 133x 140x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x    
export const formatDate = (isoStr) => {
    const date = new Date(isoStr);
    return date.toLocaleTimeString('fr-FR', {
        hour: '2-digit',
        minute: '2-digit',
    }) + ' ' + date.toLocaleDateString('fr-FR', {
        day: '2-digit',
        month: '2-digit',
    });
};
export const pad = (num, padLength = 3) => String(num).padStart(padLength, ' ');
 
export const plural = (count, singular, plural, padLength = 1) => {
    return `${pad(count, padLength)} ${count > 1 ? plural : singular}`;
}
export const secondsToHms = (nbSeconds) => {
    nbSeconds = Number(nbSeconds);
    let h = Math.floor(nbSeconds / 3600);
    let m = Math.floor(nbSeconds % 3600 / 60);
    let s = Math.floor(nbSeconds % 3600 % 60);
 
    let hDisplay = h !== 0 ? (Math.abs(h) + "h ") : "";
    let mDisplay = m !== 0 ? (Math.abs(m) + "min ") : "";
    let sDisplay = s !== 0 ? (Math.abs(s) + "sec") : "";
    let result = (nbSeconds < 0 ? "-" : "") + hDisplay + mDisplay + sDisplay;
    return result === "" ? "none" : result;
}
 
export const intValueOf = mixed => {
    if (isNaN(mixed) || mixed === null || mixed === undefined) return 0;
    return mixed;
}
 
export const formatSession = sess => {
    const {
        browser, os, screen, language,
        country, subdivision1, city,
        firstAt, lastAt, visits, views
    } = sess;
 
    const loc = `${country}${city ? ` ${subdivision1 || ''} ${city}` : ''}`;
    const period = `${formatDate(firstAt)} - ${formatDate(lastAt)}`;
    const view_s = plural(views, 'view ', 'views', 3);
    const visit_s = plural(visits, 'visit ', 'visits', 3);
 
    return ` - ${view_s} ${visit_s} | period: ${period} | ${loc.trim()} (${language}) with ${os} ${browser} ${screen}`;
}