Skip to content

Commit efc1a61

Browse files
committed
Create version 3
Rewrite code in TypeScript & pure ESM Deprecate old syntax in favor of a new one Add new tests Bump all dependencies Improve README
1 parent 07638e6 commit efc1a61

17 files changed

+2831
-10042
lines changed

.github/workflows/run.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ jobs:
1313
- uses: actions/checkout@v4
1414
- uses: actions/setup-node@v4
1515
with:
16-
node-version: 18
16+
node-version: 20
1717
- run: npm ci
1818
- run: npm run lint
1919

2020
test:
2121
name: Test (Node v${{ matrix.node }})
2222
runs-on: ubuntu-latest
23+
strategy:
24+
matrix:
25+
node: [14, 20]
2326
steps:
2427
- uses: actions/checkout@v4
2528
- uses: actions/setup-node@v4
2629
with:
27-
node-version: 18
30+
node-version: ${{ matrix.node }}
2831
- run: npm ci
2932
- run: npm run coverage
3033
- name: Update code coverage
34+
if: matrix.node == 20
3135
uses: codecov/codecov-action@v4
3236
with:
3337
token: ${{ secrets.CODECOV_TOKEN }}
@@ -42,7 +46,7 @@ jobs:
4246
- uses: actions/checkout@v4
4347
- uses: actions/setup-node@v4
4448
with:
45-
node-version: 18
49+
node-version: 20
4650
registry-url: https://registry.npmjs.org/
4751
- run: npm ci
4852
- name: Publish on npm

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
node_modules
2-
.nyc_output
3-
coverage.lcov
42
coverage
5-
6-
.idea
7-
.vscode
3+
dist

.husky/pre-commit

100755100644
+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
4-
npm run lint
1+
npx lint-staged --concurrent false

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v16.14.2
1+
v20.17.0

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 Boris K
3+
Copyright (c) 2017 Boris K - github.com/bokub
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+76-62
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
[![Version][version-src]][version-href]
55
[![Codecov][codecov-src]][codecov-href]
66
[![Downloads][downloads-src]][downloads-href]
7-
[![XO code style][xo-src]][xo-href]
7+
[![code style: prettier][code-style-src]][code-style-href]
88
[![Mentioned in Awesome Node.js][awesome-src]][awesome-href]
99

