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.
feat(kit):
DateRange
swaps dates if the 2nd date is less than the 1…
…st one (#212)
- Loading branch information
1 parent
394d5d9
commit 3efbb42
Showing
5 changed files
with
79 additions
and
8 deletions.
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
39 changes: 39 additions & 0 deletions
39
projects/kit/src/lib/masks/date-range/processors/swap-dates-postprocessor.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,39 @@ | ||
import {MaskitoOptions} from '@maskito/core'; | ||
|
||
import { | ||
isDateStringComplete, | ||
parseDateRangeString, | ||
parseDateString, | ||
segmentsToDate, | ||
} from '../../../utils'; | ||
|
||
export function createSwapDatesPostprocessor({ | ||
dateModeTemplate, | ||
datesSeparator, | ||
}: { | ||
dateModeTemplate: string; | ||
datesSeparator: string; | ||
}): NonNullable<MaskitoOptions['postprocessor']> { | ||
return ({value, selection}) => { | ||
const dateStrings = parseDateRangeString(value, dateModeTemplate, datesSeparator); | ||
const isDateRangeComplete = | ||
dateStrings.length === 2 && | ||
dateStrings.every(date => isDateStringComplete(date, dateModeTemplate)); | ||
const [from, to] = selection; | ||
const caretAtTheEnd = from >= value.length; | ||
const allValueSelected = from === 0 && to >= value.length; // dropping text inside with a pointer | ||
|
||
if (!(caretAtTheEnd || allValueSelected) || !isDateRangeComplete) { | ||
return {value, selection}; | ||
} | ||
|
||
const [fromDate, toDate] = dateStrings.map(dateString => | ||
segmentsToDate(parseDateString(dateString, dateModeTemplate)), | ||
); | ||
|
||
return { | ||
selection, | ||
value: fromDate > toDate ? dateStrings.reverse().join(datesSeparator) : value, | ||
}; | ||
}; | ||
} |