Debian Codegraph @

node/@patternfly/react-core/dist/js/components/TimePicker/TimePickerUtils.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.isWithinMinMax = exports.getSeconds = exports.getMinutes = exports.getHours = exports.validateTime = exports.parseTime = exports.makeTimeOptions = exports.pmSuffix = exports.amSuffix = void 0;
4 exports.amSuffix = ' AM';
5 exports.pmSuffix = ' PM';
6 const makeTimeOptions = (stepMinutes, hour12, delimiter, minTime, maxTime, includeSeconds) => {
7 const res = [];
8 const iter = new Date(new Date().setHours(0, 0, 0, 0));
9 const iterDay = iter.getDay();
10 while (iter.getDay() === iterDay) {
11 let hour = iter.getHours();
12 let suffix = exports.amSuffix;
13 if (hour12) {
14 if (hour === 0) {
15 hour = 12; // 12am
16 }
17 else if (hour >= 12) {
18 suffix = exports.pmSuffix;
19 }
20 if (hour > 12) {
21 hour %= 12;
22 }
23 }
24 hour = hour12 ? hour.toString() : hour.toString().padStart(2, '0');
25 const minutes = iter.getMinutes().toString().padStart(2, '0');
26 const timeOption = `${hour}${delimiter}${minutes}${hour12 ? suffix : ''}`;
27 // time option is valid if within min/max constraints
28 if ((0, exports.isWithinMinMax)(minTime, maxTime, timeOption, delimiter, includeSeconds)) {
29 res.push(timeOption);
30 }
31 iter.setMinutes(iter.getMinutes() + stepMinutes);
32 }
33 return res;
34 };
35 exports.makeTimeOptions = makeTimeOptions;
36 const parseTime = (time, timeRegex, delimiter, is12Hour, includeSeconds) => {
37 const date = new Date(time);
38 // if default time is a ISO 8601 formatted date string, we parse it to hh:mm(am/pm) format
39 if (!isNaN(date.getDate()) && (time instanceof Date || time.includes('T'))) {
40 const hours = is12Hour
41 ? `${date.getHours() > 12 ? date.getHours() - 12 : date.getHours()}`
42 : `${date.getHours()}`.padStart(2, '0');
43 const minutes = `${date.getMinutes()}`.padStart(2, '0');
44 const seconds = includeSeconds ? `${date.getSeconds()}`.padStart(2, '0') : '';
45 const secondsWithDelimiter = seconds ? `${delimiter}${seconds}` : '';
46 let ampm = '';
47 if (is12Hour && date.getHours() > 11) {
48 ampm = exports.pmSuffix;
49 }
50 else if (is12Hour) {
51 ampm = exports.amSuffix;
52 }
53 return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
54 }
55 else if (typeof time === 'string') {
56 time = time.trim();
57 if (time !== '' && (0, exports.validateTime)(time, timeRegex, delimiter, is12Hour)) {
58 const [, hours, minutes, seconds, suffix = ''] = timeRegex.exec(time);
59 const secondsWithDelimiter = includeSeconds ? `${delimiter}${seconds !== null && seconds !== void 0 ? seconds : '00'}` : '';
60 let ampm = '';
61 // Format AM/PM according to design
62 if (is12Hour) {
63 const uppercaseSuffix = suffix.toUpperCase();
64 if (uppercaseSuffix === exports.amSuffix.toUpperCase().trim()) {
65 ampm = exports.amSuffix;
66 }
67 else if (uppercaseSuffix === exports.pmSuffix.toUpperCase().trim()) {
68 ampm = exports.pmSuffix;
69 }
70 else {
71 // if this 12 hour time is missing am/pm but otherwise valid,
72 // append am/pm depending on time of day
73 ampm = new Date().getHours() > 11 ? exports.pmSuffix : exports.amSuffix;
74 }
75 }
76 return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
77 }
78 }
79 return time.toString();
80 };
81 exports.parseTime = parseTime;
82 const validateTime = (time, timeRegex, delimiter, is12Hour) => {
83 // ISO 8601 format is valid
84 const date = new Date(time);
85 if (!isNaN(date.getDate()) && time.includes('T')) {
86 return true;
87 }
88 // hours only valid if they are [0-23] or [1-12]
89 const hours = parseInt(time.split(delimiter)[0]);
90 const validHours = hours >= (is12Hour ? 1 : 0) && hours <= (is12Hour ? 12 : 23);
91 // minutes verified by timeRegex
92 // empty string is valid
93 return time === '' || (timeRegex.test(time) && validHours);
94 };
95 exports.validateTime = validateTime;
96 const getHours = (time, timeRegex) => {
97 const parts = time.match(timeRegex);
98 if (parts && parts.length) {
99 if (/pm/i.test(parts[4])) {
100 return parseInt(parts[1]) === 12 ? parseInt(parts[1]) : parseInt(parts[1]) + 12;
101 }
102 if (/am/i.test(parts[4])) {
103 return parseInt(parts[1]) === 12 ? 0 : parseInt(parts[1]);
104 }
105 return parseInt(parts[1]);
106 }
107 return null;
108 };
109 exports.getHours = getHours;
110 const getMinutes = (time, timeRegex) => {
111 const parts = time.match(timeRegex);
112 return parts && parts.length ? parseInt(parts[2]) : null;
113 };
114 exports.getMinutes = getMinutes;
115 const getSeconds = (time, timeRegex) => {
116 var _a;
117 const seconds = (_a = time.match(timeRegex)) === null || _a === void 0 ? void 0 : _a[3];
118 return seconds ? parseInt(seconds) : null;
119 };
120 exports.getSeconds = getSeconds;
121 const isWithinMinMax = (minTime, maxTime, time, delimiter, includeSeconds) => {
122 // do not throw error if empty string
123 if (time.trim() === '') {
124 return true;
125 }
126 // correctly format as 24hr times (12:30AM => 00:30, 1:15 => 01:15)
127 const min24HourTime = convertTo24Hour(minTime, delimiter, includeSeconds);
128 const selected24HourTime = convertTo24Hour(time, delimiter, includeSeconds);
129 const max24HourTime = convertTo24Hour(maxTime, delimiter, includeSeconds);
130 // simple string comparison for 24hr times
131 return min24HourTime <= selected24HourTime && selected24HourTime <= max24HourTime;
132 };
133 exports.isWithinMinMax = isWithinMinMax;
134 const convertTo24Hour = (time, delimiter, includeSeconds) => {
135 const timeReg = new RegExp(`^\\s*(\\d\\d?)${delimiter}([0-5]\\d)${delimiter}?([0-5]\\d)?\\s*([AaPp][Mm])?\\s*$`);
136 const regMatches = timeReg.exec(time);
137 if (!regMatches || !regMatches.length) {
138 return;
139 }
140 let hours = regMatches[1].padStart(2, '0');
141 const minutes = regMatches[2];
142 let seconds = regMatches[3] ? `${delimiter}${regMatches[3]}` : '';
143 // When seconds is empty and 'includeSeconds' is enabled, append 0 seconds.
144 if (!seconds && includeSeconds) {
145 seconds = `${delimiter}00`;
146 }
147 const suffix = regMatches[4] || '';
148 if (suffix.toUpperCase() === 'PM' && hours !== '12') {
149 hours = `${parseInt(hours) + 12}`;
150 }
151 else if (suffix.toUpperCase() === 'AM' && hours === '12') {
152 hours = '00';
153 }
154 return `${hours}${delimiter}${minutes}${seconds}`;
155 };
156 //# sourceMappingURL=TimePickerUtils.js.map