├── .npmrc ├── .gitattributes ├── .gitignore ├── screenshot.png ├── .editorconfig ├── index.test-d.ts ├── .github └── workflows │ └── main.yml ├── package.json ├── license ├── index.js ├── test.js ├── index.d.ts └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/sparkly/HEAD/screenshot.png -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import sparkly from './index.js'; 3 | 4 | expectType(sparkly([0, 3, 5, 8, 4, 3, 4, 10])); 5 | expectType(sparkly([0, 3, 5, '', 4, 3, 4, 10])); 6 | expectType(sparkly([1, 2, 3, 4, 5], {minimum: 0})); 7 | expectType(sparkly([1, 2, 3, 4, 5], {maximum: 10})); 8 | expectType(sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'})); 9 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v5 18 | - uses: actions/setup-node@v5 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sparkly", 3 | "version": "6.0.1", 4 | "description": "Generate sparklines `▁▂▃▅▂▇`", 5 | "license": "MIT", 6 | "repository": "sindresorhus/sparkly", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 19 | }, 20 | "scripts": { 21 | "//test": "xo && ava && tsd", 22 | "test": "ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts" 27 | ], 28 | "keywords": [ 29 | "spark", 30 | "sparkly", 31 | "line", 32 | "sparkline", 33 | "sparklines", 34 | "unicode", 35 | "data", 36 | "graph", 37 | "plot", 38 | "ascii" 39 | ], 40 | "dependencies": { 41 | "chalk": "^4.1.2" 42 | }, 43 | "devDependencies": { 44 | "ava": "^3.15.0", 45 | "tsd": "^0.19.0", 46 | "xo": "^0.46.4" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import chalk from 'chalk'; 2 | 3 | export default function sparkly(numbers, options = {}) { 4 | if (!Array.isArray(numbers)) { 5 | throw new TypeError('Expected an array'); 6 | } 7 | 8 | let ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']; 9 | 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]]; 10 | const finiteNumbers = numbers.filter(number => Number.isFinite(number)); 11 | const minimum = typeof options.minimum === 'number' ? options.minimum : (typeof options.maximum === 'number' ? 0 : Math.min(...finiteNumbers)); 12 | const maximum = typeof options.maximum === 'number' ? options.maximum : Math.max(...finiteNumbers); 13 | 14 | // Use a high tick if data is constant and max is not equal to 0. 15 | if (minimum === maximum && maximum !== 0) { 16 | ticks = [ticks[4]]; 17 | } 18 | 19 | return numbers.map(number => { 20 | if (!Number.isFinite(number)) { 21 | return ' '; 22 | } 23 | 24 | let tickIndex; 25 | 26 | if (minimum === maximum) { 27 | tickIndex = 0; 28 | } else { 29 | tickIndex = Math.ceil(((number - minimum) / (maximum - minimum)) * ticks.length) - 1; 30 | } 31 | 32 | if (tickIndex < 0) { 33 | tickIndex = 0; 34 | } 35 | 36 | if (options.style === 'fire') { 37 | return chalk.rgb(...color[tickIndex])(ticks[tickIndex]); 38 | } 39 | 40 | return ticks[tickIndex]; 41 | }).join(''); 42 | } 43 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import chalk from 'chalk'; 3 | import sparkly from './index.js'; 4 | 5 | chalk.level = 3; 6 | 7 | // Example 8 | console.log(' ' + sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}) + '\n'); 9 | 10 | test('creates graph', t => { 11 | t.is(sparkly([1, 5, 22, 13, 5]), '▁▂█▅▂'); 12 | t.is(sparkly([0, 30, 55, 80, 33, 150]), '▁▂▃▅▂█'); 13 | t.is(sparkly([5.5, 20]), '▁█'); 14 | t.is(sparkly([1, 2, 3, 4, 100, 5, 10, 20, 50, 300]), '▁▁▁▁▃▁▁▁▂█'); 15 | t.is(sparkly([1, 50, 100]), '▁▄█'); 16 | t.is(sparkly([2, 4, 8]), '▁▃█'); 17 | t.is(sparkly([1, 2, 3, 4, 5]), '▁▂▄▆█'); 18 | t.is(sparkly([25, 45]), '▁█'); 19 | }); 20 | 21 | test('anything other than finite numbers causes holes', t => { 22 | t.is(sparkly([1, '', 3, Number.NaN, 5]), '▁ ▄ █'); 23 | }); 24 | 25 | test('use the middle tick if data is constant', t => { 26 | t.is(sparkly([10, 10, 10, 10, 10]), '▅▅▅▅▅'); 27 | }); 28 | 29 | test('use the lowest tick if data is all 0', t => { 30 | t.is(sparkly([0, 0, 0, 0, 0]), '▁▁▁▁▁'); 31 | }); 32 | 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}), '▁▂▃▄▄'); 38 | }); 39 | 40 | test('colored graph', t => { 41 | t.is(sparkly([1], {style: 'fire'}), chalk.rgb(5, 5, 4)('▅')); 42 | }); 43 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | interface Options { 2 | /** 3 | Minimum value of the sparkline range. 4 | 5 | Values are scaled relative to this baseline. When not specified: 6 | - If `maximum` is set, defaults to `0` (for backwards compatibility) 7 | - Otherwise, defaults to the minimum value in the data 8 | */ 9 | readonly minimum?: number; 10 | 11 | /** 12 | Maximum value of the sparkline range. 13 | 14 | Values are scaled relative to this maximum. When not specified, defaults to the maximum value in the data. 15 | */ 16 | readonly maximum?: number; 17 | 18 | /** 19 | Apply color styling to the sparklines. 20 | 21 | The `'fire'` style uses a gradient from yellow to red. Each bar has a fixed width of one terminal column. 22 | 23 | @default 'fire' 24 | */ 25 | readonly style?: 'fire'; 26 | } 27 | 28 | /** 29 | Generate sparklines `▁▂▃▅▂▇`. 30 | 31 | @param numbers - The numbers to create the sparkline from. 32 | 33 | @example 34 | ``` 35 | import sparkly from 'sparkly'; 36 | 37 | sparkly([0, 3, 5, 8, 4, 3, 4, 10]); 38 | //=> '▁▃▄▇▄▃▄█' 39 | 40 | // Specifying anything other than finite numbers will cause holes 41 | sparkly([0, 3, 5, '', 4, 3, 4, 10]); 42 | //=> '▁▃▄ ▄▃▄█' 43 | 44 | // Specifying minimum and/or maximum options will change the sparkline range 45 | sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}); 46 | //=> '▁▂▃▄▄' 47 | 48 | // With only maximum set, minimum defaults to 0 for backwards compatibility 49 | sparkly([10, 20, 30, 40, 50], {maximum: 100}); 50 | //=> '▁▂▃▄▄' 51 | 52 | // Specifying a style option will change the sparkline color 53 | sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}); 54 | //=> Colored sparkline (gradient from yellow to red) 55 | ``` 56 | */ 57 | export default function sparkly( 58 | numbers: ReadonlyArray, 59 | options?: Options 60 | ): string; 61 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ![sparkly](https://cloud.githubusercontent.com/assets/170270/4068189/1b47cab0-2e36-11e4-8b75-16b80330147e.gif) 2 | 3 | > Generate sparklines `▁▂▃▅▂▇` 4 | 5 | JavaScript port of [spark.sh](https://github.com/holman/spark). 6 | 7 | [Some cool use-cases.](https://github.com/holman/spark/wiki/Wicked-Cool-Usage) 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install sparkly 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import sparkly from 'sparkly'; 19 | 20 | sparkly([0, 3, 5, 8, 4, 3, 4, 10]); 21 | //=> '▁▃▄▇▄▃▄█' 22 | 23 | // Specifying anything other than finite numbers will cause holes 24 | sparkly([0, 3, 5, '', 4, 3, 4, 10]); 25 | //=> '▁▃▄ ▄▃▄█' 26 | 27 | // Specifying minimum and/or maximum options will change the sparkline range 28 | sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}); 29 | //=> '▁▂▃▄▄' 30 | 31 | // With only maximum set, minimum defaults to 0 for backwards compatibility 32 | sparkly([10, 20, 30, 40, 50], {maximum: 100}); 33 | //=> '▁▂▃▄▄' 34 | 35 | // Specifying a style option will change the sparkline color 36 | sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}); 37 | // ↓ 38 | ``` 39 | 40 | 41 | 42 | ## API 43 | 44 | ### sparkly(numbers, options?) 45 | 46 | #### numbers 47 | 48 | Type: `number[]` 49 | 50 | The numbers to create the sparkline from. 51 | 52 | #### options 53 | 54 | Type: `object` 55 | 56 | ##### minimum 57 | 58 | Type: `number` 59 | 60 | Minimum value of the sparkline range. 61 | 62 | Values are scaled relative to this baseline. When not specified: 63 | - If `maximum` is set, defaults to `0` (for backwards compatibility) 64 | - Otherwise, defaults to the minimum value in the data 65 | 66 | ##### maximum 67 | 68 | Type: `number` 69 | 70 | Maximum value of the sparkline range. 71 | 72 | Values are scaled relative to this maximum. When not specified, defaults to the maximum value in the data. 73 | 74 | ##### style 75 | 76 | Type: `string`\ 77 | Values: `'fire'` 78 | 79 | Apply color styling to the sparklines. 80 | 81 | The `'fire'` style uses a gradient from yellow to red. Each bar has a fixed width of one terminal column. 82 | 83 | ## Related 84 | 85 | - [sparkly-cli](https://github.com/sindresorhus/sparkly-cli) - CLI for this package 86 | --------------------------------------------------------------------------------