Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kleur

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kleur - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

92

index.js

@@ -1,25 +0,68 @@

const { format } = require('util');
const CODES = require('./codes');
const $ = { enabled:true };
const RGX = /\x1b\[[0-9]+m/ig;
function print() {
let out = format.apply(null, arguments);
if (!$.enabled || out.trim().length == 0) return out;
const CODES = {
// modifiers
reset: fmt(0, 0),
bold: fmt(1, 22),
dim: fmt(2, 22),
italic: fmt(3, 23),
underline: fmt(4, 24),
inverse: fmt(7, 27),
hidden: fmt(8, 28),
strikethrough: fmt(9, 29),
// colors
black: fmt(30, 39),
red: fmt(31, 39),
green: fmt(32, 39),
yellow: fmt(33, 39),
blue: fmt(34, 39),
magenta: fmt(35, 39),
cyan: fmt(36, 39),
white: fmt(37, 39),
gray: fmt(90, 39),
// background colors
bgBlack: fmt(40, 49),
bgRed: fmt(41, 49),
bgGreen: fmt(42, 49),
bgYellow: fmt(43, 49),
bgBlue: fmt(44, 49),
bgMagenta: fmt(45, 49),
bgCyan: fmt(46, 49),
bgWhite: fmt(47, 49)
};
let i=0, tmp, arr=this.keys, isMulti=!!~out.indexOf('\n');
for (; i < arr.length; i++) {
tmp = CODES[arr[i]]; // { x1, x2, rgx }
out = tmp.beg + out.replace(tmp.rgx, tmp.beg) + tmp.end;
isMulti && (out = out.replace(/(\r?\n)/g, `${tmp.end}$1${tmp.beg}`));
function fmt(x, y) {
return {
open: `\x1b[${x}m`,
close: `\x1b[${y}m`,
rgx: new RegExp(`\\x1b\\[${y}m`, 'g')
}
}
return out;
function run(key, str) {
let tmp = CODES[key];
return tmp.open + str.replace(tmp.rgx, tmp.open) + tmp.close;
}
function wrap(keys) {
let ctx = {};
let fn = Object.setPrototypeOf(print.bind(ctx), $);
ctx.keys = fn.keys = keys;
function exec(key, str) {
if (!$.enabled) return str;
let arr = this.keys;
while (arr.length > 0) {
str = run(arr.shift(), str);
}
this.keys.push(key);
return str;
}
function attach(key) {
let ctx = { keys:[key] };
let fn = exec.bind(ctx, key);
for (let k in CODES) {
Reflect.defineProperty(fn, k, {
get() {
ctx.keys.push(k);
return fn;
}
});
}
return fn;

@@ -29,18 +72,5 @@ }

for (let k in CODES) {
CODES[k] = {
beg: `\x1b[${CODES[k][0]}m`,
end: `\x1b[${CODES[k][1]}m`,
rgx: new RegExp(`\\x1b\\[${CODES[k][1]}m`, 'g')
};
Object.defineProperty($, k, {
get() {
return this.keys !== void 0 ? (this.keys.push(k),this) : wrap([k]);
}
});
$[k] = attach(k);
}
$.clear = str => {
return str && typeof str === 'string' ? str.replace(RGX, '') : str;
}
module.exports = $;

@@ -85,6 +85,1 @@ // TypeScript definitions for kleur

export let enabled: boolean;
/**
* Remove Styles
*/
export function clear(string: string): string;
{
"name": "kleur",
"version": "1.0.2",
"version": "2.0.0",
"repository": "lukeed/kleur",

@@ -5,0 +5,0 @@ "description": "The fastest Node.js library for formatting terminal text with ANSI colors~!",

@@ -7,10 +7,13 @@ <div align="center">

<a href="https://npmjs.org/package/kleur">
<img src="https://img.shields.io/npm/v/kleur.svg" alt="version" />
<img src="https://badgen.now.sh/npm/v/kleur" alt="version" />
</a>
<a href="https://travis-ci.org/lukeed/kleur">
<img src="https://img.shields.io/travis/lukeed/kleur.svg" alt="travis" />
<img src="https://badgen.now.sh/travis/lukeed/kleur" alt="travis" />
</a>
<a href="https://npmjs.org/package/kleur">
<img src="https://img.shields.io/npm/dm/kleur.svg" alt="downloads" />
<img src="https://badgen.now.sh/npm/dm/kleur" alt="downloads" />
</a>
<a href="https://packagephobia.now.sh/result?p=kleur">
<img src="https://packagephobia.now.sh/badge?p=kleur" alt="install size" />
</a>
</div>

@@ -26,7 +29,6 @@

* No `String.prototype` modifications
* Supports [`printf`](#printf-formatting) formatting
* Conditional [color support](#conditional-support)
* Familiar [API](#api)
_Heavily inspired by [`ansi-colors`](https://github.com/doowb/ansi-colors). See [Credits](#credits) for more info!_
_Originally inspired by [`ansi-colors`](https://github.com/doowb/ansi-colors). See [Credits](#credits) for more info!_

@@ -49,8 +51,2 @@

// or variadic arguments
kleur.red('this', 'is', 'also', 'red');
// or printf formatting
kleur.red('%s, %s!', 'hello', 'world');
// chained methods

@@ -79,7 +75,4 @@ kleur.blue.bold.underline('howdy partner');

// with template literals
console.log(yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`));
// or variadic arguments
console.log(yellow('foo', red.bold('red'), 'bar', cyan('cyan'), 'baz'));
console.log(yellow('foo ' + red.bold('red') + ' bar ' + cyan('cyan') + ' baz'));
```

@@ -90,31 +83,2 @@

### `printf` Formatting
> See [`util.format`](https://nodejs.org/api/util.html#util_util_format_format_args) for documentation
```js
const { yellow, bgGreen, bold } = require('kleur');
// basic usage
console.log(bold.cyan('%s, %s!', 'Hello', 'World', '-Anonymous'));
// or with nested colors
console.log( bold('%s-like %s... %s!', 'printf', bgGreen.black('formatting'), yellow('YAY')) );
```
<img src="shots/3.png" width="300" />
### Clear Formatting
Manually strip all ANSI codes from a given string.
```js
let str = kleur.blue('Howdy partner');
//=> styled
kleur.clear(str);
//=> 'Howdy partner'
```
### Conditional Support

@@ -162,6 +126,6 @@

```
ansi-colors: 1.150ms
chalk: 8.440ms
clorox: 0.471ms
kleur: 0.611ms
chalk: 9.372ms
turbocolor: 0.526ms
ansi-colors: 0.851ms
kleur: 0.862ms
```

@@ -173,18 +137,18 @@

# All Colors
ansi-colors x 60,646 ops/sec ±0.49% (96 runs sampled)
chalk x 7,228 ops/sec ±3.25% (73 runs sampled)
clorox x 86,631 ops/sec ±0.59% (94 runs sampled)
kleur x 95,595 ops/sec ±0.24% (96 runs sampled)
ansi-colors x 60,762 ops/sec ±0.36% (96 runs sampled)
chalk x 7,144 ops/sec ±3.99% (71 runs sampled)
turbocolor x 98,181 ops/sec ±0.30% (95 runs sampled)
kleur x 146,739 ops/sec ±0.55% (93 runs sampled)
# Stacked colors
ansi-colors x 13,576 ops/sec ±0.42% (93 runs sampled)
chalk x 1,669 ops/sec ±4.56% (71 runs sampled)
clorox x 26,166 ops/sec ±1.44% (91 runs sampled)
kleur x 28,674 ops/sec ±0.29% (93 runs sampled)
ansi-colors x 13,600 ops/sec ±0.15% (95 runs sampled)
chalk x 1,690 ops/sec ±4.75% (70 runs sampled)
turbocolor x 28,830 ops/sec ±0.21% (94 runs sampled)
kleur x 30,573 ops/sec ±0.15% (96 runs sampled)
# Nested colors
ansi-colors x 28,712 ops/sec ±0.60% (96 runs sampled)
chalk x 3,446 ops/sec ±4.59% (69 runs sampled)
Clorox x 40,821 ops/sec ±1.90% (94 runs sampled)
kleur x 43,242 ops/sec ±0.17% (97 runs sampled)
ansi-colors x 28,747 ops/sec ±0.50% (90 runs sampled)
chalk x 3,424 ops/sec ±4.04% (71 runs sampled)
turbocolor x 41,181 ops/sec ±2.99% (91 runs sampled)
kleur x 61,155 ops/sec ±0.19% (96 runs sampled)
```

@@ -195,13 +159,18 @@

This project is based on [Brian Woodward](https://github.com/doowb)'s awesome [`ansi-colors`](https://github.com/doowb/ansi-colors) project. My original implementation involved writing into a global state &mdash; first by writing into an output string, and then by saving the `keys` array into the `$` directly. Both approaches were leaky & allowed for accidental chains/overwrites. In turn, I borrowed `ansi-colors`'s approach in writing `keys` state into each chain directly.
This project was originally inspired by [Brian Woodward](https://github.com/doowb)'s awesome [`ansi-colors`](https://github.com/doowb/ansi-colors) project.
Aside from the performance boost, `kleur` exists as a separate module because I've removed some of `ansi-colors`'s defining features, like bright color variants and symbols. It's tailor-made for my needs and experimentation.
Unlike v1, the latest version(s) of `kleur` no longer supports:
> You'll probably want to use [`ansi-colors`](https://github.com/doowb/ansi-colors), especially if you need any of those features! [You'll be in good company!](https://www.npmjs.com/browse/depended/ansi-colors)
* printf-formatting
* variadic function arguments
* multiline text via `\n` or `\r`
* `kleur.clear()` method
The benchmark suite is also imported directly from `ansi-colors` :raised_hands:
In addition, `kleur` continues to be ship without symbols and bright color variants.
If you need _any_ of these features, please use `ansi-colors` instead~!
## License
MIT © [Luke Edwards](https://lukeed.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc