Last active
June 13, 2025 00:56
-
-
Save sudojunior/628343a4e74ed3dcf202fa97b127ee6d to your computer and use it in GitHub Desktop.
Duration functions and polyfill
This file contains hidden or 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 hidden or 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 hidden or 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment