From c605037fbb25d3e0e0fff062d27cd857f6562fa0 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Thu, 25 Jul 2024 00:27:39 +0200 Subject: [PATCH] number parsing, add jsdoc --- public/scripts/utils.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/public/scripts/utils.js b/public/scripts/utils.js index a4167ffde..096954ca8 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -722,15 +722,21 @@ export function timestampToMoment(timestamp) { return moment; } +/** + * Parses a timestamp and returns a moment object representing the parsed date and time. + * @param {string|number} timestamp - The timestamp to parse. It can be a string or a number. + * @returns {moment.Moment} - A moment object representing the parsed date and time. If the timestamp is invalid, an invalid moment object is returned. + */ function parseTimestamp(timestamp) { if (!timestamp) return moment.invalid(); // Unix time (legacy TAI / tags) if (typeof timestamp === 'number' || /^\d+$/.test(timestamp)) { - if (isNaN(timestamp)) return moment.invalid(); - if (!isFinite(timestamp)) return moment.invalid(); - if (timestamp < 0) return moment.invalid(); - return moment(Number(timestamp)); + const number = Number(timestamp); + if (isNaN(number)) return moment.invalid(); + if (!isFinite(number)) return moment.invalid(); + if (number < 0) return moment.invalid(); + return moment(number); } let dtFmt = [];