Last active
February 12, 2021 17:33
-
-
Save sudojunior/628343a4e74ed3dcf202fa97b127ee6d to your computer and use it in GitHub Desktop.
Duration functions and polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number.seconds = function(n) { | |
return Number(n).seconds(); | |
} | |
Number.prototype.seconds = function() { | |
return this.valueOf() * 1000; | |
} | |
Number.minutes = functions (n) { | |
return Number(n).minutes(); | |
} | |
Number.prototype.minutes = function () { | |
return this.seconds() * 60; | |
} | |
Number.hours = function () { | |
return Number(n).hours(); | |
} | |
Number.prototype.hours = function () { | |
return this.minutes() * 60; | |
} | |
Number.days = function (n) { | |
return Number(n).days(); | |
} | |
Number.prototype.days = function () { | |
return this.hours() * 24; | |
} | |
Number.weeks = function (n) { | |
return Number(n).weeks(); | |
} | |
Number.prototype.weeks = function(n) { | |
return this.days() * 7; | |
} | |
// stopping here, as stated before in the functions file, not all of it is consistant |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment