All files / src/components/RangeDateField/utils getRangeEditableSections.ts

100% Statements 23/23
90% Branches 9/10
100% Functions 1/1
100% Lines 22/22

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                          40x 40x   40x 40x 40x 40x   40x                           40x     40x 206x   206x 206x   206x 40x   166x     206x   206x 40x       40x 40x   40x     40x    
import type {DateTime} from '@gravity-ui/date-utils';
 
import type {DateFieldSectionWithoutPosition} from '../../DateField/types';
import {getEditableSections, isEditableSection, toEditableSection} from '../../DateField/utils';
import type {EDITABLE_SEGMENTS} from '../../DateField/utils';
import type {RangeValue} from '../../types';
 
export function getRangeEditableSections(
    sections: DateFieldSectionWithoutPosition[],
    value: RangeValue<DateTime>,
    validSegments: RangeValue<typeof EDITABLE_SEGMENTS>,
    delimiter: string,
) {
    const start = getEditableSections(sections, value.start, validSegments.start);
    const end = getEditableSections(sections, value.end, validSegments.end);
 
    const last = start[start.length - 1];
    let position = last.end;
    const previousEditableSection = last.nextEditableSection;
    const sectionsCount = start.length + 1;
 
    const delimiterSection = toEditableSection(
        {
            type: 'literal',
            contentType: 'letter',
            format: delimiter,
            placeholder: delimiter,
            hasLeadingZeros: false,
        },
        value.start,
        validSegments.start,
        position,
        previousEditableSection,
    );
 
    position += delimiterSection.textValue.length - 1;
 
    let nextEditableSection;
    for (let index = 0; index < end.length; index++) {
        const section = end[index];
 
        section.start += position;
        section.end += position;
 
        if (section.previousEditableSection === 0 && nextEditableSection === undefined) {
            section.previousEditableSection = previousEditableSection;
        } else {
            section.previousEditableSection += sectionsCount;
        }
 
        section.nextEditableSection += sectionsCount;
 
        if (isEditableSection(section) && nextEditableSection === undefined) {
            nextEditableSection = index + sectionsCount;
        }
    }
 
    Eif (nextEditableSection !== undefined) {
        delimiterSection.nextEditableSection = nextEditableSection;
 
        start[previousEditableSection].nextEditableSection = nextEditableSection;
    }
 
    return [...start, delimiterSection, ...end];
}