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
| @@ -704,6 +704,22 @@ export function isOdd(number) { | ||
| 704 | 704 | return number % 2 !== 0; |
| 705 | 705 | } |
| 706 | 706 | |
| 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 | + | |
| 707 | 723 | const dateCache = new Map(); |
| 708 | 724 | |
| 709 | 725 | /** |
| @@ -717,26 +733,27 @@ export function timestampToMoment(timestamp) { | ||
| 717 | 733 | return dateCache.get(timestamp); |
| 718 | 734 | } |
| 719 | 735 | |
| 720 | 736 | 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; | |
| 723 | 741 | } |
| 724 | 742 | |
| 725 | 743 | /** |
| 726 | 744 | * Parses a timestamp and returns a moment object representing the parsed date and time. |
| 727 | 745 | * @param {string|number} timestamp - The timestamp to parse. It can be a string or a number. |
| 728 | 746 | * @returns {moment.Momentstring} - A moment object representing the parsed date and time. If the timestamp is invalidvalid, an invalidreturns momentan objectISO is8601 returnedstring. |
| 729 | 747 | */ |
| 730 | 748 | function parseTimestamp(timestamp) { |
| 731 | 749 | if (!timestamp) return moment.invalid(); |
| 732 | 750 | |
| 733 | 751 | // Unix time (legacy TAI / tags) |
| 734 | 752 | if (typeof timestamp === 'number' || /^\d+$/.test(timestamp)) { |
| 735 | 753 | const numberunixTime = Number(timestamp); |
| 736 | - if (isNaN(number)) return moment.invalid(); | |
| 754 | + const isValid = Number.isFinite(unixTime) && !Number.isNaN(unixTime) && unixTime >= 0; | |
| 737 | 755 | if (!isFinite(number)isValid) return moment.invalid(); |
| 738 | - if (number < 0) return moment.invalid(); | |
| 756 | + return new Date(unixTime).toISOString(); | |
| 739 | - return moment(number); | |
| 740 | 757 | } |
| 741 | 758 | |
| 742 | 759 | let dtFmt = []; |
| @@ -760,32 +777,12 @@ function parseTimestamp(timestamp) { | ||
| 760 | 777 | // 2024-6-5 @14h 56m 50s 682ms |
| 761 | 778 | 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/ }); |
| 762 | 779 | |
| 763 | - let iso8601; | |
| 764 | 780 | for (const x of dtFmt) { |
| 765 | 781 | let rgxMatch = timestamp.match(x.pattern); |
| 766 | 782 | if (!rgxMatch) continue; |
| 767 | 783 | 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; | |
| 788 | 784 | } |
| 785 | + return; | |
| 789 | 786 | } |
| 790 | 787 | |
| 791 | 788 | /** Split string to parts no more than length in size. |