From 5d7b838a030f8bdab66c5b8a48cc30c9b23b6737 Mon Sep 17 00:00:00 2001 From: Florrie Date: Fri, 6 Mar 2020 16:59:52 -0400 Subject: a whole heckin lot of stuff but mainly lofam1/2 --- upd8-util.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'upd8-util.js') 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]; + }; +}; -- cgit 1.3.0-6-gf8a5