From e9b3668e0b3f8e1e493f1fc93ab220bc3ea4f939 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 8 Apr 2018 21:20:06 -0300 Subject: Add %timeLeft% template replacement --- src/loop-play.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/loop-play.js b/src/loop-play.js index f01ee5f..d5343a9 100644 --- a/src/loop-play.js +++ b/src/loop-play.js @@ -29,34 +29,42 @@ const { const { processTemplateString } = require('./general-util') function getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}) { - let timeDone, duration + // Multiplication casts to numbers; addition prioritizes strings. + // Thanks, JavaScript! + const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec) + const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec) + const percentVal = (100 / lenSecTotal) * curSecTotal + const percentDone = ( + (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%' + ) + + const leftSecTotal = lenSecTotal - curSecTotal + let leftHour = Math.floor(leftSecTotal / 3600) + let leftMin = Math.floor((leftSecTotal - leftHour * 3600) / 60) + let leftSec = Math.floor(leftSecTotal - leftHour * 3600 - leftMin * 60) const pad = val => val.toString().padStart(2, '0') curMin = pad(curMin) curSec = pad(curSec) lenMin = pad(lenMin) lenSec = pad(lenSec) + leftMin = pad(leftMin) + leftSec = pad(leftSec) // We don't want to display hour counters if the total length is less // than an hour. + let timeDone, timeLeft, duration if (parseInt(lenHour) > 0) { timeDone = `${curHour}:${curMin}:${curSec}` + timeLeft = `${leftHour}:${leftMin}:${leftSec}` duration = `${lenHour}:${lenMin}:${lenSec}` } else { timeDone = `${curMin}:${curSec}` + timeLeft = `${leftMin}:${leftSec}` duration = `${lenMin}:${lenSec}` } - // Multiplication casts to numbers; addition prioritizes strings. - // Thanks, JavaScript! - const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec) - const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec) - const percentVal = (100 / lenSecTotal) * curSecTotal - const percentDone = ( - (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%' - ) - - return {percentDone, timeDone, duration} + return {percentDone, timeDone, timeLeft, duration} } class Player extends EventEmitter { -- cgit 1.3.0-6-gf8a5