Skip to content

Optimise: Timelydiff Function #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 28 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports.timelydiff = timelydiff;