Skip to content

Commit

Permalink
chore: improve createPseudoRangeSeparatorPreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Feb 6, 2024
1 parent 4a5107b commit 9c652d6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DemoPath} from '@demo/constants';

describe('DateRange | Separator', () => {
describe('DateRange | dateSeparator', () => {
describe('/', () => {
beforeEach(() => {
cy.visit(`/${DemoPath.DateRange}/API?dateSeparator=/`);
Expand Down
2 changes: 1 addition & 1 deletion projects/kit/src/lib/masks/date-range/date-range-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function maskitoDateRangeOptionsGenerator({
mask: [...dateMask, ...Array.from(rangeSeparator), ...dateMask],
overwriteMode: 'replace',
preprocessors: [
createPseudoRangeSeparatorPreprocessor(rangeSeparator),
createPseudoRangeSeparatorPreprocessor({rangeSeparator, dateSeparator}),
createZeroPlaceholdersPreprocessor(),
normalizeDatePreprocessor({
dateModeTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import {POSSIBLE_DATE_RANGE_SEPARATOR} from '../constants';
* It replaces pseudo range separators with valid one.
* @example User types hyphen / en-dash / em-dash / minus => it is replaced with valid range separator.
*/
export function createPseudoRangeSeparatorPreprocessor(
rangeSeparator: string,
): MaskitoPreprocessor {
const pseudoSeparatorsRegExp = new RegExp(
`[${POSSIBLE_DATE_RANGE_SEPARATOR.join('')}]`,
'gi',
);
export function createPseudoRangeSeparatorPreprocessor({
rangeSeparator,
dateSeparator,
}: {
rangeSeparator: string;
dateSeparator: string;
}): MaskitoPreprocessor {
const pseudoRangeSeparators = POSSIBLE_DATE_RANGE_SEPARATOR.filter(
x => !rangeSeparator.includes(x) && x !== dateSeparator,
).join('');
const pseudoRangeSeparatorsRE = new RegExp(`[${pseudoRangeSeparators}]`, 'gi');

return ({elementState, data}) => {
const {value, selection} = elementState;

return {
elementState: {
selection,
value: value.replace(pseudoSeparatorsRegExp, rangeSeparator),
value: value.replace(pseudoRangeSeparatorsRE, rangeSeparator),
},
data: data.replace(pseudoSeparatorsRegExp, rangeSeparator),
data: data.replace(pseudoRangeSeparatorsRE, rangeSeparator),
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('DateRange (maskitoTransform) | Pseudo range separators', () => {
expect(maskitoTransform(`01012000${CHAR_EN_DASH}10102000`, options)).toBe(
`01.01.2000${CHAR_EN_DASH}10.10.2000`,
);
expect(maskitoTransform(`01012000 ${CHAR_EN_DASH} 10102000`, options)).toBe(
`01.01.2000${CHAR_EN_DASH}10.10.2000`,
);
});

it('replaces hyphen with valid range separator', () => {
Expand Down
8 changes: 3 additions & 5 deletions projects/kit/src/lib/processors/valid-date-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export function createValidDatePreprocessor({
const {validatedDateString, updatedSelection} = validateDateString({
dateString,
dateModeTemplate,
offset: validatedValue
? validatedValue.length + rangeSeparator.length
: 0,
offset: validatedValue.length,
selection: [from, to],
});

Expand All @@ -63,8 +61,8 @@ export function createValidDatePreprocessor({
to = updatedSelection[1];

validatedValue +=
hasRangeSeparator && validatedValue
? rangeSeparator + validatedDateString
hasRangeSeparator && !validatedValue
? validatedDateString + rangeSeparator
: validatedDateString;
}

Expand Down

0 comments on commit 9c652d6

Please sign in to comment.