Skip to content

Commit

Permalink
fix jstrace#2 jstrace#4 Handle zero values.
Browse files Browse the repository at this point in the history
  • Loading branch information
watsoncj committed Feb 10, 2017
1 parent c74a0e5 commit 788b46f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ function histogram(data, opts) {
if (opts.sort) data = data.sort(descending);

var maxKey = max(data.map(function(d){ return d.key.length }));
var maxVal = max(data.map(function(d){ return d.val }));
var maxVal = max(data.map(function(d){ return d.val })) || width;
var str = '';

// blah blah histo

for (var i = 0; i < data.length; i++) {
var d = data[i];
var p = (d.val / maxVal) || 1;
var p = d.val / maxVal;
var shown = Math.round(width * p);
var blank = width - shown
var bar = Array(shown + 1).join(barc);
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"should": "*",
"bytes": "~0.2.1"
},
"license": "MIT"
}
"license": "MIT",
"scripts": {
"test": "node test.js"
}
}
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var assert = require('assert');
var bars = require('./index');

var options = {width: 10};

var zeros = {a: 0, b: 0};
var zerosExpected = '\
a | | 0\n\
b | | 0\n\
';
assert.equal(bars(zeros, options), zerosExpected, 'it should handle all zeros');

var zeroFive = {a: 0, b: 5};
var zeroFiveExpected = '\
a | | 0\n\
b | ########## | 5\n\
';
assert.equal(bars(zeroFive, options), zeroFiveExpected, 'it should handle a single zero');

console.log('PASS');

0 comments on commit 788b46f

Please sign in to comment.