From 3cce98ed2cf7e442c535dd59cce4482f87447d12 Mon Sep 17 00:00:00 2001 From: Yashaarth Bhandari Date: Tue, 22 Aug 2023 12:21:41 +0530 Subject: [PATCH] first commit --- index.js | 101 +++++++++++++++---------------------------------------- 1 file changed, 28 insertions(+), 73 deletions(-) diff --git a/index.js b/index.js index 30279e5..dda5de3 100644 --- a/index.js +++ b/index.js @@ -1,77 +1,32 @@ -//This function takes a Unix timestamp and an optional length parameter, -//and returns a human-readable time difference in the past or future. function timelydiff(timestamp, length = null) { - - //Calculate the time difference between the current time and the timestamp, in seconds. - let timeDifference = (Date.now() - timestamp) / 1000; - let timeDirection = null; - - - //Determine whether the time difference is in the past or future (is negative) - //and sets the timeDirection variable accordingly. - //It also takes the absolute value of timeDifference in case it's negative. - if (timeDifference < 0) { - timeDifference = Math.abs(timeDifference); - timeDirection = 'future'; - } - else { - timeDirection = 'past'; - } - - - //This is an array of arrays, each containing three elements: - //a unit of time (short and default), - //the calculated value of that unit based on the timeDifference, - //and the maximum value that unit can haveĀ  - //(i.e. 60 seconds in a minute, 24 hours in a day, etc.). - const timeUnits = [ - [{ short: 's', def: 'second' }, timeDifference, 60], - [{ short: 'm', def: 'minute' }, Math.round(timeDifference / 60), 60], - [{ short: 'h', def: 'hour' }, Math.round(timeDifference / 3600), 24], - [{ short: 'd', def: 'day' }, Math.round(timeDifference / 86400), 7], - [{ short: 'w', def: 'week' }, Math.round(timeDifference / 604800), 4], - [{ short: 'mo', def: 'month' }, Math.round(timeDifference / 2419200), 12], - [{ short: 'y', def: 'year' }, Math.round(timeDifference / 29030400), 10], - [{ short: 'dcd', def: 'decade' }, Math.round(timeDifference / 290304000), Infinity] - ]; - - - //this helper function returns the time string with "in" or "ago" depending on the time direction. - function positionTime(timeString) { - if (timeDirection === 'future') { - return `in ${timeString}`; - } else { - return `${timeString}${length === 'shorter' ? '' : ' ago'}`; - } + const now = Date.now(); + const timeDifference = Math.abs((now - timestamp) / 1000); + const timeDirection = now < timestamp ? "future" : "past"; + const timeUnits = [ + { short: "s", def: "second", max: 60 }, + { short: "m", def: "minute", max: 60 }, + { short: "h", def: "hour", max: 24 }, + { short: "d", def: "day", max: 7 }, + { short: "w", def: "week", max: 4 }, + { short: "mo", def: "month", max: 12 }, + { short: "y", def: "year", max: 10 }, + { short: "dcd", def: "decade", max: Infinity }, + ]; + + for (const unit of timeUnits) { + const { short, def, max } = unit; + const value = Math.round(timeDifference / (length === "short" ? 1 : max)); + if (value < max) { + const pluralize = value > 1 ? "s" : ""; + const timeString = + length === "shorter" + ? `${value}${short}` + : `${value} ${def}${pluralize}`; + return `${timeDirection === "future" ? "in " : ""}${timeString}${ + length === "shorter" ? "" : " ago" + }`; } - - - //This is a for loop that iterates over each element in the timeUnits array. - //It checks if the calculated value for that unit is less than the maximum value for that unit. - //If so, it formats the output string using the positionTime helper function and the length parameter. - //If the length parameter is null or longer, it returns the default format (e.g. "2 hours ago"), - //and if the length parameter is "short" or "shorter", - //it returns a shorter format (e.g. "2h" or "2 hours"). - for (const [name, value, range] of timeUnits) { - - if (value < range) { - let trunc = Math.trunc(value); - - //if value > 1, pluralize by adding 's' - let pluralize = trunc > 1 ? 's' : ''; - - if (length === null) { - return positionTime(`${trunc} ${name.def}${pluralize}`); - } - - if (length === 'short' || length === 'shorter') { - return positionTime(`${trunc}${name.short}`); - } - - break; - } - } - + } } -module.exports.timelydiff = timelydiff; \ No newline at end of file +module.exports.timelydiff = timelydiff;