-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
40 lines (31 loc) · 1.06 KB
/
index.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
40
'use strict';
var isFinite = require('is-finite');
var colors = require('ansi-256-colors');
module.exports = function (numbers, opts) {
if (!Array.isArray(numbers)) {
throw new TypeError('Expected an array');
}
opts = opts || {};
var ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
var color = [[5, 5, 4], [5, 5, 3], [5, 5, 0], [5, 4, 0], [5, 3, 0], [5, 2, 0], [5, 1, 0], [5, 0, 0]];
var finiteNumbers = numbers.filter(isFinite);
var min = typeof opts.min === 'number' ? opts.min : Math.min.apply(null, finiteNumbers);
var max = typeof opts.max === 'number' ? opts.max : Math.max.apply(null, finiteNumbers);
// use a high tick if data is constant
if (min === max) {
ticks = [ticks[4]];
}
return numbers.map(function (el) {
if (!isFinite(el)) {
return ' ';
}
var tickIndex = Math.ceil((el / max) * ticks.length) - 1;
if (tickIndex < 0) {
tickIndex = 0;
}
if (opts.style === 'fire') {
return colors.fg.getRgb.apply(colors.fg, color[tickIndex]) + ticks[tickIndex] + colors.reset;
}
return ticks[tickIndex];
}).join('');
};