number parsing, add jsdoc

This commit is contained in:
Wolfsblvt 2024-07-25 00:27:39 +02:00
parent d30fc5d165
commit c605037fbb
1 changed files with 10 additions and 4 deletions

View File

@ -722,15 +722,21 @@ export function timestampToMoment(timestamp) {
return moment; 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) { function parseTimestamp(timestamp) {
if (!timestamp) return moment.invalid(); if (!timestamp) return moment.invalid();
// Unix time (legacy TAI / tags) // Unix time (legacy TAI / tags)
if (typeof timestamp === 'number' || /^\d+$/.test(timestamp)) { if (typeof timestamp === 'number' || /^\d+$/.test(timestamp)) {
if (isNaN(timestamp)) return moment.invalid(); const number = Number(timestamp);
if (!isFinite(timestamp)) return moment.invalid(); if (isNaN(number)) return moment.invalid();
if (timestamp < 0) return moment.invalid(); if (!isFinite(number)) return moment.invalid();
return moment(Number(timestamp)); if (number < 0) return moment.invalid();
return moment(number);
} }
let dtFmt = []; let dtFmt = [];