All files / src/components/RelativeRangeDatePicker utils.ts

70% Statements 28/40
65.45% Branches 36/55
40% Functions 2/5
70% Lines 28/40

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 1115x                                                                                 5x 10x 5x   5x   1x   4x 4x 4x 4x   4x 2x   4x                       21x 11x     10x   21x 21x 10x         10x 10x 5x               10x 10x 4x     6x                 6x   6x    
import {dateTime, dateTimeParse} from '@gravity-ui/date-utils';
 
import type {Value} from '../RelativeDatePicker';
import type {ExtractFunctionType, RangeValue} from '../types';
 
import type {Preset} from './components/Presets/defaultPresets';
import {i18n} from './components/Presets/i18n';
import {getPresetTitle} from './components/Presets/utils';
 
function resolveTimeZone(timeZone: string) {
    if (timeZone === 'default' || timeZone === 'system') {
        return dateTime({timeZone}).timeZone();
    }
 
    return timeZone;
}
 
export function normalizeTimeZone(timeZone: string) {
    const lowered = timeZone.toLowerCase();
    if (lowered === 'default' || lowered === 'system') {
        return lowered;
    }
 
    return resolveTimeZone(timeZone);
}
 
export function getTimeZoneOffset(timeZone: string) {
    return `UTC ${dateTime({timeZone}).format('Z')}`;
}
 
interface GetDefaultTitleArgs {
    value: RangeValue<Value | null> | null;
    timeZone: string;
    alwaysShowAsAbsolute?: boolean;
    allowNullableValues?: boolean;
    format?: string;
    presets?: Preset[];
    presetsTranslations?: ExtractFunctionType<typeof i18n>;
    lang?: string;
}
 
const isPresetValue = (value: RangeValue<Value | null> | null, allowNullableValues?: boolean) => {
    if (!value || value.start?.type === 'absolute' || value.end?.type === 'absolute') {
        return null;
    }
    if (!allowNullableValues && (value.start === null || value.end === null)) {
        // we can't get here with no nullable values allowed but just in case...
        return null;
    }
    let start = null;
    let end = null;
    Eif (value.start?.type === 'relative') {
        start = value.start.value;
    }
    if (value.end?.type === 'relative') {
        end = value.end.value;
    }
    return {start, end};
};
export function getDefaultTitle({
    value,
    timeZone,
    alwaysShowAsAbsolute,
    allowNullableValues,
    format = 'L',
    presets,
    presetsTranslations = i18n,
    lang = 'en',
}: GetDefaultTitleArgs): string {
    if (!value) {
        return '';
    }
 
    const tz = timeZone === 'default' ? '' : ` (${getTimeZoneOffset(timeZone)})`;
 
    let from = '';
    if (value.start) {
        from =
            value.start.type === 'relative' && !alwaysShowAsAbsolute
                ? value.start.value
                : (dateTimeParse(value.start.value, {timeZone, lang})?.format(format) ?? '');
    }
    let to = '';
    if (value.end) {
        to =
            value.end.type === 'relative' && !alwaysShowAsAbsolute
                ? value.end.value
                : (dateTimeParse(value.end.value, {timeZone, roundUp: true, lang})?.format(
                      format,
                  ) ?? '');
    }
 
    const presetSearch = isPresetValue(value, allowNullableValues);
    if (!alwaysShowAsAbsolute && presetSearch) {
        return getPresetTitle(presetSearch.start, presetSearch.end, presets, presetsTranslations);
    }
 
    Iif (allowNullableValues) {
        if (!from) {
            return `${presetsTranslations('To')}: ${to}${tz}`;
        }
        if (!to) {
            return `${presetsTranslations('From')}: ${from}${tz}`;
        }
    }
 
    const delimiter = ' — ';
 
    return `${from}${delimiter}${to}${tz}`;
}