diff --git a/Readme.md b/Readme.md index 532ba29..05a07c9 100644 --- a/Readme.md +++ b/Readme.md @@ -65,8 +65,21 @@ var data = { console.log(); console.log(bars(data, { bar: '*', width: 20, sort: true, map: bytes })); + +// force order + +var data = { + ferrets: 20, + cats: 12, + dogs: 30, + koalas: 3 +}; + +console.log(); +console.log(bars(data, { keys: ['cats', 'dogs', 'ferrets'] })); ``` + # License MIT \ No newline at end of file diff --git a/index.js b/index.js index 67df5ee..694d925 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ function histogram(data, opts) { // normalize data - var data = toArray(data); + var data = toArray(data, opts.keys || Object.keys(data)); if (opts.sort) data = data.sort(descending); var maxKey = max(data.map(function(d){ return d.key.length })); @@ -79,8 +79,8 @@ function max(data) { * Turn object into an array. */ -function toArray(obj) { - return Object.keys(obj).map(function(key){ +function toArray(obj, keys) { + return keys.map(function(key){ return { key: key, val: obj[key] diff --git a/package.json b/package.json index bd1ee3b..474d476 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ }, "license": "MIT", "scripts": { - "test": "node test.js" + "test": "mocha test.js" } } diff --git a/test.js b/test.js index 90c827e..9ad2a91 100644 --- a/test.js +++ b/test.js @@ -1,20 +1,79 @@ -var assert = require('assert'); +var should = require('should'); var bars = require('./index'); +var bytes = require('bytes'); -var options = {width: 10}; +describe('bars', function () { -var zeros = {a: 0, b: 0}; -var zerosExpected = '\ + it('it should display bars', function () { + var data = { aa: 10, bbbbb: 999, c: 88 }; + var expected = '\ + aa | | 10\n\ + bbbbb | ########## | 999\n\ + c | # | 88\n\ +'; + bars(data, { width: 10 }).should.equal(expected); + }); + + it('it should customize output', function () { + var data = { d: 1, e: 6 }; + var expected = '\ + d | === | 1\n\ + e | =============== | 6\n\ +'; + bars(data, { width: 15, bar: '=' }).should.equal(expected); + }); + + it('it should sort output', function () { + var data = { d: 1, e: 6, f: 3 }; + var expected = '\ + e | ########## | 6\n\ + f | ##### | 3\n\ + d | ## | 1\n\ +'; + bars(data, { width: 10, sort: true }).should.equal(expected); + }); + + it('it should use keys in order', function () { + var data = { d: 1, e: 6, '0': 7 }; + var expected = '\ + d | # | 1\n\ + e | ######### | 6\n\ + 0 | ########## | 7\n\ +'; + bars(data, { width: 10, keys: ['d', 'e', '0'] }).should.equal(expected); + }); + + it('it should map values', function () { + var data = { + '/srv': bytes('5gb'), + '/data': bytes('150gb'), + '/etc': bytes('150mb') + }; + var expected = '\ + /data | ******************** | 150gb\n\ + /srv | * | 5gb\n\ + /etc | | 150mb\n\ +'; + bars(data, { bar: '*', width: 20, sort: true, map: bytes }).should.equal(expected); + }); + + + it('it should handle all zeros', function () { + var data = { a: 0, b: 0 }; + var expected = '\ a | | 0\n\ b | | 0\n\ '; -assert.equal(bars(zeros, options), zerosExpected, 'it should handle all zeros'); + bars(data, { width: 10 }).should.equal(expected); + }); -var zeroFive = {a: 0, b: 5}; -var zeroFiveExpected = '\ + it('it should handle a single zero', function () { + var data = { a: 0, b: 5 }; + var expected = '\ a | | 0\n\ b | ########## | 5\n\ '; -assert.equal(bars(zeroFive, options), zeroFiveExpected, 'it should handle a single zero'); + bars(data, { width: 10 }).should.equal(expected); + }); -console.log('PASS'); +}); \ No newline at end of file