From fe514359b182818abb7b01dea4bdd6da831c6d29 Mon Sep 17 00:00:00 2001 From: fredgan Date: Fri, 11 Dec 2020 11:55:14 +0800 Subject: [PATCH] Fix: modified `:total` type to positive integer The input `:total` value is float, but the output `:current` value is more than `:total` value. The input `:total` is nagative integer or zero, but the output rendering failed. Closes visionmedia#177, #166 --- lib/node-progress.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..417b74c 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -1,3 +1,4 @@ + /*! * node-progress * Copyright(c) 2011 TJ Holowaychuk @@ -52,8 +53,10 @@ function ProgressBar(fmt, options) { } else { options = options || {}; if ('string' != typeof fmt) throw new Error('format required'); - if ('number' != typeof options.total) throw new Error('total required'); - } + if (options.total % 1 !== 0 || options.total <= 0) { + throw new Error('format required positive integer'); + } + }s this.fmt = fmt; this.curr = options.curr || 0; @@ -154,7 +157,7 @@ ProgressBar.prototype.render = function (tokens, force) { } var width = Math.min(this.width, availableSpace); - + width = Math.floor(width); /* TODO: the following assumes the user has one ':bar' token */ completeLength = Math.round(width * ratio); complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);