Permalink
Browse files

Prevent throwing an error when skipping too far.

If `bar.curr` is far enough past `bar.total`, `complete` could
be larger than `bar.width`. This would cause the `incomplete`
character count to be below 0, which is an invalid array length.
  • Loading branch information...
1 parent 78dbc92 commit b6e2d280e28b688b5ebe56b494610fcaae5d6a53 @hughsk hughsk committed with jdan Dec 27, 2013
Showing with 5 additions and 2 deletions.
  1. +5 −2 lib/node-progress.js
View
@@ -113,8 +113,11 @@ ProgressBar.prototype.render = function(tokens){
return;
}
- var percent = (this.curr >= this.total) ? 100 : this.curr / this.total * 100
- , complete = Math.round(this.width * (this.curr / this.total))
+ var ratio = this.curr / this.total;
+ ratio = Math.min(Math.max(ratio, 0), 1);
+
+ var percent = ratio * 100
+ , complete = Math.round(this.width * ratio)
, incomplete
, elapsed = new Date - this.start
, eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);

0 comments on commit b6e2d28

Please sign in to comment.