@coreui/react-pro
Version:
UI Components Library for React.js
177 lines (174 loc) • 8.07 kB
JavaScript
/**
* Converts a 12-hour time format to a 24-hour time format.
* @param {('am' | 'pm')} abbr The abbreviation indicating AM or PM.
* @param {number} hour The hour to be converted.
* @returns {number} The hour in 24-hour format.
*/
var convert12hTo24h = function (abbr, hour) {
if (abbr === 'am' && hour === 12) {
return 0;
}
if (abbr === 'am') {
return hour;
}
if (abbr === 'pm' && hour === 12) {
return 12;
}
return hour + 12;
};
/**
* Converts a 24-hour time format to a 12-hour format.
* @param {number} hour The hour to be converted.
* @returns {number} The hour in 12-hour format.
*/
var convert24hTo12h = function (hour) { return hour % 12 || 12; };
/**
* Converts a time input into a Date object.
* @param {Date | string | null | undefined} time The time input to be converted.
* @returns {Date | null} The converted Date object or null if the input is falsy.
*/
var convertTimeToDate = function (time) {
return time ? (time instanceof Date ? time : new Date("1970-01-01 ".concat(time))) : null;
};
/**
* Retrieves the AM/PM part of the specified date according to the given locale.
* @param {Date} date The date from which to extract the AM/PM part.
* @param {string} locale The locale to use for formatting.
* @returns {string} 'am' or 'pm' based on the given date and locale.
*/
var getAmPm = function (date, locale) {
if (date.toLocaleTimeString(locale).includes('AM')) {
return 'am';
}
if (date.toLocaleTimeString(locale).includes('PM')) {
return 'pm';
}
return date.getHours() >= 12 ? 'pm' : 'am';
};
/**
* Formats an array of time values (hours, minutes, or seconds) according to the specified locale and partial.
* @param {number[]} values An array of time values to format.
* @param {string} locale The locale to use for formatting.
* @param {('hour' | 'minute' | 'second')} partial The type of time value to format.
* @returns {Array} An array of objects with the original value and its localized label.
*/
var formatTimePartials = function (values, locale, partial) {
var date = new Date();
var formatter = new Intl.DateTimeFormat(locale, {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
});
return values.map(function (value) {
var _a;
if (partial === 'hour') {
date.setHours(value);
}
if (partial === 'minute') {
date.setMinutes(value);
}
if (partial === 'second') {
date.setSeconds(value);
}
return {
value: value,
label: ((_a = formatter.formatToParts(date).find(function (part) { return part.type === partial; })) === null || _a === void 0 ? void 0 : _a.value) || '',
};
});
};
/**
* Generates localized time partials (hours, minutes, seconds) based on the given parameters.
* @param {string} locale The locale to use for generating localized time partials.
* @param {'auto' | boolean} ampm Determines whether to use 12-hour or 24-hour format. 'auto' decides based on locale.
* @param {boolean | number[] | Function} hours An array of hours, a boolean, or a function to generate hours.
* @param {boolean | number[] | Function} minutes An array of minutes, a boolean, or a function to generate minutes.
* @param {boolean | number[] | Function} seconds An array of seconds, a boolean, or a function to generate seconds.
* @returns {LocalizedTimePartials} An object containing arrays of localized time partials and a boolean indicating if 12-hour format is used.
*/
var getLocalizedTimePartials = function (locale, ampm, hours, minutes, seconds) {
if (ampm === void 0) { ampm = 'auto'; }
if (hours === void 0) { hours = []; }
if (minutes === void 0) { minutes = []; }
if (seconds === void 0) { seconds = []; }
var hour12 = (ampm === 'auto' && isAmPm(locale)) || ampm === true;
var listOfHours = Array.isArray(hours) && hours.length > 0
? hours
: (typeof hours === 'function'
? Array.from({ length: hour12 ? 12 : 24 }, function (_, i) { return (hour12 ? i + 1 : i); }).filter(function (hour) {
return hours(hour);
})
: Array.from({ length: hour12 ? 12 : 24 }, function (_, i) { return (hour12 ? i + 1 : i); }));
var listOfMinutes = Array.isArray(minutes) && minutes.length > 0
? minutes
: (typeof minutes === 'function'
? Array.from({ length: 60 }, function (_, i) { return i; }).filter(function (minute) { return minutes(minute); })
: Array.from({ length: 60 }, function (_, i) { return i; }));
var listOfSeconds = Array.isArray(seconds) && seconds.length > 0
? seconds
: (typeof seconds === 'function'
? Array.from({ length: 60 }, function (_, i) { return i; }).filter(function (second) { return seconds(second); })
: Array.from({ length: 60 }, function (_, i) { return i; }));
return {
listOfHours: formatTimePartials(listOfHours, locale, 'hour'),
listOfMinutes: formatTimePartials(listOfMinutes, locale, 'minute'),
listOfSeconds: formatTimePartials(listOfSeconds, locale, 'second'),
hour12: hour12,
};
};
/**
* Gets the selected hour from a date object in either 12-hour or 24-hour format based on locale and preference.
* @param {Date | null} date The date object from which to extract the hour. If null, the function returns an empty string.
* @param {string} locale The locale to use when determining whether to return in 12-hour or 24-hour format.
* @param {'auto' | boolean} ampm Determines the format of the hour returned. 'auto' decides based on locale, true forces 12-hour format, and false forces 24-hour format.
* @returns {string | number} The hour in the specified format or an empty string if the date is null.
*/
var getSelectedHour = function (date, locale, ampm) {
if (ampm === void 0) { ampm = 'auto'; }
return date
? ((ampm === 'auto' && isAmPm(locale)) || ampm === true
? convert24hTo12h(date.getHours())
: date.getHours())
: '';
};
/**
* Gets the selected minutes from a date object.
* @param {Date | null} date The date object from which to extract the minutes. If null, the function returns an empty string.
* @returns {string | number} The minutes from the date or an empty string if the date is null.
*/
var getSelectedMinutes = function (date) { return (date ? date.getMinutes() : ''); };
/**
* Gets the selected seconds from a date object.
* @param {Date | null} date The date object from which to extract the seconds. If null, the function returns an empty string.
* @returns {string | number} The seconds from the date or an empty string if the date is null.
*/
var getSelectedSeconds = function (date) { return (date ? date.getSeconds() : ''); };
/**
* Determines if the given locale uses AM/PM format.
* @param {string} locale The locale to check.
* @returns {boolean} True if the locale uses AM/PM format, otherwise false.
*/
var isAmPm = function (locale) {
return ['am', 'AM', 'pm', 'PM'].some(function (el) { return new Date().toLocaleString(locale).includes(el); });
};
/**
* Validates if the given string represents a valid time.
* @param {string} time The time string to validate.
* @returns {boolean} True if the string is a valid time, otherwise false.
*/
var isValidTime = function (time) {
var d = new Date("1970-01-01 ".concat(time));
return d instanceof Date && d.getTime();
};
exports.convert12hTo24h = convert12hTo24h;
exports.convert24hTo12h = convert24hTo12h;
exports.convertTimeToDate = convertTimeToDate;
exports.formatTimePartials = formatTimePartials;
exports.getAmPm = getAmPm;
exports.getLocalizedTimePartials = getLocalizedTimePartials;
exports.getSelectedHour = getSelectedHour;
exports.getSelectedMinutes = getSelectedMinutes;
exports.getSelectedSeconds = getSelectedSeconds;
exports.isAmPm = isAmPm;
exports.isValidTime = isValidTime;
//# sourceMappingURL=utils.js.map
;