-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
36 lines (28 loc) · 1.09 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
import chalk from 'chalk';
export default function sparkly(numbers, options = {}) {
if (!Array.isArray(numbers)) {
throw new TypeError('Expected an array');
}
let ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
const 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]];
const finiteNumbers = numbers.filter(number => Number.isFinite(number));
const minimum = typeof options.minimum === 'number' ? options.minimum : Math.min(...finiteNumbers);
const maximum = typeof options.maximum === 'number' ? options.maximum : Math.max(...finiteNumbers);
// Use a high tick if data is constant and max is not equal to 0.
if (minimum === maximum && maximum !== 0) {
ticks = [ticks[4]];
}
return numbers.map(number => {
if (!Number.isFinite(number)) {
return ' ';
}
let tickIndex = Math.ceil((number / maximum) * ticks.length) - 1;
if (maximum === 0 || tickIndex < 0) {
tickIndex = 0;
}
if (options.style === 'fire') {
return chalk.rgb(...color[tickIndex])(ticks[tickIndex]);
}
return ticks[tickIndex];
}).join('');
}