« get me outta code hell

Metadata (in memory) - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/general-util.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-02-25 11:27:11 -0400
committerFlorrie <towerofnix@gmail.com>2019-02-25 11:27:11 -0400
commit75251bb2309505c20dc7500117a17649d41412d8 (patch)
tree97c646cb2ccb224a456147aaec8c4a5854990bb5 /general-util.js
parent4eee44f7d92dcdf2c8bccea78723ba0b194eb2d7 (diff)
Metadata (in memory)
Diffstat (limited to 'general-util.js')
-rw-r--r--general-util.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/general-util.js b/general-util.js
index 0b9f081..708e150 100644
--- a/general-util.js
+++ b/general-util.js
@@ -92,3 +92,88 @@ module.exports.shuffleArray = function(array) {
   return workingArray
 }
 
+module.exports.throttlePromise = function(maximumAtOneTime = 10) {
+  // Returns a function that takes a callback to create a promise and either
+  // runs it now, if there is an available slot, or enqueues it to be run
+  // later, if there is not.
+
+  let activeCount = 0
+  const queue = []
+
+  const execute = function(callback) {
+    activeCount++
+    return callback().finally(() => {
+      activeCount--
+
+      if (queue.length) {
+        return execute(queue.shift())
+      }
+    })
+  }
+
+  return function(callback) {
+    if (activeCount >= maximumAtOneTime) {
+      return new Promise((resolve, reject) => {
+        queue.push(function() {
+          return callback().then(resolve, reject)
+        })
+      })
+    } else {
+      return execute(callback)
+    }
+  }
+}
+
+module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal) {
+  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)
+
+  // Yeah, yeah, duplicate math.
+  let curHour = Math.floor(curSecTotal / 3600)
+  let curMin = Math.floor((curSecTotal - curHour * 3600) / 60)
+  let curSec = Math.floor(curSecTotal - curHour * 3600 - curMin * 60)
+
+  // Wee!
+  let lenHour = Math.floor(lenSecTotal / 3600)
+  let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60)
+  let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 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}`
+  }
+
+  return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal}
+}
+
+module.exports.getTimeStrings = function({curHour, curMin, curSec, lenHour, 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)
+
+  return module.exports.getTimeStringsFromSec(curSecTotal, lenSecTotal)
+}