All files / src/lib utils.ts

94.59% Statements 35/37
73.68% Branches 14/19
100% Functions 6/6
93.75% Lines 30/32

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  1x 1x 1x 38x 38x 8002828x 8002828x 8002826x 2x 1x 1x 1x         38x     1x 10x 10x 10x 10x                 1x 47x 47x 47x 47x     1x 34x             1x 24x 6x     6x     1x  
type MiliSeconds = number;
export const K_BYTE = 1024;
export const M_BYTE = 1024 * K_BYTE;
export function sizeOf (str: string): number {
    let total = 0;
    for (let i = 0, len = str.length; i < len; i++) {
        const charCode = str.charCodeAt(i);
        if (charCode <= 0x007f) {
            total += 1;
        } else if (charCode <= 0x07ff) {
            total += 2;
        } else Eif (charCode <= 0xffff) {
            total += 3;
        } else {
            total += 4;
        }
    }
    return total;
}
 
export function isValidDay (day: string): boolean {
    const dayParts = day.split('-');
    const M = parseInt(dayParts[1]);
    const D = parseInt(dayParts[2]);
    return (
        M > 0 &&
        M <= 12 &&
        D > 0 &&
        D <= 31 &&
        new Date(day).toString() !== 'Invalid Date'
    );
}
 
export function dateFormat2Day (date: Date): string {
    const Y = date.getFullYear();
    const M = date.getMonth() + 1;
    const D = date.getDate();
    return `${Y}-${M < 10 ? '0' + M : M}-${D < 10 ? '0' + D : D}`;
}
 
export function getStartOfDay (date: Date): MiliSeconds {
    return new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate()
    ).getTime();
}
 
export function dayFormat2Date (day: string): Date {
    const [year, month, date] = (day.match(/(\d+)/g) || []).map(n => parseInt(n));
    Iif (year < 1000) {
        throw new Error(`Invalid dayString: ${day}`);
    }
    return new Date(year, month - 1, date);
}
 
export const ONE_DAY_TIME_SPAN = 24 * 60 * 60 * 1000;