-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): dayPeriod pattern support (#12354)
* feat(core): add dayPeriod pattern support * refactor(core, docs): update Date/timePicker examples, optimized translateDayPeriod pipe performance, managed adapter format method to handle dayPeriod pattern etc... * fix(core): simplified pipe if conditional check and updated time picker format example comment and updated dayPeriod strings * fix(core): remove dayperiod strings from fr lang * fix(core): handle invalid date inputs gracefully * fix(docs): updated dateTime example with increased width of picker input * fix(docs): timepicker format test * fix(docs): dateTimePicker test --------- Co-authored-by: deno <[email protected]> Co-authored-by: Mike O'Donnell <[email protected]>
- Loading branch information
1 parent
08ef8f5
commit c70f83a
Showing
22 changed files
with
365 additions
and
47 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
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,68 @@ | ||
/** | ||
* Converts a given time string to a desired format. | ||
* | ||
* @param {string} inputStr - The input time string to be converted. | ||
* @returns {string} - The converted time string in the desired format. | ||
*/ | ||
export function convertToDesiredFormat(inputStr: string): string { | ||
const dayPeriodMapping = { | ||
morning: 0, | ||
afternoon: 12, | ||
evening: 12, | ||
night: 0 | ||
}; | ||
|
||
const dayPeriodPattern = /\b(morning|afternoon|evening|night)\b/; | ||
const amPmPattern = /\b(AM|PM)\b/i; | ||
|
||
const convertDayPeriod = (input: string): string => { | ||
const match = input.match(dayPeriodPattern); | ||
if (match) { | ||
const period = match[0].toLowerCase(); | ||
const timeShift = dayPeriodMapping[period]; | ||
input = input.replace(dayPeriodPattern, '').trim(); | ||
const timeMatch = input.match(/\d{1,2}:\d{2}/); | ||
if (timeMatch) { | ||
const [hoursStr, minutesStr] = timeMatch[0].split(':'); | ||
let hours = Number(hoursStr); | ||
const minutes = Number(minutesStr); | ||
hours = (hours % 12) + timeShift; | ||
input = input.replace( | ||
timeMatch[0], | ||
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}` | ||
); | ||
} | ||
} | ||
return input; | ||
}; | ||
|
||
const convertAmPm = (input: string): string => { | ||
const match = input.match(amPmPattern); | ||
if (match) { | ||
const period = match[0].toUpperCase(); | ||
input = input.replace(amPmPattern, '').trim(); | ||
const timeMatch = input.match(/\d{1,2}:\d{2}/); | ||
if (timeMatch) { | ||
const [hoursStr, minutesStr] = timeMatch[0].split(':'); | ||
let hours = Number(hoursStr); | ||
const minutes = Number(minutesStr); | ||
if (period === 'PM' && hours < 12) { | ||
hours += 12; | ||
} | ||
if (period === 'AM' && hours === 12) { | ||
hours = 0; | ||
} | ||
input = input.replace( | ||
timeMatch[0], | ||
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}` | ||
); | ||
} | ||
} | ||
return input; | ||
}; | ||
|
||
inputStr = convertDayPeriod(inputStr); | ||
inputStr = convertAmPm(inputStr); | ||
|
||
return inputStr.replace(/\bat\b/i, '').trim(); | ||
} |
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
Oops, something went wrong.