Skip to content

Commit ca3c60d

Browse files
BendingBendersindresorhus
authored andcommittedApr 28, 2019
Require Node.js 8, add TypeScript definition (#14)
1 parent 829b3cb commit ca3c60d

7 files changed

+78
-22
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3+
- '12'
34
- '10'
45
- '8'
5-
- '6'

‎index.d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
declare namespace sparkly {
2+
interface Options {
3+
/**
4+
Minimum range.
5+
*/
6+
readonly minimum?: number;
7+
8+
/**
9+
Maximum range.
10+
*/
11+
readonly maximum?: number;
12+
13+
/**
14+
Style for the sparklines.
15+
*/
16+
readonly style?: 'fire';
17+
}
18+
}
19+
20+
/**
21+
Generate sparklines `▁▂▃▅▂▇`.
22+
23+
@param numbers - Numbers to create the sparkline from.
24+
25+
@example
26+
```
27+
const sparkly = require('sparkly');
28+
29+
sparkly([0, 3, 5, 8, 4, 3, 4, 10]);
30+
//=> '▁▃▄▇▄▃▄█'
31+
32+
// Specifying anything other than finite numbers will cause holes
33+
sparkly([0, 3, 5, '', 4, 3, 4, 10]);
34+
//=> '▁▃▄ ▄▃▄█'
35+
36+
// Specifying an object with minimum and maximum options will change the sparkline range
37+
sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10});
38+
//=> '▁▂▃▄▄'
39+
```
40+
*/
41+
declare function sparkly(
42+
numbers: ReadonlyArray<number | ''>,
43+
options?: sparkly.Options
44+
): string;
45+
46+
export = sparkly;

‎index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module.exports = (numbers, options = {}) => {
99
let ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
1010
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]];
1111
const finiteNumbers = numbers.filter(number => Number.isFinite(number));
12-
const min = typeof options.min === 'number' ? options.min : Math.min(...finiteNumbers);
13-
const max = typeof options.max === 'number' ? options.max : Math.max(...finiteNumbers);
12+
const minimum = typeof options.minimum === 'number' ? options.minimum : Math.min(...finiteNumbers);
13+
const maximum = typeof options.maximum === 'number' ? options.maximum : Math.max(...finiteNumbers);
1414

1515
// Use a high tick if data is constant and max is not equal to 0
16-
if (min === max && max !== 0) {
16+
if (minimum === maximum && maximum !== 0) {
1717
ticks = [ticks[4]];
1818
}
1919

@@ -22,9 +22,9 @@ module.exports = (numbers, options = {}) => {
2222
return ' ';
2323
}
2424

25-
let tickIndex = Math.ceil((el / max) * ticks.length) - 1;
25+
let tickIndex = Math.ceil((el / maximum) * ticks.length) - 1;
2626

27-
if (max === 0 || tickIndex < 0) {
27+
if (maximum === 0 || tickIndex < 0) {
2828
tickIndex = 0;
2929
}
3030

‎index.test-d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {expectType} from 'tsd';
2+
import sparkly = require('.');
3+
4+
expectType<string>(sparkly([0, 3, 5, 8, 4, 3, 4, 10]));
5+
expectType<string>(sparkly([0, 3, 5, '', 4, 3, 4, 10]));
6+
expectType<string>(sparkly([1, 2, 3, 4, 5], {minimum: 0}));
7+
expectType<string>(sparkly([1, 2, 3, 4, 5], {maximum: 10}));
8+
expectType<string>(sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}));

‎package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"spark",
@@ -31,10 +32,11 @@
3132
"ascii"
3233
],
3334
"dependencies": {
34-
"chalk": "^2.4.1"
35+
"chalk": "^2.4.2"
3536
},
3637
"devDependencies": {
37-
"ava": "*",
38-
"xo": "*"
38+
"ava": "^1.4.1",
39+
"tsd": "^0.7.2",
40+
"xo": "^0.24.0"
3941
}
4042
}

‎readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ sparkly([0, 3, 5, 8, 4, 3, 4, 10]);
2626
sparkly([0, 3, 5, '', 4, 3, 4, 10]);
2727
//=> '▁▃▄ ▄▃▄█'
2828

29-
// Specifying a min max object will change the sparkline range
30-
sparkly([1, 2, 3, 4, 5], {min: 0, max: 10});
29+
// Specifying an object with minimum and maximum options will change the sparkline range
30+
sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10});
3131
//=> '▁▂▃▄▄'
3232

3333
// Specifying a style option will change the sparkline color
@@ -50,15 +50,15 @@ Numbers to create the sparkline from.
5050

5151
#### options
5252

53-
Type: `Object`
53+
Type: `object`
5454

55-
##### min
55+
##### minimum
5656

5757
Type: `number`
5858

5959
Minimum range.
6060

61-
##### max
61+
##### maximum
6262

6363
Type: `number`
6464

‎test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ test('use the lowest tick if data is all 0', t => {
3030
t.is(sparkly([0, 0, 0, 0, 0]), '▁▁▁▁▁');
3131
});
3232

33-
test('min and max arguments set graph range', t => {
34-
t.is(sparkly([1], {min: 0, max: 1}), '█');
35-
t.is(sparkly([1, 2, 3, 4, 5], {min: 0, max: 10}), '▁▂▃▄▄');
36-
t.is(sparkly([10, 11, 12, 13], {min: 0}), '▇▇██');
37-
t.is(sparkly([10, 20, 30, 40, 50], {max: 100}), '▁▂▃▄▄');
33+
test('minimum and maximum arguments set graph range', t => {
34+
t.is(sparkly([1], {minimum: 0, maximum: 1}), '█');
35+
t.is(sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}), '▁▂▃▄▄');
36+
t.is(sparkly([10, 11, 12, 13], {minimum: 0}), '▇▇██');
37+
t.is(sparkly([10, 20, 30, 40, 50], {maximum: 100}), '▁▂▃▄▄');
3838
});
3939

4040
test('colored graph', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.