Created
June 28, 2020 03:36
-
-
Save UnicornEatingPopcorn/9b6114bedae63e57073c93b9a8921812 to your computer and use it in GitHub Desktop.
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
const minute = 60 | |
const hour = 3600 | |
const day = 86400 | |
const year = 31536000 | |
function formatDuration(number) { | |
if(number < 0) { | |
return "function accepts only positive integers" | |
} | |
if(number === 0) { | |
return "now" | |
} | |
return whichTimeUnitBelongs(number) | |
} | |
function whichTimeUnitBelongs(number) { | |
if(number < minute) { | |
return consistsOfSeconds(number) | |
} | |
if(number >= minute && number < hour) { | |
return consistsOfMinutes(number) | |
} | |
if(number >= hour && number < day) { | |
return consistsOf(number, hour, "hour") | |
} | |
if(number >= day && number < year) { | |
return consistsOf(number, day, "day") | |
} | |
if(number >= year) { | |
return consistsOf(number, year, "year") | |
} | |
} | |
function consistsOfSeconds(number) { | |
if(number === 1) { | |
return "1 second" | |
} | |
if(number < minute && number !== 1) { | |
return `${number} seconds` | |
} | |
} | |
function consistsOfMinutes(number) { | |
const amount = Math.floor(number / minute) | |
const modulo = number % minute | |
const seconds = consistsOfSeconds(modulo) | |
const nounNumber = amount !== 1 ? "s" : "" | |
if(number === minute) { | |
return "1 minute" | |
} | |
if(modulo === 0 && number !== minute ) { | |
return `${amount} minutes` | |
} else { | |
return `${amount} minute` + `${nounNumber}` + " and " + `${seconds}` | |
} | |
} | |
function consistsOf(number, timeUnit, timeUnitName) { | |
const amount = Math.floor(number / timeUnit) | |
const modulo = number % timeUnit | |
const nounNumber = amount !== 1 ? "s" : "" | |
if(number === timeUnit) { | |
return `1 ${timeUnitName}` | |
} | |
if(modulo === 0 && number !== timeUnit) { | |
return `${amount} ${timeUnitName}s` | |
} else { | |
const timeUnitOfModulo = whichTimeUnitBelongs(modulo) | |
const punctuation = timeUnitOfModulo.includes(" and ") ? ", " : " and " | |
return `${amount} ${timeUnitName}` + `${nounNumber}` + `${punctuation}` + `${timeUnitOfModulo}` | |
} | |
} | |
formatDuration(31530060001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment