Permalink
Browse files

add value mapping support

  • Loading branch information...
1 parent c35ed90 commit c513b9bb635a639fe2da6c5cf30131c5a35e8ac2 @tj tj committed Feb 27, 2014
Showing with 59 additions and 7 deletions.
  1. +6 −0 History.md
  2. +20 −3 Readme.md
  3. +13 −1 examples/simple.js
  4. +10 −1 index.js
  5. +10 −2 package.json
View
@@ -0,0 +1,6 @@
+
+1.1.0 / 2014-02-26
+==================
+
+ * change to make .sort an option
+ * docs
View
@@ -4,15 +4,21 @@
Ascii histograms for node.
```
- ferrets | ############################################################ | 15
cats | ######################## | 6
+ ferrets | ############################################################ | 15
dogs | ######## | 2
koalas | | 0
+
dogs | ==================== | 30
ferrets | ============= | 20
cats | ======== | 12
koalas | == | 3
+
+
+ /data | ******************** | 150gb
+ /srv | * | 5gb
+ /etc | | 150mb
```
## Installation
@@ -24,12 +30,12 @@ $ npm install ascii-histogram
## Example
```js
-
var histogram = require('ascii-histogram');
+var bytes = require('bytes');
var data = {
- ferrets: 15,
cats: 6,
+ ferrets: 15,
dogs: 2,
koalas: 0
};
@@ -48,6 +54,17 @@ var data = {
console.log();
console.log(histogram(data, { bar: '=', width: 20, sort: true }));
+
+// value mapping
+
+var data = {
+ '/srv': bytes('5gb'),
+ '/data': bytes('150gb'),
+ '/etc': bytes('150mb')
+};
+
+console.log();
+console.log(histogram(data, { bar: '*', width: 20, sort: true, map: bytes }));
```
# License
View
@@ -1,5 +1,6 @@
var histogram = require('..');
+var bytes = require('bytes');
var data = {
cats: 6,
@@ -21,4 +22,15 @@ var data = {
};
console.log();
-console.log(histogram(data, { bar: '=', width: 20, sort: true }));
+console.log(histogram(data, { bar: '=', width: 20, sort: true }));
+
+// value mapping
+
+var data = {
+ '/srv': bytes('5gb'),
+ '/data': bytes('150gb'),
+ '/etc': bytes('150mb')
+};
+
+console.log();
+console.log(histogram(data, { bar: '*', width: 20, sort: true, map: bytes }));
View
@@ -27,6 +27,7 @@ function histogram(data, opts) {
var width = opts.width || 60;
var barc = opts.bar || '#';
+ var map = opts.map || noop;
// normalize data
@@ -46,7 +47,7 @@ function histogram(data, opts) {
var blank = width - shown
var bar = Array(shown + 1).join(barc);
bar += Array(blank + 1).join(' ');
- str += fmt(' %*s | %s | %s\n', d.key, maxKey, bar, d.val);
+ str += fmt(' %*s | %s | %s\n', d.key, maxKey, bar, map(d.val));
}
return str;
@@ -85,4 +86,12 @@ function toArray(obj) {
val: obj[key]
}
})
+}
+
+/**
+ * Noop map function.
+ */
+
+function noop(val) {
+ return val;
}
View
@@ -3,13 +3,21 @@
"version": "1.1.0",
"repository": "visionmedia/node-ascii-histogram",
"description": "Ascii histograms",
- "keywords": ["ascii", "histogram", "stats", "data", "cli", "terminal"],
+ "keywords": [
+ "ascii",
+ "histogram",
+ "stats",
+ "data",
+ "cli",
+ "terminal"
+ ],
"dependencies": {
"printf": "~0.1.2"
},
"devDependencies": {
"mocha": "*",
- "should": "*"
+ "should": "*",
+ "bytes": "~0.2.1"
},
"license": "MIT"
}

0 comments on commit c513b9b

Please sign in to comment.