Skip to content

Commit

Permalink
fix(kit): DateRange should accept single character date segment pas…
Browse files Browse the repository at this point in the history
…te even if date and range separators are equal (#1796)
  • Loading branch information
AndreiBelokopytov authored Nov 5, 2024
1 parent 1811223 commit be6a4c3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
74 changes: 30 additions & 44 deletions projects/kit/src/lib/processors/normalize-date-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,39 @@ export function normalizeDatePreprocessor({
dateTimeSeparator?: string;
}): MaskitoPreprocessor {
return ({elementState, data}) => {
const separator = rangeSeparator
? new RegExp(`${rangeSeparator}|-`)
: dateTimeSeparator;
const possibleDates = data.split(separator);
const dates = data.includes(dateTimeSeparator)
? [possibleDates[0] ?? '']
: possibleDates;
const templateSegments = dateModeTemplate.split(dateSegmentsSeparator);
const includesTime = data.includes(dateTimeSeparator);
const dateSegments = data
.slice(0, includesTime ? data.indexOf(dateTimeSeparator) : Infinity)
.split(/\D/)
.filter(Boolean);

if (
dates.every(
(date) =>
date.trim().split(/\D/).filter(Boolean).length ===
dateModeTemplate.split(dateSegmentsSeparator).length,
)
) {
const newData = dates
.map((date) =>
normalizeDateString(date, dateModeTemplate, dateSegmentsSeparator),
)
.join(rangeSeparator);

return {
elementState,
data: `${newData}${
data.includes(dateTimeSeparator)
? dateTimeSeparator + possibleDates[1] || ''
: ''
}`,
};
if (!dateSegments.length || dateSegments.length % templateSegments.length !== 0) {
return {elementState, data};
}

return {elementState, data};
};
}
const dates = dateSegments.reduce<string[]>((dates, segment, index) => {
const template = templateSegments[index % templateSegments.length] ?? '';
const dateIndex = Math.trunc(index / templateSegments.length);
const isLastDateSegment =
index % templateSegments.length === templateSegments.length - 1;

function normalizeDateString(
dateString: string,
template: string,
separator: string,
): string {
const dateSegments = dateString.split(/\D/).filter(Boolean);
const templateSegments = template.split(separator);
const normalizedSegments = dateSegments.map((segment, index) =>
index === templateSegments.length - 1
? segment
: segment.padStart(templateSegments[index]?.length ?? 0, '0'),
);
if (!dates[dateIndex]) {
dates[dateIndex] = '';
}

return normalizedSegments.join(separator);
dates[dateIndex] += isLastDateSegment
? segment
: `${segment.padStart(template.length, '0')}${dateSegmentsSeparator}`;

return dates;
}, []);

return {
elementState,
data: includesTime
? `${dates[0]}${data.slice(data.indexOf(dateTimeSeparator))}`
: dates.join(rangeSeparator),
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ describe('normalizeDatePreprocessor', () => {
it('empty input => 06.2.2023-07.2.2023', () => {
check('06.2.2023-07.2.2023', '06.02.2023 – 07.02.2023');
});

it('empty input => 06-2-2023 - 07-2-2023', () => {
check('06-2-2023-07-2-2023', '06.02.2023 – 07.02.2023');
});

it('empty input => 06-2-2023-07-2-2023', () => {
check('06-2-2023-07-2-2023', '06.02.2023 – 07.02.2023');
});
});

describe('Input-date long mode', () => {
Expand Down Expand Up @@ -90,6 +98,14 @@ describe('normalizeDatePreprocessor', () => {
it('empty input => 6.2.2023', () => {
check('6.2.2022, 15', '06.02.2022, 15');
});

it('empty input => 6.2.2023, 12:01.001', () => {
check('6.2.2023, 12:01.001', '06.02.2023, 12:01.001');
});

it('empty input => 6.2.2023, 01.001', () => {
check('6.2.2023, 01.001', '06.02.2023, 01.001');
});
});
});

Expand Down

0 comments on commit be6a4c3

Please sign in to comment.