« get me outta code hell

a whole heckin lot of stuff but mainly lofam1/2 - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/upd8-util.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-03-06 16:59:52 -0400
committerFlorrie <towerofnix@gmail.com>2020-03-06 16:59:52 -0400
commit5d7b838a030f8bdab66c5b8a48cc30c9b23b6737 (patch)
tree973721cc5d33f8f2713316f6e450701f283a99bc /upd8-util.js
parentf665b4de4f30484398bdc9986b0383802d3652a8 (diff)
a whole heckin lot of stuff but mainly lofam1/2
Diffstat (limited to 'upd8-util.js')
-rw-r--r--upd8-util.js50
1 files changed, 48 insertions, 2 deletions
diff --git a/upd8-util.js b/upd8-util.js
index cd0beed..82200ee 100644
--- a/upd8-util.js
+++ b/upd8-util.js
@@ -47,17 +47,44 @@ module.exports.joinNoOxford = function(array, plural = 'and') {
 module.exports.progressPromiseAll = function (msg, array) {
     let done = 0, total = array.length;
     process.stdout.write(`\r${msg} [0/${total}]`);
+    const start = Date.now();
     return Promise.all(array.map(promise => promise.then(val => {
         done++;
+        // const pc = `${done}/${total}`;
+        const pc = (Math.round(done / total * 1000) / 10 + '%').padEnd('99.9%'.length, ' ');
         if (done === total) {
-            process.stdout.write(`\r\x1b[2m${msg} [${done}/${total}] \x1b[0;32mDone!\x1b[0m \n`)
+            const time = Date.now() - start;
+            process.stdout.write(`\r\x1b[2m${msg} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`)
         } else {
-            process.stdout.write(`\r${msg} [${done}/${total}]`);
+            process.stdout.write(`\r${msg} [${pc}] `);
         }
         return val;
     })));
 };
 
+module.exports.queue = function (array, max = 10) {
+    const begin = [];
+    let current = 0;
+    const ret = array.map(fn => new Promise((resolve, reject) => {
+        begin.push(() => {
+            current++;
+            Promise.resolve(fn()).then(value => {
+                current--;
+                if (current < max && begin.length) {
+                    begin.shift()();
+                }
+                resolve(value);
+            }, reject);
+        });
+    }));
+
+    for (let i = 0; i < max && begin.length; i++) {
+        begin.shift()();
+    }
+
+    return ret;
+};
+
 module.exports.th = function (n) {
     if (n % 10 === 1 && n !== 11) {
         return n + 'st';
@@ -74,3 +101,22 @@ module.exports.th = function (n) {
 module.exports.s = function (n, word) {
     return `${n} ${word}` + (n === 1 ? '' : 's');
 };
+
+// Hey, did you know I apparently put a space 8efore the parameters in function
+// names? 8ut only in function expressions, not declar8tions? I mean, I guess
+// you did. You're pro8a8ly more familiar with my code than I am 8y this
+// point. I haven't messed with any of this code in ages. Yay!!!!!!!!
+//
+// This function only does anything on o8jects you're going to 8e reusing.
+// Argua8ly I could use a WeakMap here, 8ut since the o8ject needs to 8e
+// reused to 8e useful anyway, I just store the result with a symbol.
+// Sorry if it's 8een frozen I guess??
+module.exports.cacheOneArg = function (fn) {
+    const symbol = Symbol('Cache');
+    return arg => {
+        if (!arg[symbol]) {
+            arg[symbol] = fn(arg);
+        }
+        return arg[symbol];
+    };
+};