forked from msiebuhr/node-textspark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spark.js
39 lines (31 loc) · 1.08 KB
/
spark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* ASCII sparklines.
*/
(function(root) {
var ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
var spark = function (ints) {
// Ignore impossible numbers when making graphs
var pureNumbers = ints.filter(function (num) {
return (!isNaN(num)) && (!(num === Infinity || num === -Infinity));
});
// Find min and max
var max = Math.max.apply(null, pureNumbers),
min = Math.min.apply(null, pureNumbers);
// Convert to chars
var steps = ints.map(function (tick) {
if (isNaN(tick)) { return " "; }
if (tick === Infinity || tick === -Infinity) { return "∞"; }
var index = Math.round((tick - min) / max * (ticks.length -1));
return ticks[index];
});
return steps.join("");
}
if(typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
module.exports = spark;
}
exports.spark = spark;
} else {
root.spark = spark;
}
})(this || window);