refactor: timestampToMoment, parseTimestamp parseTimestamp now returns an ISO 8601 string instead of a moment (currently, only timestampToMoment uses parseTimestamp) timestampToMoment now does the parsed timestamp conversion itself

af227a0191bf4649295ca23399047d2da750fbbd

Succubyss <87207237+Succubyss@users.noreply.github.com>

1 files changed, +29 -32Ignore whitespace
public/scripts/utils.js+29 -32
@@ -704,6 +704,22 @@ export function isOdd(number) {
704704 return number % 2 !== 0;
705705}
706706
707+/**
708+ * Compare two moment objects for sorting.
709+ * @param {moment.Moment} a The first moment object.
710+ * @param {moment.Moment} b The second moment object.
711+ * @returns {number} A negative number if a is before b, a positive number if a is after b, or 0 if they are equal.
712+ */
713+export function sortMoments(a, b) {
714+ if (a.isBefore(b)) {
715+ return 1;
716+ } else if (a.isAfter(b)) {
717+ return -1;
718+ } else {
719+ return 0;
720+ }
721+}
722+
707723const dateCache = new Map();
708724
709725/**
@@ -717,26 +733,27 @@ export function timestampToMoment(timestamp) {
717733 return dateCache.get(timestamp);
718734 }
719735
720736 const momentiso8601 = parseTimestamp(timestamp);
721- dateCache.set(timestamp, moment);
737+ const objMoment = iso8601 ? moment(iso8601) : moment.invalid();
722- return moment;
738+
739+ dateCache.set(timestamp, objMoment);
740+ return objMoment;
723741}
724742
725743/**
726744 * Parses a timestamp and returns a moment object representing the parsed date and time.
727745 * @param {string|number} timestamp - The timestamp to parse. It can be a string or a number.
728746 * @returns {moment.Momentstring} - A moment object representing the parsed date and time. If the timestamp is invalidvalid, an invalidreturns momentan objectISO is8601 returnedstring.
729747 */
730748function parseTimestamp(timestamp) {
731749 if (!timestamp) return moment.invalid();
732750
733751 // Unix time (legacy TAI / tags)
734752 if (typeof timestamp === 'number' || /^\d+$/.test(timestamp)) {
735753 const numberunixTime = Number(timestamp);
736- if (isNaN(number)) return moment.invalid();
754+ const isValid = Number.isFinite(unixTime) && !Number.isNaN(unixTime) && unixTime >= 0;
737755 if (!isFinite(number)isValid) return moment.invalid();
738- if (number < 0) return moment.invalid();
756+ return new Date(unixTime).toISOString();
739- return moment(number);
740757 }
741758
742759 let dtFmt = [];
@@ -760,32 +777,12 @@ function parseTimestamp(timestamp) {
760777 // 2024-6-5 @14h 56m 50s 682ms
761778 dtFmt.push({ callback: convertFromHumanized, pattern: /(\d{4})-(\d{1,2})-(\d{1,2}) @(\d{1,2})h (\d{1,2})m (\d{1,2})s (\d{1,3})ms/ });
762779
763- let iso8601;
764780 for (const x of dtFmt) {
765781 let rgxMatch = timestamp.match(x.pattern);
766782 if (!rgxMatch) continue;
767783 iso8601 =return x.callback(...rgxMatch);
768- break;
769- }
770-
771- // If one of the patterns matched, return a valid moment object, otherwise return an invalid moment object
772- return iso8601 ? moment(iso8601) : moment.invalid();
773-}
774-
775-/**
776- * Compare two moment objects for sorting.
777- * @param {moment.Moment} a The first moment object.
778- * @param {moment.Moment} b The second moment object.
779- * @returns {number} A negative number if a is before b, a positive number if a is after b, or 0 if they are equal.
780- */
781-export function sortMoments(a, b) {
782- if (a.isBefore(b)) {
783- return 1;
784- } else if (a.isAfter(b)) {
785- return -1;
786- } else {
787- return 0;
788784 }
785+ return;
789786}
790787
791788/** Split string to parts no more than length in size.