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