@activecollab/components
Version:
ActiveCollab Components
116 lines (110 loc) • 6.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withLeadingZero = exports.isValidTime = exports.isDecimal = exports.formatHours = void 0;
var _currencyUtils = require("./currencyUtils");
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
* @function formatHours
* @description
* Formats a decimal number representing hours into a formatted string (HH:MM) or a short form if needed.
* The input can be a number, string, or undefined. It handles various formats and can optionally add a
* leading zero to the hours component. With the new "format" argument, if set to "short" and the value is
* greater than or equal to 1000, it will return a shortened format (e.g., 1K) similar to formatNumber.
*
* @param {number | string | undefined} num - The input representing the hours.
* @param {boolean} [withLeadingZeroHours=false] - Whether to add a leading zero to the hours part.
* @param {boolean} [trimZeroMinutes=false] - Whether to remove minutes if they are zero.
* @param {"long" | "short"} [format="long"] - The format type, either "long" for full numbers or "short" for abbreviated output.
*
* @returns {string} - A formatted time string or a shortened string.
*
* @example
* formatHours(1.5) // "1:30"
* formatHours("3.5", true) // "03:30"
* formatHours(1500, false, false, "short") // "1K" (using formatNumber)
*/
var formatHours = exports.formatHours = function formatHours(num) {
var withLeadingZeroHours = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var trimZeroMinutes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var format = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "long";
// Handle the explicit zero case.
if (num === 0 || num === "0") {
return trimZeroMinutes ? "0" : withLeadingZeroHours ? "00:00" : "0:00";
}
if (!num) {
return "";
}
// If the input is in colon format, process it as before.
if (typeof num === "string" && num.indexOf(":") >= 0) {
// eslint-disable-next-line prefer-const
var _num$split = num.split(":"),
_num$split2 = _slicedToArray(_num$split, 2),
_hours = _num$split2[0],
_minutes = _num$split2[1];
if (_minutes && _minutes.length === 1 && Number(_minutes) < 10) {
_minutes = "".concat(Number(_minutes), "0");
}
if (_hours && _minutes) {
if (trimZeroMinutes && _minutes === "00") {
return withLeadingZeroHours ? withLeadingZero(_hours) : String(Number(_hours));
}
return withLeadingZeroHours ? "".concat(withLeadingZero(_hours), ":").concat(_minutes) : "".concat(_hours, ":").concat(_minutes);
} else if (_hours && !_minutes) {
return withLeadingZeroHours ? "".concat(withLeadingZero(_hours), ":00") : "".concat(_hours, ":00");
} else if (!_hours && _minutes) {
return withLeadingZeroHours ? "00:".concat(_minutes) : "0:".concat(_minutes);
} else if (!_hours && !_minutes) {
return withLeadingZeroHours ? "00:00" : "0:00";
}
return withLeadingZeroHours ? "00:".concat(_minutes) : "0:".concat(_minutes);
}
// Replace comma with dot if needed.
if (typeof num === "string" && num.indexOf(",") >= 0) {
num = num.replace(",", ".");
}
var input = typeof num === "string" ? parseFloat(num) : num;
// Use short formatting if specified and the value is >= 1000.
if (format === "short" && input >= 1000) {
return (0, _currencyUtils.formatNumber)(input, ",", ".", true, 2, "short");
}
// For whole numbers.
if (!isDecimal(input)) {
if (trimZeroMinutes) {
return withLeadingZeroHours ? withLeadingZero(input) : String(Number(input));
}
return withLeadingZeroHours ? "".concat(withLeadingZero(input), ":00") : "".concat(input, ":00");
}
// Process decimal hours.
var decimal = input.toFixed(2);
var time = decimal.toString().split(".");
var hours = time[0];
if (withLeadingZeroHours) {
hours = withLeadingZero(hours);
}
var minutes = time[1];
var minutesFormatted = Math.round(parseInt(minutes, 10) / 100 * 60);
if (trimZeroMinutes && minutesFormatted === 0) {
return hours;
}
return "".concat(hours, ":").concat(withLeadingZero(minutesFormatted));
};
var withLeadingZero = exports.withLeadingZero = function withLeadingZero(num) {
var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
var s = "".concat(num);
while (s.length < size) s = "0" + s;
return s;
};
var isDecimal = exports.isDecimal = function isDecimal(num) {
return !Number.isInteger(num);
};
var isValidTime = exports.isValidTime = function isValidTime(time) {
return time === undefined || /^([01]\d|2[0-3]):([0-5]\d)$/.test(time);
};
//# sourceMappingURL=timeUtils.js.map