All files / src/components/RelativeRangeDatePicker/components/Presets utils.ts

47.05% Statements 24/51
56.6% Branches 30/53
71.42% Functions 5/7
47.05% Lines 24/51

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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 1655x                           5x   5x                   5x                               4x 4x   4x 17x 4x                                                               11x 23x 23x   23x 23x   23x 11x     12x       12x       12x                                                                                                             5x 10x 10x 8x         10x      
import {dateTimeParse} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
 
import type {ExtractFunctionType} from '../../../types';
 
import {
    DEFAULT_DATE_PRESETS,
    DEFAULT_OTHERS_PRESETS,
    DEFAULT_TIME_PRESETS,
    allPresets,
} from './defaultPresets';
import type {Preset} from './defaultPresets';
import {i18n} from './i18n';
 
const lastRe = /^now-(\d+)([smhdwMy])$/;
 
const oneUnit = {
    s: 'Last second',
    m: 'Last minute',
    h: 'Last hour',
    d: 'Last day',
    w: 'Last week',
    M: 'Last month',
    y: 'Last year',
} as const;
 
const countUnit = {
    s: 'Last {count} second',
    m: 'Last {count} minute',
    h: 'Last {count} hour',
    d: 'Last {count} day',
    w: 'Last {count} week',
    M: 'Last {count} month',
    y: 'Last {count} year',
} as const;
 
export function getPresetTitle(
    start: string | null,
    end: string | null,
    presets: Preset[] = allPresets,
    t: ExtractFunctionType<typeof i18n> = i18n,
): string {
    const startText = start?.replace(/\s+/g, '') ?? start;
    const endText = end?.replace(/\s+/g, '') ?? end;
 
    for (const preset of presets) {
        if (preset.from === startText && preset.to === endText) {
            return t(preset.title as any);
        }
    }
    if (!startText) {
        return `${t('To')}: ${endText}`;
    }
    if (!endText) {
        return `${t('From')}: ${startText}`;
    }
 
    if (endText === 'now') {
        const match = lastRe.exec(startText);
        if (match) {
            const [, count, unit] = match;
            if (isDateUnit(unit)) {
                const template = Number(count) === 1 ? oneUnit[unit] : countUnit[unit];
                return t(template, {count});
            }
        }
    }
 
    return startText + ' — ' + endText;
}
 
function isDateUnit(value: string): value is 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y' {
    return ['s', 'm', 'h', 'd', 'w', 'M', 'y'].includes(value);
}
 
export function filterPresetList(
    presets: Preset[],
    {minValue, allowNullableValues}: {minValue?: DateTime; allowNullableValues?: boolean} = {},
) {
    return presets.filter((preset) => {
        const from = preset.from ? dateTimeParse(preset.from) : undefined;
        const to = preset.to ? dateTimeParse(preset.to, {roundUp: true}) : undefined;
 
        const hasNullableFrom = allowNullableValues && preset.from === null;
        const hasNullableTo = allowNullableValues && preset.to === null;
 
        if ((!from && !hasNullableFrom) || (!to && !hasNullableTo)) {
            return false;
        }
 
        Iif (to?.isBefore(from)) {
            return false;
        }
 
        Iif (minValue && from?.isBefore(minValue)) {
            return false;
        }
 
        return true;
    });
}
 
export interface PresetTab {
    id: string;
    title: string;
    presets: Preset[];
}
 
export function getDefaultPresetTabs({
    withTime,
    minValue,
    allowNullableValues,
    t = i18n,
}: {
    minValue?: DateTime;
    withTime?: boolean;
    allowNullableValues?: boolean;
    t?: (key: 'Main' | 'Other') => string;
}) {
    const tabs: PresetTab[] = [];
 
    const mainTab: PresetTab = {
        id: 'main',
        title: t('Main'),
        presets: [],
    };
    const mainPresets = DEFAULT_DATE_PRESETS;
    if (withTime) {
        mainPresets.unshift(...DEFAULT_TIME_PRESETS);
    }
    mainTab.presets = filterPresetList(mainPresets, {minValue, allowNullableValues});
 
    if (mainTab.presets.length > 0) {
        tabs.push(mainTab);
    }
 
    const otherTab: PresetTab = {
        id: 'other',
        title: t('Other'),
        presets: filterPresetList(DEFAULT_OTHERS_PRESETS, {minValue, allowNullableValues}),
    };
 
    if (otherTab.presets.length > 0) {
        tabs.push(otherTab);
    }
 
    return tabs;
}
 
export function filterPresetTabs(
    tabs: PresetTab[],
    {minValue, allowNullableValues}: {minValue?: DateTime; allowNullableValues?: boolean} = {},
) {
    return tabs.reduce<PresetTab[]>((acc, tab) => {
        const presets = filterPresetList(tab.presets, {minValue, allowNullableValues});
        if (presets.length) {
            acc.push({
                ...tab,
                presets,
            });
        }
        return acc;
    }, []);
}