Skip to content

Commit

Permalink
refactor(kit): remove DateRange-specific logic from common date-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Feb 6, 2024
1 parent 54e7ec9 commit 97dc5a1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
9 changes: 0 additions & 9 deletions projects/kit/src/lib/constants/possible-dates-separator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
import {CHAR_EM_DASH, CHAR_EN_DASH, CHAR_HYPHEN, CHAR_MINUS} from './unicode-characters';

export const POSSIBLE_DATE_RANGE_SEPARATOR = [
CHAR_HYPHEN,
CHAR_EN_DASH,
CHAR_EM_DASH,
CHAR_MINUS,
];

export const POSSIBLE_DATE_TIME_SEPARATOR = [',', ' '];
15 changes: 15 additions & 0 deletions projects/kit/src/lib/masks/date-range/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
CHAR_EM_DASH,
CHAR_EN_DASH,
CHAR_HYPHEN,
CHAR_JP_HYPHEN,
CHAR_MINUS,
} from '../../constants';

export const POSSIBLE_DATE_RANGE_SEPARATOR = [
CHAR_HYPHEN,
CHAR_EN_DASH,
CHAR_EM_DASH,
CHAR_MINUS,
CHAR_JP_HYPHEN,
];
2 changes: 2 additions & 0 deletions projects/kit/src/lib/masks/date-range/date-range-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../processors';
import {MaskitoDateMode, MaskitoDateSegments} from '../../types';
import {createMinMaxRangeLengthPostprocessor} from './processors/min-max-range-length-postprocessor';
import {createPseudoRangeSeparatorPreprocessor} from './processors/preudo-range-separator-preprocessor';
import {createSwapDatesPostprocessor} from './processors/swap-dates-postprocessor';

export function maskitoDateRangeOptionsGenerator({
Expand Down Expand Up @@ -38,6 +39,7 @@ export function maskitoDateRangeOptionsGenerator({
mask: [...dateMask, ...Array.from(rangeSeparator), ...dateMask],
overwriteMode: 'replace',
preprocessors: [
createPseudoRangeSeparatorPreprocessor(rangeSeparator),
createZeroPlaceholdersPreprocessor(),
normalizeDatePreprocessor({
dateModeTemplate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {MaskitoPreprocessor} from '@maskito/core';

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',
);

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

return {
elementState: {
selection,
value: value.replace(pseudoSeparatorsRegExp, rangeSeparator),
},
data: data.replace(pseudoSeparatorsRegExp, rangeSeparator),
};
};
}
31 changes: 31 additions & 0 deletions projects/kit/src/lib/masks/date/tests/date-mask.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskito/core';
import {maskitoDateOptionsGenerator} from '@maskito/kit';

/**
* If any of these tests fail,
* it can mean that browser autofill or composition are not working properly
* for Date mask
*/
describe('Date (maskitoTransform)', () => {
describe('[mode]="yyyy/mm/dd"', () => {
let options: MaskitoOptions = MASKITO_DEFAULT_OPTIONS;

beforeEach(() => {
options = maskitoDateOptionsGenerator({
mode: 'yyyy/mm/dd',
separator: '/',
});
});

// TODO: fix this bug later
xit('pads digit > 1 with zero for months (12345 => 1234/05)', () => {
expect(maskitoTransform('12345', options)).toBe('1234/05');
});

// TODO: https://github.com/taiga-family/maskito/pull/907
xit('accepts full width characters', () => {
expect(maskitoTransform('12345', options)).toBe('1234/05');
expect(maskitoTransform('12341226', options)).toBe('1234/12/26');
});
});
});
5 changes: 0 additions & 5 deletions projects/kit/src/lib/processors/valid-date-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {POSSIBLE_DATE_RANGE_SEPARATOR} from '../constants';
import {escapeRegExp, parseDateRangeString, validateDateString} from '../utils';

export function createValidDatePreprocessor({
Expand All @@ -22,10 +21,6 @@ export function createValidDatePreprocessor({
};
}

if (POSSIBLE_DATE_RANGE_SEPARATOR.includes(data)) {
return {elementState, data: rangeSeparator};
}

const newCharacters = data.replace(
new RegExp(
`[^\\d${escapeRegExp(dateSegmentsSeparator)}${rangeSeparator}]`,
Expand Down

0 comments on commit 97dc5a1

Please sign in to comment.