(
value: string | undefined,
defaultValue: string,
options: CalendarTimeValidationOptions = {}
)
| 11 | }; |
| 12 | |
| 13 | export function normalizeCalendarTimeValue( |
| 14 | value: string | undefined, |
| 15 | defaultValue: string, |
| 16 | options: CalendarTimeValidationOptions = {} |
| 17 | ): CalendarTimeValidationResult { |
| 18 | if (!value) { |
| 19 | return { value: defaultValue, isValid: true }; |
| 20 | } |
| 21 | |
| 22 | const match = /^(\d{2}):([0-5]\d)(?::([0-5]\d))?$/.exec(value); |
| 23 | if (!match) { |
| 24 | return { value: defaultValue, isValid: false }; |
| 25 | } |
| 26 | |
| 27 | const hours = Number(match[1]); |
| 28 | const minutes = Number(match[2]); |
| 29 | const seconds = Number(match[3] ?? "00"); |
| 30 | const maxHour = options.maxHour ?? 23; |
| 31 | |
| 32 | if (hours < 0 || hours > maxHour) { |
| 33 | return { value: defaultValue, isValid: false }; |
| 34 | } |
| 35 | |
| 36 | if ( |
| 37 | options.allowMaxHourOnlyAtZero && |
| 38 | hours === maxHour && |
| 39 | (minutes !== 0 || seconds !== 0) |
| 40 | ) { |
| 41 | return { value: defaultValue, isValid: false }; |
| 42 | } |
| 43 | |
| 44 | return { |
| 45 | value: `${match[1]}:${match[2]}:${match[3] ?? "00"}`, |
| 46 | isValid: true, |
| 47 | }; |
| 48 | } |
no outgoing calls
no test coverage detected