All files / src/components/utils dates.ts

82.35% Statements 14/17
70% Branches 14/20
100% Functions 6/6
82.35% Lines 14/17

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 6022x       2254x               69x               275x 140x     135x     135x       135x               2123x     2123x 46x     2077x       9x             6520x    
import {dateTime} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
 
export function isWeekend(date: DateTime) {
    return [0, 6].includes(date.day());
}
 
export interface PlaceholderValueOptions {
    placeholderValue?: DateTime;
    timeZone?: string;
}
export function createPlaceholderValue({placeholderValue, timeZone}: PlaceholderValueOptions) {
    return placeholderValue ?? dateTime({timeZone}).startOf('day');
}
 
export function isInvalid(
    value: DateTime | null | undefined,
    minValue: DateTime | undefined,
    maxValue: DateTime | undefined,
) {
    if (!value) {
        return false;
    }
 
    Iif (minValue && value.isBefore(minValue)) {
        return true;
    }
    Iif (maxValue && maxValue.isBefore(value)) {
        return true;
    }
 
    return false;
}
 
export function constrainValue(
    value: DateTime,
    minValue: DateTime | undefined,
    maxValue: DateTime | undefined,
) {
    Iif (minValue && value.isBefore(minValue)) {
        return minValue;
    }
    if (maxValue && maxValue.isBefore(value)) {
        return maxValue;
    }
 
    return value;
}
 
export function mergeDateTime(date: DateTime, time: DateTime) {
    return date
        .set('hours', time.hour())
        .set('minutes', time.minute())
        .set('seconds', time.second());
}
 
export function formatDateTime(date: DateTime, format: string, timezone: string, lang: string) {
    return dateTime({input: date, timeZone: timezone, lang}).format(format);
}