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.
refactor(kit): move some
DateRange
-specific logic from common date-…
…utils to new preprocessor (#1022)
- Loading branch information
1 parent
eab31b2
commit f8bd925
Showing
8 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
projects/demo-integrations/src/tests/kit/date-range/date-range-separator.cy.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
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 |
---|---|---|
@@ -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 = [',', ' ']; |
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,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, | ||
]; |
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
32 changes: 32 additions & 0 deletions
32
projects/kit/src/lib/masks/date-range/processors/pseudo-range-separator-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,32 @@ | ||
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, | ||
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(pseudoRangeSeparatorsRE, rangeSeparator), | ||
}, | ||
data: data.replace(pseudoRangeSeparatorsRE, rangeSeparator), | ||
}; | ||
}; | ||
} |
43 changes: 43 additions & 0 deletions
43
projects/kit/src/lib/masks/date-range/tests/pseudo-range-separators.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,43 @@ | ||
import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskito/core'; | ||
import {maskitoDateRangeOptionsGenerator} from '@maskito/kit'; | ||
|
||
import {CHAR_EM_DASH, CHAR_EN_DASH, CHAR_HYPHEN, CHAR_MINUS} from '../../../constants'; | ||
|
||
describe('DateRange (maskitoTransform) | Pseudo range separators', () => { | ||
let options: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; | ||
|
||
beforeEach(() => { | ||
options = maskitoDateRangeOptionsGenerator({ | ||
mode: 'dd/mm/yyyy', | ||
dateSeparator: '.', | ||
rangeSeparator: CHAR_EN_DASH, | ||
}); | ||
}); | ||
|
||
it('works with already valid range separator', () => { | ||
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', () => { | ||
expect(maskitoTransform(`01012000${CHAR_HYPHEN}10102000`, options)).toBe( | ||
`01.01.2000${CHAR_EN_DASH}10.10.2000`, | ||
); | ||
}); | ||
|
||
it('replaces em-dash with valid range separator', () => { | ||
expect(maskitoTransform(`01012000${CHAR_EM_DASH}10102000`, options)).toBe( | ||
`01.01.2000${CHAR_EN_DASH}10.10.2000`, | ||
); | ||
}); | ||
|
||
it('replaces minus with valid range separator', () => { | ||
expect(maskitoTransform(`01012000${CHAR_MINUS}10102000`, options)).toBe( | ||
`01.01.2000${CHAR_EN_DASH}10.10.2000`, | ||
); | ||
}); | ||
}); |
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,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'); | ||
}); | ||
}); | ||
}); |
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