All files time.ts

69.33% Statements 52/75
47.37% Branches 18/38
60% Functions 6/10
77.97% Lines 46/59

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1417x 7x 7x 7x                     7x       7x             7x             7x             7x         7x 3x 3x 3x 3x 3x   3x 3x   3x             3x             3x     3x 12x 12x   12x 12x   4x 4x     3x                 8x 4x 4x       4x 4x 4x 4x 4x     4x 2x   1x   1x     1x                     2x   2x   2x   2x 1x                    
import { deepMergeRight } from "./deepMerge";
import { entries } from "./entries";
import { isOneOf } from "./isOneOf";
import { toDate } from "./parse";
import { DeepPartial } from "./types";
 
interface PrettyMs {
    days: { unit: string },
    hours: { unit: string },
    minutes: { unit: string },
    seconds: { unit: string }
}
 
// P.Time.Day(200)
export namespace Time {
    /**
     * Converts seconds to milliseconds
     */
    export function Second(seconds: number) {
        return seconds * 1000;
    }
 
    /**
     * Converts minutes to milliseconds
     */
    export function Minute(minutes: number) {
        return Second(minutes) * 60;
    }
 
    /**
     * Converts hours to milliseconds
     */
    export function Hour(h: number) {
        return Minute(h) * 60;
    }
 
    /**
     * Converts days to milliseconds
     */
    export function Day(d: number) {
        return Hour(d) * 24;
    }
}
 
export function prettyMs(milliseconds: number, options?: DeepPartial<PrettyMs>): string {
    const seconds = Math.floor(milliseconds / 1000)
    const d_seconds = Math.floor(milliseconds / 1000) % 60
    const minutes = (seconds - d_seconds) / 60
    const d_minutes = minutes % 60
    const hours = (minutes - d_minutes) / 60
 
    const d_hours = hours % 24
    const days = (hours - d_hours) / 24
 
    const val = {
        seconds: d_seconds,
        minutes: d_minutes,
        hours: d_hours,
        days: days
    }
 
    const defaultUnit = {
        days: { unit: 'd' },
        hours: { unit: 'h' },
        minutes: { unit: 'min' },
        seconds: { unit: 's' }
    }
 
    const merged = deepMergeRight(defaultUnit, options || {})
 
 
    const entriesV = entries(merged)
        .filter(([k]) => {
            return isOneOf(k, ['days', 'hours', 'minutes', 'seconds'])
        })
        .filter(([k]) => {
            return val[k]
        })
        .map(([k, v]) => {
            return `${val[k]}${v.unit}`
        }).join(" ")
 
    return entriesV
}
 
 
/**
 * 
 * @param date 
 * @param from - {optional} time diff 
 */
export function prettyTimeDiff(date: number | Date | string, Efrom: number = Date.now()): string {
    const validDate = toDate(date)
    Iif (validDate == null) {
        throw new Error(`${date} is not valid date format`)
    }
 
    const dateDiff = (from - validDate.getTime())
    const delta = Math.floor(dateDiff / 1000);
    const minute = 60,
        hour = minute * 60,
        day = hour * 24
 
 
    if (delta > 0) {
        if (delta < 30) return 'just now';
 
        Iif (delta < minute) return delta + ' seconds ago';
 
        Iif (delta < 2 * minute) return 'a minute ago'
 
 
        Eif (delta < hour) return Math.floor(delta / minute) + ' minutes ago';
 
        if (Math.floor(delta / hour) == 1) return '1 hour ago'
 
        if (delta < day) return Math.floor(delta / hour) + ' hours ago';
 
        if (delta < day * 2) return 'yesterday';
 
        return Math.floor(delta / day) + ' days ago';
    }
 
    const posDelta = Math.abs(delta)
 
    Iif (posDelta < 30) return 'just now';
 
    Iif (posDelta < minute) return `after ${delta} seconds`;
 
    if (posDelta < 2 * minute) return 'after one minute'
    Eif (posDelta < hour) return `after ${Math.floor(posDelta / minute)} minutes`;
 
    if (Math.floor(posDelta / hour) == 1) return 'after one hour'
 
    if (posDelta < day) return `after ${Math.floor(posDelta / hour)} hours`;
 
    if (posDelta < day * 2) return 'tomorrow';
 
    return `after ${Math.floor(delta / day)} days`;
}