diff options
Diffstat (limited to 'general-util.js')
-rw-r--r-- | general-util.js | 122 |
1 files changed, 81 insertions, 41 deletions
diff --git a/general-util.js b/general-util.js index b767a1b..78843f9 100644 --- a/general-util.js +++ b/general-util.js @@ -153,62 +153,77 @@ export function getSecFromTimestamp(timestamp) { } } -export function getTimeStringsFromSec(curSecTotal, lenSecTotal, fraction = false) { - 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) - let leftFrac = lenSecTotal % 1 - - // Yeah, yeah, duplicate math. +export function getTimeStringsFromSec(curSecTotal, lenSecTotal = null, fraction = false) { + const pad = val => val.toString().padStart(2, '0') + const padFrac = val => Math.floor(val * 1000).toString().padEnd(3, '0') + + // We don't want to display hour counters if the total length is less + // than an hour. + const displayAsHours = Math.max(curSecTotal, lenSecTotal ?? 0) >= 3600 + + const strings = {curSecTotal, lenSecTotal} + let curHour = Math.floor(curSecTotal / 3600) let curMin = Math.floor((curSecTotal - curHour * 3600) / 60) let curSec = Math.floor(curSecTotal - curHour * 3600 - curMin * 60) let curFrac = curSecTotal % 1 - // Wee! - let lenHour = Math.floor(lenSecTotal / 3600) - let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60) - let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 60) - let lenFrac = lenSecTotal % 1 - - const pad = val => val.toString().padStart(2, '0') - const padFrac = val => Math.floor(val * 1000).toString().padEnd(3, '0') curMin = pad(curMin) curSec = pad(curSec) - lenMin = pad(lenMin) - lenSec = pad(lenSec) - leftMin = pad(leftMin) - leftSec = pad(leftSec) curFrac = padFrac(curFrac) - lenFrac = padFrac(lenFrac) - leftFrac = padFrac(leftFrac) - // 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 || parseInt(curHour) > 0) { - timeDone = `${curHour}:${curMin}:${curSec}` - timeLeft = `${leftHour}:${leftMin}:${leftSec}` - duration = `${lenHour}:${lenMin}:${lenSec}` + if (displayAsHours) { + strings.timeDone = `${curHour}:${curMin}:${curSec}` } else { - timeDone = `${curMin}:${curSec}` - timeLeft = `${leftMin}:${leftSec}` - duration = `${lenMin}:${lenSec}` + strings.timeDone = `${curMin}:${curSec}` } if (fraction) { - timeDone += '.' + curFrac - timeLeft += '.' + leftFrac - duration += '.' + lenFrac + strings.timeDone += '.' + curFrac + } + + if (typeof lenSecTotal === 'number') { + const percentVal = (100 / lenSecTotal) * curSecTotal + strings.percentDone = (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%' + + // Yeah, yeah, duplicate math. + 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) + let leftFrac = leftSecTotal % 1 + + // Wee! + let lenHour = Math.floor(lenSecTotal / 3600) + let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60) + let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 60) + let lenFrac = lenSecTotal % 1 + + lenMin = pad(lenMin) + lenSec = pad(lenSec) + lenFrac = padFrac(lenFrac) + + leftMin = pad(leftMin) + leftSec = pad(leftSec) + leftFrac = padFrac(leftFrac) + + if (typeof lenSecTotal === 'number') { + if (displayAsHours) { + strings.timeLeft = `${leftHour}:${leftMin}:${leftSec}` + strings.duration = `${lenHour}:${lenMin}:${lenSec}` + } else { + strings.timeLeft = `${leftMin}:${leftSec}` + strings.duration = `${lenMin}:${lenSec}` + } + + if (fraction) { + strings.timeLeft += '.' + leftFrac + strings.duration += '.' + lenFrac + } + } } - return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal} + return strings } export function getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}) { @@ -335,3 +350,28 @@ export async function parseOptions(options, optionDescriptorMap) { } parseOptions.handleDashless = Symbol() + +export async function silenceEvents(emitter, eventsToSilence, callback) { + const oldEmit = emitter.emit + + emitter.emit = function(event, ...data) { + if (!eventsToSilence.includes(event)) { + oldEmit.apply(emitter, [event, ...data]) + } + } + + await callback() + + emitter.emit = oldEmit +} + +// Kindly stolen from ESDiscuss: +// https://esdiscuss.org/topic/proposal-add-an-option-to-omit-prototype-of-objects-created-by-json-parse#content-1 +export function parseWithoutPrototype(string) { + return JSON.parse(string, function(k, v) { + if (v && typeof v === 'object' && !Array.isArray(v)) { + return Object.assign(Object.create(null), v) + } + return v + }) +} |