Skip to content

Instantly share code, notes, and snippets.

@sudojunior
Last active June 13, 2025 00:56
Show Gist options
  • Select an option

  • Save sudojunior/628343a4e74ed3dcf202fa97b127ee6d to your computer and use it in GitHub Desktop.

Select an option

Save sudojunior/628343a4e74ed3dcf202fa97b127ee6d to your computer and use it in GitHub Desktop.
Duration functions and polyfill
import duration from "./duration-functions.js";
for (const unit of Object.keys(duration)) {
Number[unit] = function (n) {
return Number(n)[unit]();
}
Number.prototype[unit] = function () {
return duration[unit](this);
}
}
// miliseconds = n => n - identity
const seconds = n => n * 1000;
const minutes = n => seconds(n) * 60;
const hours = n => minutes(n) * 60;
const days = n => hours(n) * 24;
const weeks = n => days(n) * 7;
const months = (n) => days(n) * 30;
// unsure and unlikely - generic use of duration result is not likely in this context
const years = (n) => days(n) * 365;
// likewise here, and all additional units below
const decades = n => years(n) * 10;
const centries = n => years(n) * 100;
const millennia = n => years(n) * 1000;
export {
seconds,
minutes,
hours,
days,
weeks,
months,
years,
decades,
centries,
millennia
}
import duration from "./duration-functions.js";
for (const unit of Object.keys(duration)) {
Number[unit] = function (n) {
return Number(n)[unit]();
}
Number.prototype[unit] = function () {
return duration[unit](this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment