box-ui-elements-mlh
Version:
93 lines (70 loc) • 4.12 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var VALID_TIME_REGEX = /[0-9]{1,2}.?.?.?[0-9]{0,2}\s?[AaPp]?\.?\s?[Mm]?/;
var AM_REGEX = /[Aa]\.?\s?[Mm]/;
var PM_REGEX = /[Pp]\.?\s?[Mm]/;
var NUMBER_REGEX = /[0-9]{1,2}/g;
var TWELVE_HOURS = 12;
var SIXTY_MINUTES = 60;
var DEFAULT_PARSED_TIME = {
hours: 0,
minutes: 0
};
var VALID_NUMBER_COUNT = 2;
/**
* Check that the input string is in a valid time format
* @param input - input string to test
* @returns
*/
export var isValidTime = function isValidTime(input) {
return !!input && VALID_TIME_REGEX.test(input);
};
/**
* Parse an input string and convert it into an object containing numerical hours and minutes.
* @param input - input string to be converted
* @returns
*/
export var parseTimeFromString = function parseTimeFromString(input) {
if (!input) return DEFAULT_PARSED_TIME; // Throw an error if the input fails the initial regex
if (!isValidTime(input)) {
throw new SyntaxError();
} // Throw an error if the input contains more than four numbers
var timeArray = input.match(NUMBER_REGEX);
if (!timeArray || !timeArray.length) return DEFAULT_PARSED_TIME;
if (timeArray.length > VALID_NUMBER_COUNT) {
throw new SyntaxError();
} // If there are three numbers total, the regex match will split them
// unevenly, loading two numbers for the hours value and one for the
// minutes value. For instance, "305" would be split into ["30", "5"].
// This should be ["3", "05"] instead, so we will move the second
// number in the hours value to the beginning of the minutes value.
var hasAMNotation = AM_REGEX.test(input);
var hasPMNotation = PM_REGEX.test(input);
var _timeArray = _slicedToArray(timeArray, 2),
hours = _timeArray[0],
minutes = _timeArray[1];
if (hours && minutes && hours.length === 2 && minutes.length === 1) {
minutes = hours[1] + minutes;
hours = hours[0];
} // Convert the minutes value into an integer
var numericMinutes = minutes ? parseInt(minutes, 10) : 0; // Set the hours to "0" if the input translates to midnight
// Convert the hours to 24-hour format if this is a PM time
var numericHours = parseInt(hours, 10);
if (hasAMNotation && numericHours === TWELVE_HOURS) {
numericHours = 0;
} else if (hasPMNotation && numericHours < TWELVE_HOURS) {
numericHours += TWELVE_HOURS;
} // Throw an error if the hours or minutes are out of range
if (numericHours > TWELVE_HOURS * 2 || numericMinutes >= SIXTY_MINUTES) {
throw new SyntaxError();
}
return {
hours: numericHours,
minutes: numericMinutes
};
};
//# sourceMappingURL=TimeInputUtils.js.map