@renderlesskit/react
Version:
Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit
256 lines (204 loc) • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.add = add;
exports.convertValue = convertValue;
exports.cycleValue = cycleValue;
exports.getSegmentLimits = getSegmentLimits;
exports.isNumeric = isNumeric;
exports.parseNumber = parseNumber;
exports.setSegment = setSegment;
var _utils = require("../../utils");
function convertValue(value) {
if (!value) {
return undefined;
}
return new Date(value);
}
function getSegmentLimits(date, type, options) {
var value, minValue, maxValue;
switch (type) {
case "day":
value = (0, _utils.getDate)(date);
minValue = 1;
maxValue = (0, _utils.getDaysInMonth)(date);
break;
case "dayPeriod":
value = (0, _utils.getHours)(date) >= 12 ? 12 : 0;
minValue = 0;
maxValue = 12;
break;
case "hour":
value = (0, _utils.getHours)(date);
if (options.hour12) {
var isPM = value >= 12;
minValue = isPM ? 12 : 0;
maxValue = isPM ? 23 : 11;
} else {
minValue = 0;
maxValue = 23;
}
break;
case "minute":
value = (0, _utils.getMinutes)(date);
minValue = 0;
maxValue = 59;
break;
case "second":
value = (0, _utils.getSeconds)(date);
minValue = 0;
maxValue = 59;
break;
case "month":
value = (0, _utils.getMonth)(date) + 1;
minValue = 1;
maxValue = 12;
break;
case "year":
value = (0, _utils.getYear)(date);
minValue = 1;
maxValue = 9999;
break;
default:
return {};
}
return {
value: value,
minValue: minValue,
maxValue: maxValue
};
}
function add(value, part, amount, options) {
switch (part) {
case "day":
{
var day = (0, _utils.getDate)(value);
return (0, _utils.setDate)(value, cycleValue(day, amount, 1, (0, _utils.getDaysInMonth)(value)));
}
case "dayPeriod":
{
var hours = (0, _utils.getHours)(value);
var isPM = hours >= 12;
return (0, _utils.setHours)(value, isPM ? hours - 12 : hours + 12);
}
case "hour":
{
var _hours = (0, _utils.getHours)(value);
var min = 0;
var max = 23;
if (options.hour12) {
var _isPM = _hours >= 12;
min = _isPM ? 12 : 0;
max = _isPM ? 23 : 11;
}
_hours = cycleValue(_hours, amount, min, max);
return (0, _utils.setHours)(value, _hours);
}
case "minute":
{
var minutes = cycleValue((0, _utils.getMinutes)(value), amount, 0, 59, true);
return (0, _utils.setMinutes)(value, minutes);
}
case "month":
{
var months = cycleValue((0, _utils.getMonth)(value), amount, 0, 11);
return (0, _utils.setMonth)(value, months);
}
case "second":
{
var seconds = cycleValue((0, _utils.getSeconds)(value), amount, 0, 59, true);
return (0, _utils.setSeconds)(value, seconds);
}
case "year":
{
var year = cycleValue((0, _utils.getYear)(value), amount, 1, 9999, true);
return (0, _utils.setYear)(value, year);
}
}
}
function cycleValue(value, amount, min, max) {
var round = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
if (round) {
value += amount > 0 ? 1 : -1;
if (value < min) {
value = max;
}
var div = Math.abs(amount);
if (amount > 0) {
value = Math.ceil(value / div) * div;
} else {
value = Math.floor(value / div) * div;
}
if (value > max) {
value = min;
}
} else {
value += amount;
if (value < min) {
value = max - (min - value - 1);
} else if (value > max) {
value = min + (value - max - 1);
}
}
return value;
}
function setSegment(value, part, segmentValue, options) {
switch (part) {
case "day":
return (0, _utils.setDate)(value, segmentValue);
case "dayPeriod":
{
var hours = (0, _utils.getHours)(value);
var wasPM = hours >= 12;
var isPM = segmentValue >= 12;
if (isPM === wasPM) {
return value;
}
return (0, _utils.setHours)(value, wasPM ? hours - 12 : hours + 12);
}
case "hour":
// In 12 hour time, ensure that AM/PM does not change
if (options.hour12) {
var _hours2 = (0, _utils.getHours)(value);
var _wasPM = _hours2 >= 12;
if (!_wasPM && segmentValue === 12) {
segmentValue = 0;
}
if (_wasPM && segmentValue < 12) {
segmentValue += 12;
}
}
return (0, _utils.setHours)(value, segmentValue);
case "minute":
return (0, _utils.setMinutes)(value, segmentValue);
case "month":
return (0, _utils.setMonth)(value, segmentValue - 1);
case "second":
return (0, _utils.setSeconds)(value, segmentValue);
case "year":
return (0, _utils.setYear)(value, segmentValue);
}
} // Converts unicode number strings to real JS numbers.
// Numbers can be displayed and typed in many number systems, but JS
// only understands latin numbers.
// See https://www.fileformat.info/info/unicode/category/Nd/list.htm
// for a list of unicode numeric characters.
// Currently only Arabic and Latin numbers are supported, but more
// could be added here in the future.
// Keep this in sync with `isNumeric` below.
function parseNumber(str) {
str = str // Arabic Indic
.replace(/[\u0660-\u0669]/g, function (c) {
return String(c.charCodeAt(0) - 0x0660);
}) // Extended Arabic Indic
.replace(/[\u06f0-\u06f9]/g, function (c) {
return String(c.charCodeAt(0) - 0x06f0);
});
return Number(str);
} // Checks whether a unicode string could be converted to a number.
// Keep this in sync with `parseNumber` above.
function isNumeric(str) {
return /^[0-9\u0660-\u0669\u06f0-\u06f9]+$/.test(str);
}
//# sourceMappingURL=index.js.map