@atlaskit/datetime-picker
Version:
A date time picker allows the user to select an associated date and time.
200 lines • 7.22 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { convertTo24hrTime } from './convert-to24hr-time';
import { isValid } from './is-valid';
import { removeSpacer } from './remove-spacer';
function assignToDate(time) {
var dateTime = new Date();
dateTime.setHours(time.hour);
dateTime.setMinutes(time.minute);
// milliseconds is not supported
dateTime.setSeconds(time.seconds || 0, 0);
return dateTime;
}
function parseLegacyTime(time) {
var allowCompactTime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var trimmedTime = time.toString().trim();
var compactTime = allowCompactTime ? trimmedTime.match(/^\d{5}$/) : null;
if (compactTime) {
var normalizedTime = "0".concat(compactTime[0]);
var hour = Number(normalizedTime.slice(0, 2));
var minute = Number(normalizedTime.slice(2, 4));
var seconds = Number(normalizedTime.slice(4, 6));
if (minute <= 59 && seconds <= 59) {
return assignToDate({
hour: hour,
minute: minute,
seconds: seconds
});
}
}
if (/^\d{5}$/.test(trimmedTime)) {
throw RangeError('invalid time format');
}
if (!isValid(trimmedTime)) {
throw RangeError('invalid time format');
}
var time1 = removeSpacer(trimmedTime);
var time2 = convertTo24hrTime(time1);
if (!time2) {
throw RangeError('invalid time format');
}
return assignToDate(_objectSpread(_objectSpread({}, time2), {}, {
hour: time2.hour === 24 ? 0 : time2.hour
}));
}
var escapeRegExp = function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
var normalizeDigits = function normalizeDigits(time, timeFormat) {
return Array.from(time, function (character) {
return timeFormat.digits[character] || character;
}).join('');
};
var isValidHourForCycle = function isValidHourForCycle(hour, hourCycle) {
return hourCycle === 'h11' ? hour <= 11 : hourCycle === 'h12' ? hour >= 1 && hour <= 12 : hour <= 24;
};
function parseExactLocalizedTime(time, timeFormat) {
if (!timeFormat.timeParts) {
return undefined;
}
var dayPeriods = Object.entries(timeFormat.dayPeriods).filter(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
value = _ref2[1];
return value;
});
var groupTypes = [];
var patternParts = timeFormat.timeParts.map(function (part) {
if (part.type === 'hour' || part.type === 'minute' || part.type === 'second') {
groupTypes.push(part.type);
return part.type === 'hour' ? '(\\d{1,2})' : '(\\d{2})';
}
if (part.type === 'dayPeriod') {
if (dayPeriods.length === 0) {
return null;
}
groupTypes.push('dayPeriod');
return "(".concat(dayPeriods.map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
value = _ref4[1];
return escapeRegExp(value);
}).join('|'), ")");
}
if (part.type === 'literal') {
return escapeRegExp(part.value);
}
return null;
});
if (patternParts.some(function (part) {
return part === null;
})) {
return undefined;
}
var match = time.match(new RegExp("^".concat(patternParts.join(''), "$"), 'u'));
if (!match) {
return undefined;
}
var values = Object.fromEntries(groupTypes.map(function (type, index) {
return [type, match[index + 1]];
}));
if (!values.hour || !values.minute) {
return undefined;
}
var hour = Number(values.hour);
var minute = Number(values.minute);
var seconds = Number(values.second || '0');
if (minute > 59 || seconds > 59) {
return null;
}
if (values.dayPeriod) {
var _dayPeriods$find;
var period = (_dayPeriods$find = dayPeriods.find(function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
value = _ref6[1];
return value === values.dayPeriod;
})) === null || _dayPeriods$find === void 0 ? void 0 : _dayPeriods$find[0];
if (!period || !isValidHourForCycle(hour, timeFormat.hourCycle)) {
return null;
}
return assignToDate({
hour: period === 'pm' ? hour % 12 + 12 : hour % 12,
minute: minute,
seconds: seconds
});
}
if (hour > 24) {
return null;
}
return assignToDate({
hour: hour === 24 ? 0 : hour,
minute: minute,
seconds: seconds
});
}
function parseLocalizedTime(normalizedTime, timeFormat) {
var dayPeriods = Object.entries(timeFormat.dayPeriods).filter(function (_ref7) {
var _ref8 = _slicedToArray(_ref7, 2),
value = _ref8[1];
return value;
});
var markers = dayPeriods.filter(function (_ref9) {
var _ref0 = _slicedToArray(_ref9, 2),
value = _ref0[1];
return new RegExp(escapeRegExp(value), 'u').test(normalizedTime);
});
if (markers.length === 0) {
return undefined;
}
if (markers.length !== 1) {
return null;
}
var _markers = _slicedToArray(markers, 1),
_markers$ = _slicedToArray(_markers[0], 2),
period = _markers$[0],
marker = _markers$[1];
var numericTime = normalizedTime.replace(new RegExp(escapeRegExp(marker), 'u'), '').trim();
var match = numericTime.match(/^(\d{1,2})(?:[:.](\d{2}))?(?:[:.](\d{2}))?$/);
if (!match) {
return null;
}
var _match = _slicedToArray(match, 4),
hourValue = _match[1],
_match$ = _match[2],
minuteValue = _match$ === void 0 ? '0' : _match$,
_match$2 = _match[3],
secondValue = _match$2 === void 0 ? '0' : _match$2;
var hour = Number(hourValue);
var minute = Number(minuteValue);
var seconds = Number(secondValue);
if (!isValidHourForCycle(hour, timeFormat.hourCycle) || minute > 59 || seconds > 59) {
return null;
}
return assignToDate({
hour: period === 'pm' ? hour % 12 + 12 : hour % 12,
minute: minute,
seconds: seconds
});
}
export default function parseTime(time, timeFormat) {
if (timeFormat) {
var normalizedTime = normalizeDigits(time, timeFormat);
var exactLocalizedTime = parseExactLocalizedTime(normalizedTime, timeFormat);
if (exactLocalizedTime instanceof Date) {
return exactLocalizedTime;
}
if (exactLocalizedTime === null) {
throw RangeError('invalid time format');
}
var localizedTime = parseLocalizedTime(normalizedTime, timeFormat);
if (localizedTime instanceof Date) {
return localizedTime;
}
if (localizedTime === null) {
throw RangeError('invalid time format');
}
return parseLegacyTime(normalizedTime, true);
}
return parseLegacyTime(time);
}