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<number | ''>,
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<string>(sparkly([0, 3, 5, 8, 4, 3, 4, 10]));
expectType<string>(sparkly([0, 3, 5, '', 4, 3, 4, 10]));
expectType<string>(sparkly([1, 2, 3, 4, 5], {minimum: 0}));
expectType<string>(sparkly([1, 2, 3, 4, 5], {maximum: 10}));
expectType<string>(sparkly([1, 2, 3, 4, 5, 6, 7, 8], {style: 'fire'}));
================================================
FILE: license
================================================
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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
================================================
# 
> 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'});
// ↓
```
<img src="screenshot.png" width="383">
## 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)('▅'));
});
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
SYMBOL INDEX (2 symbols across 2 files)
FILE: index.d.ts
type Options (line 1) | interface Options {
FILE: index.js
function sparkly (line 3) | function sparkly(numbers, options = {}) {
Condensed preview — 12 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (10K chars).
[
{
"path": ".editorconfig",
"chars": 175,
"preview": "root = true\n\n[*]\nindent_style = tab\nend_of_line = lf\ncharset = utf-8\ntrim_trailing_whitespace = true\ninsert_final_newlin"
},
{
"path": ".gitattributes",
"chars": 19,
"preview": "* text=auto eol=lf\n"
},
{
"path": ".github/workflows/main.yml",
"chars": 436,
"preview": "name: CI\non:\n - push\n - pull_request\njobs:\n test:\n name: Node.js ${{ matrix.node-version }}\n runs-on: ubuntu-la"
},
{
"path": ".gitignore",
"chars": 23,
"preview": "node_modules\nyarn.lock\n"
},
{
"path": ".npmrc",
"chars": 19,
"preview": "package-lock=false\n"
},
{
"path": "index.d.ts",
"chars": 1548,
"preview": "interface Options {\n\t/**\n\tMinimum value of the sparkline range.\n\n\tValues are scaled relative to this baseline. When not "
},
{
"path": "index.js",
"chars": 1228,
"preview": "import chalk from 'chalk';\n\nexport default function sparkly(numbers, options = {}) {\n\tif (!Array.isArray(numbers)) {\n\t\tt"
},
{
"path": "index.test-d.ts",
"chars": 373,
"preview": "import {expectType} from 'tsd';\nimport sparkly from './index.js';\n\nexpectType<string>(sparkly([0, 3, 5, 8, 4, 3, 4, 10])"
},
{
"path": "license",
"chars": 1117,
"preview": "MIT License\n\nCopyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)\n\nPermission is hereby grant"
},
{
"path": "package.json",
"chars": 863,
"preview": "{\n\t\"name\": \"sparkly\",\n\t\"version\": \"6.0.1\",\n\t\"description\": \"Generate sparklines `▁▂▃▅▂▇`\",\n\t\"license\": \"MIT\",\n\t\"reposito"
},
{
"path": "readme.md",
"chars": 1872,
"preview": "# \n\n> Gene"
},
{
"path": "test.js",
"chars": 1300,
"preview": "import test from 'ava';\nimport chalk from 'chalk';\nimport sparkly from './index.js';\n\nchalk.level = 3;\n\n// Example\nconso"
}
]
About this extraction
This page contains the full source code of the sindresorhus/sparkly GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 12 files (8.8 KB), approximately 3.3k tokens, and a symbol index with 2 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.