generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kit):
Date
accept single character date segment during paste (#610
- Loading branch information
1 parent
77a81d1
commit e493198
Showing
6 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
projects/kit/src/lib/processors/normalize-date-preprocessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {MaskitoPreprocessor} from '@maskito/core'; | ||
|
||
import {DATE_TIME_SEPARATOR} from '../masks/date-time/constants'; | ||
|
||
export function normalizeDatePreprocessor({ | ||
dateModeTemplate, | ||
dateSegmentsSeparator, | ||
rangeSeparator = '', | ||
}: { | ||
dateModeTemplate: string; | ||
dateSegmentsSeparator: string; | ||
rangeSeparator?: string; | ||
}): MaskitoPreprocessor { | ||
return ({elementState, data}) => { | ||
const separator = rangeSeparator | ||
? new RegExp(`${rangeSeparator}|-`) | ||
: DATE_TIME_SEPARATOR; | ||
const possibleDates = data.split(separator); | ||
|
||
const dates = data.includes(DATE_TIME_SEPARATOR) | ||
? [possibleDates[0]] | ||
: possibleDates; | ||
|
||
if ( | ||
dates.every( | ||
date => | ||
date.trim().split(/\D/).length === | ||
dateModeTemplate.split(dateSegmentsSeparator).length, | ||
) | ||
) { | ||
const newData = dates | ||
.map(date => | ||
normalizeDateString(date, dateModeTemplate, dateSegmentsSeparator), | ||
) | ||
.join(rangeSeparator); | ||
|
||
return { | ||
elementState, | ||
data: `${newData}${ | ||
data.includes(DATE_TIME_SEPARATOR) | ||
? DATE_TIME_SEPARATOR + possibleDates[1] || '' | ||
: '' | ||
}`, | ||
}; | ||
} | ||
|
||
return {elementState, data}; | ||
}; | ||
} | ||
|
||
function normalizeDateString( | ||
dateString: string, | ||
template: string, | ||
separator: string, | ||
): string { | ||
const dateSegments = dateString.split(/\D/); | ||
const templateSegments = template.split(separator); | ||
const normalizedSegments = dateSegments.map((segment, index) => | ||
index === templateSegments.length - 1 | ||
? segment | ||
: segment.padStart(templateSegments[index].length, '0'), | ||
); | ||
|
||
return normalizedSegments.join(separator); | ||
} |
108 changes: 108 additions & 0 deletions
108
projects/kit/src/lib/processors/tests/normalize-date-preprocessor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import {MaskitoPreprocessor} from '@maskito/core'; | ||
|
||
import {normalizeDatePreprocessor} from '../normalize-date-preprocessor'; | ||
|
||
describe('normalizeDatePreprocessor', () => { | ||
describe('Input-date-range', () => { | ||
const preprocessor = normalizeDatePreprocessor({ | ||
dateModeTemplate: 'dd.mm.yyyy', | ||
dateSegmentsSeparator: '.', | ||
rangeSeparator: ' – ', | ||
}); | ||
|
||
const check = getCheckFunction(preprocessor); | ||
|
||
it('Empty input => 6.2.2023 – 7.2.2023', () => { | ||
check('6.2.2023 – 7.2.2023', '06.02.2023 – 07.02.2023'); | ||
}); | ||
|
||
it('Empty input => 6.2.2023 – 7.2.2023 (basic spaces)', () => { | ||
check('6.2.2023 – 7.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', () => { | ||
const preprocessor = normalizeDatePreprocessor({ | ||
dateModeTemplate: 'dd.mm.yyyy', | ||
dateSegmentsSeparator: '.', | ||
}); | ||
|
||
const check = getCheckFunction(preprocessor); | ||
|
||
it('Empty input => 6.2.2023', () => { | ||
check('6.2.2023', '06.02.2023'); | ||
}); | ||
|
||
it('Empty input => 06.2.2023', () => { | ||
check('06.2.2023', '06.02.2023'); | ||
}); | ||
|
||
it('Empty input => 06.2.20', () => { | ||
check('06.2.20', '06.02.20'); | ||
}); | ||
}); | ||
|
||
describe('input-date short mode', () => { | ||
const preprocessor = normalizeDatePreprocessor({ | ||
dateModeTemplate: 'mm/yy', | ||
dateSegmentsSeparator: '/', | ||
}); | ||
|
||
const check = getCheckFunction(preprocessor); | ||
|
||
it('Empty input => 2/2/22', () => { | ||
check('2/2', '02/2'); | ||
}); | ||
|
||
it('Empty input => 1.1', () => { | ||
check('1.1', '01/1'); | ||
}); | ||
|
||
it('Empty input => 3.12', () => { | ||
check('3.12', '03/12'); | ||
}); | ||
}); | ||
|
||
describe('input-date-time', () => { | ||
const preprocessor = normalizeDatePreprocessor({ | ||
dateModeTemplate: 'dd.mm.yyyy', | ||
dateSegmentsSeparator: '.', | ||
}); | ||
const check = getCheckFunction(preprocessor); | ||
|
||
it('Empty input => 6.2.2023, 12:00', () => { | ||
check('6.2.2023, 12:00', '06.02.2023, 12:00'); | ||
}); | ||
|
||
it('Empty input => 6.2.2023, 15', () => { | ||
check('6.2.2023, 15', '06.02.2023, 15'); | ||
}); | ||
|
||
it('Empty input => 06.2.2023', () => { | ||
check('06.2.2023', '06.02.2023'); | ||
}); | ||
|
||
it('Empty input => 6.2.2023', () => { | ||
check('6.2.2022, 15', '06.02.2022, 15'); | ||
}); | ||
}); | ||
}); | ||
|
||
function getCheckFunction( | ||
preprocessor: MaskitoPreprocessor, | ||
): (actual: string, expected: string) => void { | ||
return (insertedCharacters: string, expectedValue: string): void => { | ||
const EMPTY_INPUT = {value: '', selection: [0, 0] as [number, number]}; | ||
|
||
const {data} = preprocessor( | ||
{elementState: EMPTY_INPUT, data: insertedCharacters}, | ||
'insert', | ||
); | ||
|
||
expect(data).toEqual(expectedValue); | ||
}; | ||
} |