diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-31 11:18:21 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-31 11:18:21 +0800 |
commit | 229f11f164ce5109b771295d9aee8ebb74314181 (patch) | |
tree | ad4cdcc1926a1e7ec563f331c8f1edba7770b867 /packages/instant/src/util | |
parent | 0e1e9b27f6f5b93e1f9b0e2833bc600836f8e757 (diff) | |
download | dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar.gz dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar.bz2 dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar.lz dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar.xz dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.tar.zst dexon-sol-tools-229f11f164ce5109b771295d9aee8ebb74314181.zip |
Nice formatting of displayed time
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/time.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/instant/src/util/time.ts b/packages/instant/src/util/time.ts new file mode 100644 index 000000000..bfe69cad5 --- /dev/null +++ b/packages/instant/src/util/time.ts @@ -0,0 +1,39 @@ +const secondsToMinutesAndRemainingSeconds = (seconds: number): { minutes: number; remainingSeconds: number } => { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds - minutes * 60; + + return { + minutes, + remainingSeconds, + }; +}; + +const padZero = (aNumber: number): string => { + return aNumber < 10 ? `0${aNumber}` : aNumber.toString(); +}; + +export const timeUtil = { + // converts seconds to human readable version of seconds or minutes + secondsToHumanDescription: (seconds: number): string => { + const { minutes, remainingSeconds } = secondsToMinutesAndRemainingSeconds(seconds); + + if (minutes === 0) { + const suffix = seconds > 1 ? 's' : ''; + return `${seconds} second${suffix}`; + } + + const minuteSuffix = minutes > 1 ? 's' : ''; + const minuteText = `${minutes} minute${minuteSuffix}`; + + const secondsSuffix = remainingSeconds > 1 ? 's' : ''; + const secondsText = remainingSeconds === 0 ? '' : ` ${remainingSeconds} second${secondsSuffix}`; + + return `${minuteText}${secondsText}`; + }, + // converts seconds to stopwatch time (i.e. 05:30 and 00:30) + // only goes up to minutes, not hours + secondsToStopwatchTime: (seconds: number): string => { + const { minutes, remainingSeconds } = secondsToMinutesAndRemainingSeconds(seconds); + return `${padZero(minutes)}:${padZero(remainingSeconds)}`; + }, +}; |