diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/getTimes.js | 12 | ||||
| -rw-r--r-- | utils/imageUtils.js | 11 |
2 files changed, 15 insertions, 8 deletions
diff --git a/utils/getTimes.js b/utils/getTimes.js index 491d139..95df803 100644 --- a/utils/getTimes.js +++ b/utils/getTimes.js @@ -109,7 +109,7 @@ export const timeStamptoHour = (timestamp) => { export function unixTimestampToRelativeTime(unixTimestamp) { const now = Math.floor(Date.now() / 1000); // Current Unix timestamp in seconds - const secondsAgo = now - unixTimestamp; + let secondsDifference = now - unixTimestamp; const intervals = [ { label: "year", seconds: 31536000 }, @@ -121,12 +121,14 @@ export function unixTimestampToRelativeTime(unixTimestamp) { { label: "second", seconds: 1 }, ]; + const isFuture = secondsDifference < 0; + secondsDifference = Math.abs(secondsDifference); + for (const interval of intervals) { - const count = Math.floor(secondsAgo / interval.seconds); + const count = Math.floor(secondsDifference / interval.seconds); if (count >= 1) { - return count === 1 - ? ` ${count} ${interval.label} ago` - : ` ${count} ${interval.label}s ago`; + const label = count === 1 ? interval.label : `${interval.label}s`; + return isFuture ? `${count} ${label} from now` : `${count} ${label} ago`; } } diff --git a/utils/imageUtils.js b/utils/imageUtils.js index 56c98fb..8025d5b 100644 --- a/utils/imageUtils.js +++ b/utils/imageUtils.js @@ -24,10 +24,15 @@ export function getRandomId() { export function truncateImgUrl(url) { // Find the index of .png if not found find the index of .jpg let index = - url.indexOf(".png") !== -1 ? url.indexOf(".png") : url.indexOf(".jpg"); + url?.indexOf(".png") !== -1 ? url?.indexOf(".png") : url?.indexOf(".jpg"); + if (index !== -1) { - // If .png is found - url = url.slice(0, index + 4); // Slice the string from the start to the index of .png plus 4 (the length of .png) + // If .png or .jpg is found + url = url?.slice(0, index + 4); // Slice the string from the start to the index of .png or .jpg plus 4 (the length of .png or .jpg) + } else { + // If .png or .jpg is not found + return url; // Return the original url string } + return url; } |