Repository: sindresorhus/sparkly Branch: main Commit: 7a9e1c2613f5 Files: 12 Total size: 8.8 KB Directory structure: gitextract_t2wj_ax8/ ├── .editorconfig ├── .gitattributes ├── .github/ │ └── workflows/ │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ================================================ FILE: .gitattributes ================================================ * text=auto eol=lf ================================================ FILE: .github/workflows/main.yml ================================================ name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 - 14 - 12 steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test ================================================ FILE: .gitignore ================================================ node_modules yarn.lock ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: index.d.ts ================================================ interface Options { /** Minimum value of the sparkline range. Values are scaled relative to this baseline. When not specified: - If `maximum` is set, defaults to `0` (for backwards compatibility) - Otherwise, defaults to the minimum value in the data */ readonly minimum?: number; /** Maximum value of the sparkline range. Values are scaled relative to this maximum. When not specified, defaults to the maximum value in the data. */ readonly maximum?: number; /** Apply color styling to the sparklines. The `'fire'` style uses a gradient from yellow to red. Each bar has a fixed width of one terminal column. @default 'fire' */ readonly style?: 'fire'; } /** Generate sparklines `▁▂▃▅▂▇`. @param numbers - The numbers to create the sparkline from. @example ``` import sparkly from 'sparkly'; sparkly([0, 3, 5, 8, 4, 3, 4, 10]); //=> '▁▃▄▇▄▃▄█' // Specifying anything other than finite numbers will cause holes sparkly([0, 3, 5, '', 4, 3, 4, 10]); //=> '▁▃▄ ▄▃▄█' // Specifying minimum and/or maximum options will change the sparkline range sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}); //=> '▁▂▃▄▄' // With only maximum set, minimum defaults to 0 for backwards compatibility sparkly([10, 20, 30, 40, 50], {maximum: 100}); //=> '▁▂▃▄▄' // Specifying a style option will change the sparkline color sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}); //=> Colored sparkline (gradient from yellow to red) ``` */ export default function sparkly( numbers: ReadonlyArray, options?: Options ): string; ================================================ FILE: index.js ================================================ 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 : (typeof options.maximum === 'number' ? 0 : 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; if (minimum === maximum) { tickIndex = 0; } else { tickIndex = Math.ceil(((number - minimum) / (maximum - minimum)) * ticks.length) - 1; } if (tickIndex < 0) { tickIndex = 0; } if (options.style === 'fire') { return chalk.rgb(...color[tickIndex])(ticks[tickIndex]); } return ticks[tickIndex]; }).join(''); } ================================================ FILE: index.test-d.ts ================================================ import {expectType} from 'tsd'; import sparkly from './index.js'; expectType(sparkly([0, 3, 5, 8, 4, 3, 4, 10])); expectType(sparkly([0, 3, 5, '', 4, 3, 4, 10])); expectType(sparkly([1, 2, 3, 4, 5], {minimum: 0})); expectType(sparkly([1, 2, 3, 4, 5], {maximum: 10})); expectType(sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'})); ================================================ FILE: license ================================================ MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ================================================ FILE: package.json ================================================ { "name": "sparkly", "version": "6.0.1", "description": "Generate sparklines `▁▂▃▅▂▇`", "license": "MIT", "repository": "sindresorhus/sparkly", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "types": "./index.d.ts", "sideEffects": false, "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "//test": "xo && ava && tsd", "test": "ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "spark", "sparkly", "line", "sparkline", "sparklines", "unicode", "data", "graph", "plot", "ascii" ], "dependencies": { "chalk": "^4.1.2" }, "devDependencies": { "ava": "^3.15.0", "tsd": "^0.19.0", "xo": "^0.46.4" } } ================================================ FILE: readme.md ================================================ # ![sparkly](https://cloud.githubusercontent.com/assets/170270/4068189/1b47cab0-2e36-11e4-8b75-16b80330147e.gif) > Generate sparklines `▁▂▃▅▂▇` JavaScript port of [spark.sh](https://github.com/holman/spark). [Some cool use-cases.](https://github.com/holman/spark/wiki/Wicked-Cool-Usage) ## Install ```sh npm install sparkly ``` ## Usage ```js import sparkly from 'sparkly'; sparkly([0, 3, 5, 8, 4, 3, 4, 10]); //=> '▁▃▄▇▄▃▄█' // Specifying anything other than finite numbers will cause holes sparkly([0, 3, 5, '', 4, 3, 4, 10]); //=> '▁▃▄ ▄▃▄█' // Specifying minimum and/or maximum options will change the sparkline range sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}); //=> '▁▂▃▄▄' // With only maximum set, minimum defaults to 0 for backwards compatibility sparkly([10, 20, 30, 40, 50], {maximum: 100}); //=> '▁▂▃▄▄' // Specifying a style option will change the sparkline color sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}); // ↓ ``` ## API ### sparkly(numbers, options?) #### numbers Type: `number[]` The numbers to create the sparkline from. #### options Type: `object` ##### minimum Type: `number` Minimum value of the sparkline range. Values are scaled relative to this baseline. When not specified: - If `maximum` is set, defaults to `0` (for backwards compatibility) - Otherwise, defaults to the minimum value in the data ##### maximum Type: `number` Maximum value of the sparkline range. Values are scaled relative to this maximum. When not specified, defaults to the maximum value in the data. ##### style Type: `string`\ Values: `'fire'` Apply color styling to the sparklines. The `'fire'` style uses a gradient from yellow to red. Each bar has a fixed width of one terminal column. ## Related - [sparkly-cli](https://github.com/sindresorhus/sparkly-cli) - CLI for this package ================================================ FILE: test.js ================================================ import test from 'ava'; import chalk from 'chalk'; import sparkly from './index.js'; chalk.level = 3; // Example console.log(' ' + sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}) + '\n'); test('creates graph', t => { t.is(sparkly([1, 5, 22, 13, 5]), '▁▂█▅▂'); t.is(sparkly([0, 30, 55, 80, 33, 150]), '▁▂▃▅▂█'); t.is(sparkly([5.5, 20]), '▁█'); t.is(sparkly([1, 2, 3, 4, 100, 5, 10, 20, 50, 300]), '▁▁▁▁▃▁▁▁▂█'); t.is(sparkly([1, 50, 100]), '▁▄█'); t.is(sparkly([2, 4, 8]), '▁▃█'); t.is(sparkly([1, 2, 3, 4, 5]), '▁▂▄▆█'); t.is(sparkly([25, 45]), '▁█'); }); test('anything other than finite numbers causes holes', t => { t.is(sparkly([1, '', 3, Number.NaN, 5]), '▁ ▄ █'); }); test('use the middle tick if data is constant', t => { t.is(sparkly([10, 10, 10, 10, 10]), '▅▅▅▅▅'); }); test('use the lowest tick if data is all 0', t => { t.is(sparkly([0, 0, 0, 0, 0]), '▁▁▁▁▁'); }); test('minimum and maximum arguments set graph range', t => { t.is(sparkly([1], {minimum: 0, maximum: 1}), '█'); t.is(sparkly([1, 2, 3, 4, 5], {minimum: 0, maximum: 10}), '▁▂▃▄▄'); t.is(sparkly([10, 11, 12, 13], {minimum: 0}), '▇▇██'); t.is(sparkly([10, 20, 30, 40, 50], {maximum: 100}), '▁▂▃▄▄'); }); test('colored graph', t => { t.is(sparkly([1], {style: 'fire'}), chalk.rgb(5, 5, 4)('▅')); });