10-
1110
> Beautiful color gradients in terminal output
1211
1312
[![gradient-string](http://bit.ly/gradient-string-preview)](http://bit.ly/gradient-string-large)
1413

15-
1614
## Install
1715

1816
```
@@ -22,37 +20,34 @@ $ npm i gradient-string
2220
## Usage
2321

2422
```javascript
25-
const gradient = require('gradient-string');
23+
import gradient from 'gradient-string';
2624

27-
console.log(gradient('cyan', 'pink')('Hello world!'));
25+
console.log(gradient(['cyan', 'pink'])('Hello world!'));
2826
```
2927

3028
### Initialize a gradient
3129

3230
```javascript
33-
// Using varargs
34-
let coolGradient = gradient('red', 'green', 'blue');
35-
36-
// Using array
37-
let coolGradient = gradient(['#FF0000', '#00FF00', '#0000FF']);
31+
// Provide an array of colors
32+
const coolGradient = gradient(['#FF0000', '#00FF00', '#0000FF']);
3833
```
3934

4035
The colors are parsed with TinyColor, [multiple formats are accepted](https://github.com/bgrins/TinyColor/blob/master/README.md#accepted-string-input).
4136

4237
```javascript
43-
let coolGradient = gradient([
44-
tinycolor('#FFBB65'), // tinycolor object
45-
{r: 0, g: 255, b: 0}, // RGB object
46-
{h: 240, s: 1, v: 1, a: 1}, // HSVa object
47-
'rgb(120, 120, 0)', // RGB CSS string
48-
'gold' // named color
38+
const coolGradient = gradient([
39+
tinycolor('#FFBB65'), // tinycolor object
40+
{ r: 0, g: 255, b: 0 }, // RGB object
41+
{ h: 240, s: 1, v: 1, a: 1 }, // HSVa object
42+
'rgb(120, 120, 0)', // RGB CSS string
43+
'gold', // named color
4944
]);
5045
```
5146

5247
### Use a gradient
5348

5449
```javascript
55-
let coolString = coolGradient('This is a fancy string!');
50+
const coolString = coolGradient('This is a fancy string!');
5651
console.log(coolString);
5752
```
5853

@@ -61,10 +56,13 @@ console.log(coolString);
6156
### Usage
6257

6358
```javascript
64-
const gradient = require('gradient-string');
59+
import { rainbow, pastel } from 'gradient-string';
6560

66-
// Use the rainbow gradient
67-
console.log(gradient.rainbow('I love gradient-strings!'))
61+
// Use the pastel built-in gradient
62+
console.log(pastel('I love gradient-string!'));
63+
64+
// Use the rainbow built-in gradient
65+
console.log(rainbow('It is so pretty! 🌈'));
6866
```
6967

7068
### Available built-in gradients
@@ -78,29 +76,28 @@ In some cases, you may want to apply the same horizontal gradient on each line o
7876
You can use the `multiline()` method of a gradient to ensure that the colors are vertically aligned.
7977

8078
```javascript
81-
const gradient = require('gradient-string');
79+
import gradient, { rainbow } from 'gradient-string';
8280

8381
// Use the same gradient on every line
84-
let duck = gradient('orange', 'yellow').multiline([
85-
" __",
86-
"<(o )___",
87-
" ( ._> /",
88-
" `---'",
89-
].join('\n'));
82+
const duck = gradient(['green', 'yellow']).multiline(`
83+
__
84+
<(o )___
85+
( ._> /
86+
---
87+
`);
9088
console.log(duck);
9189

9290
// Works with aliases
93-
gradient.atlas.multiline('Multi line\nstring');
91+
rainbow.multiline('Multi line\nstring');
9492

95-
// Works with advanced options
96-
gradient('cyan', 'pink').multiline('Multi line\nstring', {interpolation: 'hsv'});
93+
// Works with advanced options (read below)
94+
gradient(['cyan', 'pink'], { interpolation: 'hsv' }).multiline('Multi line\nstring');
9795
```
9896

99-
10097
## Advanced gradients
10198

10299
There are also more advanced options for gradient customization, such as custom color stops, or choice of color interpolation
103-
100+
104101
### Custom color stops
105102

106103
By default, the gradient color stops are distributed equidistantly.
@@ -109,87 +106,104 @@ You can specify the position of each color stop (between `0` and `1`), using the
109106

110107
```javascript
111108
let coolGradient = gradient([
112-
{color: '#d8e0de', pos: 0},
113-
{color: '#255B53', pos: 0.8},
114-
{color: '#000000', pos: 1}
109+
{ color: '#d8e0de', pos: 0 },
110+
{ color: '#255B53', pos: 0.8 },
111+
{ color: '#000000', pos: 1 },
115112
]);
116113
```
117114

118115
### Color interpolation
119116

120-
When using a gradient, you can actually add a second parameter to choose how the colors will be generated.
117+
When creating a gradient, you can provide a second parameter to choose how the colors will be generated.
121118

122-
Here is the full gradient API:
119+
Here is the full `gradient` API:
123120

124-
#### myGradient(text, [options])
121+
#### `gradient([colors], options?)(text)`
122+
123+
##### colors
124+
125+
Type: `Array<Color>`<br>
126+
Colors of the gradient. [Multiple formats are accepted](https://github.com/bgrins/TinyColor/blob/master/README.md#accepted-string-input).
125127

126128
##### text
127-
Type: `string`<br>
129+
130+
Type: `String`<br>
128131
String you want to color.
129132

130133
##### options
131-
Type: `Object`<br>
134+
135+
Type: `Object` _(optional)_<br>
132136

133137
###### interpolation
138+
134139
Type: `string`<br>
135140
The gradient can be generated using RGB or HSV interpolation. HSV usually produces brighter colors.
136141
`interpolation` can be set to `rgb` for RGB interpolation, or`hsv` for HSV interpolation.<br>
137-
Defaults to `rgb`. Case insentitive
142+
Defaults to `rgb`. Case-insensitive
138143

139144
###### hsvSpin
145+
140146
Type: `string`<br>
141147
Used only in the case of HSV interpolation.<br>
142148
Because hue can be considered as a circle, there are two ways to go from a color to another color.<br>
143149
`hsvSpin` can be either `short` or `long`, depending on if you want to take the shortest or the longest way between two colors.<br>
144-
Defaults to `short`. Case insensitive
150+
Defaults to `short`. Case-insensitive
145151

146152
#### Example
153+
147154
##### Code
155+
148156
```javascript
149-
const redToGreen = gradient('red', 'green');
150157
const str = ''.repeat(48);
151158

152159
// Standard RGB gradient
153-
console.log(redToGreen(str));
160+
const standardRGBGradient = gradient(['red', 'green']);
154161

155162
// Short HSV gradient: red -> yellow -> green
156-
console.log(redToGreen(str, {interpolation: 'hsv'}));
163+
const shortHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv' });
157164

158165
// Long HSV gradient: red -> magenta -> blue -> cyan -> green
159-
console.log(redToGreen(str, {interpolation: 'hsv', hsvSpin: 'long'}));
160-
```
161-
##### Result
162-
![Example result](http://i.imgur.com/plQAN2Q.png)
163-
164-
## Typescript
165-
166-
Typescript definitions of gradient-string are available on [DefinitelyTyped](https://www.npmjs.com/package/@types/gradient-string)
166+
const longHSVGradient = gradient(['red', 'green'], { interpolation: 'hsv', hsvSpin: 'long' });
167167

168-
```sh
169-
npm i @types/gradient-string
168+
console.log(standardRGBGradient(str));
169+
console.log(shortHSVGradient(str));
170+
console.log(longHSVGradient(str));
170171
```
171172

173+
##### Result
174+
175+
![Example result](http://i.imgur.com/plQAN2Q.png)
172176

173177
## Dependencies
174178

175-
- [tinygradient](https://github.com/mistic100/tinygradient) - Generate gradients
176179
- [chalk](https://github.com/chalk/chalk) - Output colored text to terminal
180+
- [tinygradient](https://github.com/mistic100/tinygradient) - Generate gradients
181+
182+
## Who uses gradient-string?
177183

184+
- [Shopify](https://shopify.com/) in [Shopify CLI](https://www.npmjs.com/package/@shopify/cli-kit?activeTab=dependencies#:~:text=gradient&text=string)
185+
- [Microsoft](https://microsoft.com) in [@lage-run/reporters](https://www.npmjs.com/package/@lage-run/reporters?activeTab=dependencies#:~:text=gradient&text=string)
186+
- [Tencent](https://www.tencent.com/) in [Cloudbase Framework](https://www.npmjs.com/package/@cloudbase/framework-core#:~:text=gradient&text=string)
187+
- [Fireship](https://fireship.io/) in [this YouTube video](https://youtu.be/_oHByo8tiEY?si=3AKfAfOMXI0d9Ay6&t=76), where he shows how he built [javascript-millionaire](https://github.com/fireship-io/javascript-millionaire)
188+
- [Turoborepo](https://turbo.build/) in [@turbo/workspaces](https://www.npmjs.com/package/@turbo/workspaces?activeTab=dependencies#:~:text=gradient&text=string) and [@turbo/codemod](https://www.npmjs.com/package/@turbo/codemod?activeTab=dependencies#:~:text=gradient&text=string)
189+
- [Magic UI](https://magicui.design/) in [Magic UI CLI](https://www.npmjs.com/package/magicui-cli?activeTab=dependencies)
190+
- [Myself](https://github.com/bokub) in [chalk-animation](https://github.com/bokub/chalk-animation), the animated version of gradient-string
191+
- [Sindre Sorhus](https://github.com/sindresorhus) in [ink-gradient](https://www.npmjs.com/package/ink-gradient?activeTab=dependencies#:~:text=gradient&text=string), the [Ink](https://github.com/vadimdemedes/ink) version of gradient-string
192+
- [And ![](https://flat.badgen.net/github/dependents-repo/bokub/gradient-string?color=000&label=) more...](https://github.com/bokub/gradient-string/network/dependents), who downloaded gradient-string [![Downloads](https://flat.badgen.net/npm/dt/gradient-string?color=000&label=) times!][downloads-href]
178193

179194
## License
180195

181196
MIT © [Boris K](https://github.com/bokub)
182197

183198
[build-src]: https://flat.badgen.net/github/checks/bokub/gradient-string?label=tests
184-
[version-src]: https://runkit.io/bokub/npm-version/branches/master/gradient-string?style=flat
185-
[codecov-src]: https://flat.badgen.net/codecov/c/github/bokub/gradient-string
186-
[downloads-src]: https://flat.badgen.net/npm/dm/gradient-string?color=FF9800
187-
[xo-src]: https://flat.badgen.net/badge/code%20style/XO/5ed9c7
199+
[version-src]: https://gradgen.bokub.workers.dev/npm/v/gradient-string?gradient=b65cff,11cbfa&style=flat&label=version
200+
[codecov-src]: https://img.shields.io/codecov/c/github/bokub/rgb-light-card?style=flat-square
201+
[downloads-src]: https://flat.badgen.net/npm/dw/gradient-string?color=FF9800
202+
[code-style-src]: https://flat.badgen.net/badge/code%20style/prettier/ff69b4
188203
[awesome-src]: https://awesome.re/mentioned-badge-flat.svg
189-
190204
[build-href]: https://github.com/bokub/gradient-string/actions/workflows/run.yml
191205
[version-href]: https://www.npmjs.com/package/gradient-string
192206
[codecov-href]: https://codecov.io/gh/bokub/gradient-string
193207
[downloads-href]: https://www.npmjs.com/package/gradient-string
194-
[xo-href]: https://github.com/sindresorhus/xo
208+
[code-style-href]: https://github.com/bokub/prettier-config
195209
[awesome-href]: https://github.com/sindresorhus/awesome-nodejs

examples/built-in.js

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
#!/usr/bin/env node
22

3-
// Run with npm run built-in
4-
const gradient = require('..');
3+
// Run with "npm run built-in"
4+
import gradient from '../dist/index.js';
55

6-
const {log} = console;
6+
const { log } = console;
77
const str = '■'.repeat(48);
88

99
log('');
1010

1111
for (const t of [
12-
'cristal',
13-
'teen',
14-
'mind',
15-
'morning',
16-
'vice',
17-
'passion',
18-
'fruit',
19-
'instagram',
20-
'atlas',
21-
'retro',
22-
'summer',
23-
'pastel',
24-
'rainbow'
12+
'cristal',
13+
'teen',
14+
'mind',
15+
'morning',
16+
'vice',
17+
'passion',
18+
'fruit',
19+
'instagram',
20+
'atlas',
21+
'retro',
22+
'summer',
23+
'pastel',
24+
'rainbow',
2525
]) {
26-
log(pad(t), gradient[t](str));
26+
log(' ' + t.padEnd(11, ' '), gradient[t](str));
2727
}
2828

2929
log('');
30-
31-
function pad(s) {
32-
let i = -1;
33-
const l = 11 - s.length;
34-
while (++i < l) {
35-
s += ' ';
36-
}
37-
return ' ' + s;
38-
}

0 commit comments

Comments
 (0)