nanocolors
Advanced tools
Comparing version 0.2.6 to 0.2.7
10
index.js
@@ -22,3 +22,3 @@ import tty from 'tty' | ||
function color(open, close, closeRegexp) { | ||
function color(open, close, closeRegexp, restore = open) { | ||
function func(s) { | ||
@@ -34,3 +34,3 @@ if (!s || !s.length) { | ||
open + | ||
(!!~s.indexOf(close, 4) ? s.replace(closeRegexp, open) : s) + | ||
(!!~s.indexOf(close, 4) ? s.replace(closeRegexp, restore) : s) + | ||
close | ||
@@ -43,4 +43,6 @@ ) | ||
let close22 = '\x1b[22m' | ||
let close39 = '\x1b[39m' | ||
let close49 = '\x1b[49m' | ||
let regexp22 = /\x1b\[22m/g | ||
let regexp39 = /\x1b\[39m/g | ||
@@ -54,4 +56,4 @@ let regexp49 = /\x1b\[49m/g | ||
reset: s => `\x1b[0m${s}\x1b[0m`, | ||
bold: color('\x1b[1m', '\x1b[22m', /\x1b\[22m/g), | ||
dim: color('\x1b[2m', '\x1b[22m', /\x1b\[22m/g), | ||
bold: color('\x1b[1m', close22, regexp22, '\x1b[22m\x1b[1m'), | ||
dim: color('\x1b[2m', close22, regexp22, '\x1b[22m\x1b[2m'), | ||
italic: color('\x1b[3m', '\x1b[23m', /\x1b\[23m/g), | ||
@@ -58,0 +60,0 @@ underline: color('\x1b[4m', '\x1b[24m', /\x1b\[24m/g), |
{ | ||
"name": "nanocolors", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "4x times faster than chalk and use 5x less space in node_modules", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
121
README.md
@@ -47,120 +47,3 @@ # Nano Colors | ||
## Benchmarks | ||
Function calling time: | ||
``` | ||
$ ./test/benchmark.js | ||
chalk 11,608,010 ops/sec | ||
cli-color 752,419 ops/sec | ||
ansi-colors 3,601,857 ops/sec | ||
kleur 15,185,239 ops/sec | ||
kleur/colors 21,113,231 ops/sec | ||
colorette 19,712,884 ops/sec | ||
nanocolors 47,256,069 ops/sec | ||
``` | ||
Library loading time: | ||
``` | ||
$ ./test/loading.js | ||
chalk 3.465 ms | ||
cli-color 21.849 ms | ||
ansi-colors 1.101 ms | ||
kleur 1.628 ms | ||
kleur/colors 0.508 ms | ||
colorette 1.034 ms | ||
nanocolors 0.486 ms | ||
``` | ||
The space in `node_modules` including sub-dependencies: | ||
``` | ||
$ ./test/size.js | ||
Data from packagephobia.com | ||
chalk 101 kB | ||
cli-color 1249 kB | ||
ansi-colors 25 kB | ||
kleur 21 kB | ||
colorette 16 kB | ||
nanocolors 16 kB | ||
``` | ||
Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.8. | ||
## Replacing `chalk` | ||
1. Replace import and use named exports: | ||
```diff | ||
- import chalk from 'chalk' | ||
+ import { red, bold } from 'nanocolors' | ||
``` | ||
2. Unprefix calls: | ||
```diff | ||
- chalk.red(text) | ||
+ red(text) | ||
``` | ||
3. Replace chains to nested calls: | ||
```diff | ||
- chalk.red.bold(text) | ||
+ red(bold(text)) | ||
``` | ||
## API | ||
### Individual Colors | ||
Nano Colors exports functions: | ||
| Colors | Background Colors | Modifiers | | ||
| --------- | ------------------- | ----------------- | | ||
| `black` | `bgBlack` | dim | | ||
| `red` | `bgRed` | **bold** | | ||
| `green` | `bgGreen` | hidden | | ||
| `yellow` | `bgYellow` | _italic_ | | ||
| `blue` | `bgBlue` | <u>underline</u> | | ||
| `magenta` | `bgMagenta` | ~~strikethrough~~ | | ||
| `cyan` | `bgCyan` | reset | | ||
| `white` | `bgWhite` | | | ||
| `gray` | | | | ||
Functions are not chainable. You need to wrap it inside each other: | ||
```js | ||
import { black, bgYellow } from 'nanocolors' | ||
console.log(bgYellow(black(' WARN '))) | ||
``` | ||
Functions will use colors only if Nano Colors auto-detect that current | ||
environment supports colors. | ||
You can get support level in `isColorSupported`: | ||
```js | ||
import { isColorSupported } from 'nanocolors' | ||
if (isColorSupported) { | ||
console.log('With colors') | ||
} | ||
``` | ||
### Conditional Support | ||
You can manually switch colors on/off and override color support auto-detection: | ||
```js | ||
import { createColors } from 'nanocolors' | ||
const { red } = createColors(options.enableColors) | ||
``` | ||
On `undefined` argument, `createColors` will use value | ||
from color support auto-detection. | ||
## Docs | ||
Read **[full docs](https://github.com/ai/nanocolors#readme)** on GitHub. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
429
15445
49