All files / src/components/utils dates.ts

84.21% Statements 16/19
70% Branches 14/20
100% Functions 7/7
84.21% Lines 16/19

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 6524x       2450x               75x       19x 19x               480x 293x     187x     187x       187x               3525x     3525x 46x     3479x       10x             7083x    
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());
}
 
interface PlaceholderValueOptions {
    placeholderValue?: DateTime;
    timeZone?: string;
}
export function createPlaceholderValue({placeholderValue, timeZone}: PlaceholderValueOptions) {
    return placeholderValue ?? dateTime({timeZone}).startOf('day');
}
 
export function createPlaceholderRangeValue({placeholderValue, timeZone}: PlaceholderValueOptions) {
    const date = createPlaceholderValue({placeholderValue, timeZone});
    return {start: date.startOf('day'), end: date.endOf('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);
}