@vbarbarosh/node-helpers 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/progress.js +37 -0
package/package.json
CHANGED
package/src/progress.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// - update progress by delta
|
|
2
|
+
// - update progress by replacing total amount
|
|
3
|
+
// - render eta
|
|
4
|
+
// - render items/second
|
|
5
|
+
// - render percentage
|
|
6
|
+
// - time spent
|
|
7
|
+
function progress(total)
|
|
8
|
+
{
|
|
9
|
+
let done = 0;
|
|
10
|
+
const time0 = Date.now();
|
|
11
|
+
const out = {
|
|
12
|
+
done: 0,
|
|
13
|
+
total,
|
|
14
|
+
eta: null,
|
|
15
|
+
rate: null,
|
|
16
|
+
duration: 0,
|
|
17
|
+
progress: null,
|
|
18
|
+
percentage: null,
|
|
19
|
+
add: function (delta = 0) {
|
|
20
|
+
out.done += delta;
|
|
21
|
+
out.refresh();
|
|
22
|
+
},
|
|
23
|
+
update: function (done) {
|
|
24
|
+
out.done = done;
|
|
25
|
+
out.refresh();
|
|
26
|
+
},
|
|
27
|
+
refresh: function () {
|
|
28
|
+
out.duration = (Date.now() - time0)/1000;
|
|
29
|
+
out.rate = out.done/out.duration;
|
|
30
|
+
out.percentage = !out.total ? null : out.done/out.total;
|
|
31
|
+
out.eta = !out.total ? null : (out.total - out.done)/out.rate;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = progress;
|