From 1f5478d8d8d350cde49f52b9305977fcc66c9fae Mon Sep 17 00:00:00 2001 From: fredgan Date: Fri, 11 Dec 2020 11:49:36 +0800 Subject: [PATCH] Fix:setting `curr` breaks `:eta`,`:rate`,`:elapsed` Setting `curr` property breaks `:eta`, `:rate`, `:elapsed`. For example, the input `curr` value is 2, but the output `:eta`, `:rate`, `:elapsed` values respectively are 0.0, NaN, 0.0. Current required nature number and is less than or equal to total. Closes visionmedia#159 --- lib/node-progress.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..400be28 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -53,6 +53,9 @@ function ProgressBar(fmt, options) { options = options || {}; if ('string' != typeof fmt) throw new Error('format required'); if ('number' != typeof options.total) throw new Error('total required'); + if (options.curr % 1 !== 0 || options.curr < 0 || options.curr >= options.total) { + throw new Error('current required nature number and is less than or equal to total'); + } } this.fmt = fmt; @@ -70,6 +73,10 @@ function ProgressBar(fmt, options) { this.callback = options.callback || function () {}; this.tokens = {}; this.lastDraw = ''; + // start time for eta + if (this.curr >= 0 && this.curr <= this.total) { + this.start = new Date; + } } /** @@ -88,9 +95,6 @@ ProgressBar.prototype.tick = function(len, tokens){ if ('object' == typeof len) tokens = len, len = 1; if (tokens) this.tokens = tokens; - // start time for eta - if (0 == this.curr) this.start = new Date; - this.curr += len // try to render