( locale: string, refParts: DatetimeParts, hourCycle: DatetimeHourCycle = 'h12', minParts?: DatetimeParts, maxParts?: DatetimeParts, hourValues?: number[], minuteValues?: number[] )
| 200 | * hour and minute values according to the bounds and locale. |
| 201 | */ |
| 202 | export const generateTime = ( |
| 203 | locale: string, |
| 204 | refParts: DatetimeParts, |
| 205 | hourCycle: DatetimeHourCycle = 'h12', |
| 206 | minParts?: DatetimeParts, |
| 207 | maxParts?: DatetimeParts, |
| 208 | hourValues?: number[], |
| 209 | minuteValues?: number[] |
| 210 | ) => { |
| 211 | const computedHourCycle = getHourCycle(locale, hourCycle); |
| 212 | const use24Hour = is24Hour(computedHourCycle); |
| 213 | let processedHours = getHourData(computedHourCycle); |
| 214 | let processedMinutes = minutes; |
| 215 | let isAMAllowed = true; |
| 216 | let isPMAllowed = true; |
| 217 | |
| 218 | if (hourValues) { |
| 219 | processedHours = processedHours.filter((hour) => hourValues.includes(hour)); |
| 220 | } |
| 221 | |
| 222 | if (minuteValues) { |
| 223 | processedMinutes = processedMinutes.filter((minute) => minuteValues.includes(minute)); |
| 224 | } |
| 225 | |
| 226 | if (minParts) { |
| 227 | /** |
| 228 | * If ref day is the same as the |
| 229 | * minimum allowed day, filter hour/minute |
| 230 | * values according to min hour and minute. |
| 231 | */ |
| 232 | if (isSameDay(refParts, minParts)) { |
| 233 | /** |
| 234 | * Users may not always set the hour/minute for |
| 235 | * min value (i.e. 2021-06-02) so we should allow |
| 236 | * all hours/minutes in that case. |
| 237 | */ |
| 238 | if (minParts.hour !== undefined) { |
| 239 | processedHours = processedHours.filter((hour) => { |
| 240 | const convertedHour = refParts.ampm === 'pm' ? (hour + 12) % 24 : hour; |
| 241 | return (use24Hour ? hour : convertedHour) >= minParts.hour!; |
| 242 | }); |
| 243 | isAMAllowed = minParts.hour < 13; |
| 244 | } |
| 245 | if (minParts.minute !== undefined) { |
| 246 | /** |
| 247 | * The minimum minute range should not be enforced when |
| 248 | * the hour is greater than the min hour. |
| 249 | * |
| 250 | * For example with a minimum range of 09:30, users |
| 251 | * should be able to select 10:00-10:29 and beyond. |
| 252 | */ |
| 253 | let isPastMinHour = false; |
| 254 | if (minParts.hour !== undefined && refParts.hour !== undefined) { |
| 255 | if (refParts.hour > minParts.hour) { |
| 256 | isPastMinHour = true; |
| 257 | } |
| 258 | } |
| 259 |
no test coverage detected