@santi100/coloring-lib
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,17 +0,44 @@ | ||
type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'bold' | 'blink' | 'conceal'; | ||
type Effect = 'bold' | 'blink' | 'conceal'; | ||
type Parameter = Color | Effect; | ||
type ParameterArray = Parameter[]; | ||
/** | ||
* Color `string` with color(s) `color`. | ||
* A color to paint a string in. | ||
*/ | ||
declare type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'bold' | 'blink' | 'conceal'; | ||
/** | ||
* An effect you can give to a string. | ||
*/ | ||
declare type Effect = 'bold' | 'blink' | 'conceal'; | ||
/** | ||
* A valid color or effect. | ||
*/ | ||
declare type ColorOrEffect = Color | Effect; | ||
/** | ||
* An array of valid colors or effects. | ||
*/ | ||
declare type ArrayOfColorsOrEffects = ColorOrEffect[]; | ||
/** | ||
* Color `str` with color `color`. | ||
* | ||
* @param string The string to paint in color! | ||
* @param color The colors to paint the string in. | ||
* @param str The string to paint in color! | ||
* @param color The color to paint the string in. | ||
*/ | ||
declare function coloring(str: string, color: ParameterArray | Parameter): string; | ||
export declare function coloring(str: string, color: ColorOrEffect): string; | ||
/** | ||
* Color `str` with colors `colors`. | ||
* | ||
* @param str The string to paint in color! | ||
* @param colors The colors to paint the string in. | ||
*/ | ||
export declare function coloring(str: string, colors: ArrayOfColorsOrEffects): string; | ||
/** | ||
* Colors `text` in a rainbow pattern. | ||
* | ||
* @param str The string to paint in color! | ||
* @returns The colored string. | ||
*/ | ||
export declare function rainbowify(str: string): string; | ||
/** | ||
* @class This is a colorizer class. | ||
*/ | ||
declare class Colorizer { | ||
private _colors; | ||
export declare class Colorizer { | ||
private __string; | ||
constructor(); | ||
/** | ||
@@ -107,5 +134,5 @@ * Paints `text` in black. | ||
/** | ||
* @deprecated Use {@link Colorizer} instead. | ||
* @deprecated This is a deprecated alias for the `Colorizer` class. Use {@link Colorizer} instead. | ||
*/ | ||
declare const Coloring: typeof Colorizer; | ||
export { coloring, Colorizer, Coloring }; | ||
export declare const Coloring: typeof Colorizer; | ||
export type { ArrayOfColorsOrEffects, ColorOrEffect, Color, Effect }; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.Coloring = exports.Colorizer = exports.coloring = void 0; | ||
var cjs_1 = require("@santi100/assertion-lib/cjs"); | ||
exports.Coloring = exports.Colorizer = exports.rainbowify = exports.coloring = void 0; | ||
var assertion_lib_1 = require("@santi100/assertion-lib"); | ||
var COLORS = { | ||
@@ -18,12 +18,23 @@ black: '\x1b[30m', | ||
}; | ||
function __keys(o) { | ||
if (Object === null || Object === void 0 ? void 0 : Object.keys) | ||
return Object.keys(o); | ||
else { | ||
var keys = []; | ||
for (var key in o) { | ||
keys.push(key); | ||
} | ||
return keys; | ||
} | ||
} | ||
/** | ||
* Color `string` with color(s) `color`. | ||
* Color `str` with color(s) `color`. | ||
* | ||
* @param string The string to paint in color! | ||
* @param color The colors to paint the string in. | ||
* @param str The string to paint in color! | ||
* @param color The color(s) to paint the string in. | ||
*/ | ||
function coloring(str, color) { | ||
var isColorValid = Array.isArray(color) || color in COLORS; | ||
(0, cjs_1.assertType)(str, 'string'); | ||
(0, cjs_1.assert)(isColorValid, { expected: true, actual: isColorValid, operator: '||' }); | ||
(0, assertion_lib_1.assertTypeOf)(str, 'string', 'str'); | ||
(0, assertion_lib_1.assertTypeOf)(color, 'string', 'color'); | ||
(0, assertion_lib_1.assertOneOf)(color, 'color', __keys(COLORS)); | ||
function generateFromArray(color) { | ||
@@ -44,2 +55,31 @@ var j = ''; | ||
/** | ||
* Colors `text` in a rainbow pattern. | ||
* | ||
* @param str The string to paint in color! | ||
* @returns The colored string. | ||
*/ | ||
function rainbowify(str) { | ||
(0, assertion_lib_1.assertTypeOf)(str, 'string', 'text'); | ||
var colors = [ | ||
COLORS.red, | ||
COLORS.yellow, | ||
COLORS.green, | ||
COLORS.cyan, | ||
COLORS.blue, | ||
COLORS.magenta | ||
]; // red, orange, yellow, green, blue, magenta | ||
var letters = ''.concat(str).split(''); | ||
var coloredText = ''; | ||
var colorIndex = 0; | ||
for (var _i = 0, letters_1 = letters; _i < letters_1.length; _i++) { | ||
var element = letters_1[_i]; | ||
var letter = element; | ||
var color = colors[colorIndex]; | ||
coloredText += "".concat(color).concat(letter, "\u001B[0m"); | ||
colorIndex = (colorIndex + 1) % colors.length; | ||
} | ||
return coloredText; | ||
} | ||
exports.rainbowify = rainbowify; | ||
/** | ||
* @class This is a colorizer class. | ||
@@ -49,3 +89,7 @@ */ | ||
function Colorizer() { | ||
this._colors = ''; | ||
var _a; | ||
this.__string = ''; | ||
(_a = Object === null || Object === void 0 ? void 0 : Object.defineProperty) === null || _a === void 0 ? void 0 : _a.call(Object, this, '__string', { | ||
enumerable: false | ||
}); | ||
} | ||
@@ -59,3 +103,3 @@ /** | ||
Colorizer.prototype.black = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.black).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.black).concat(text); | ||
return this; | ||
@@ -70,3 +114,3 @@ }; | ||
Colorizer.prototype.red = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.red).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.red).concat(text); | ||
return this; | ||
@@ -81,3 +125,3 @@ }; | ||
Colorizer.prototype.green = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.green).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.green).concat(text); | ||
return this; | ||
@@ -92,3 +136,3 @@ }; | ||
Colorizer.prototype.yellow = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.yellow).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.yellow).concat(text); | ||
return this; | ||
@@ -103,3 +147,3 @@ }; | ||
Colorizer.prototype.blue = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.blue).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.blue).concat(text); | ||
return this; | ||
@@ -114,3 +158,3 @@ }; | ||
Colorizer.prototype.magenta = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.magenta).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.magenta).concat(text); | ||
return this; | ||
@@ -125,3 +169,3 @@ }; | ||
Colorizer.prototype.cyan = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.cyan).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.cyan).concat(text); | ||
return this; | ||
@@ -136,3 +180,3 @@ }; | ||
Colorizer.prototype.white = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.white).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.white).concat(text); | ||
return this; | ||
@@ -147,3 +191,3 @@ }; | ||
Colorizer.prototype.bold = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.bold).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.bold).concat(text); | ||
return this; | ||
@@ -158,3 +202,3 @@ }; | ||
Colorizer.prototype.blink = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.blink).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.blink).concat(text); | ||
return this; | ||
@@ -169,3 +213,3 @@ }; | ||
Colorizer.prototype.conceal = function (text) { | ||
this._colors = "".concat(this._colors).concat(COLORS.conceal).concat(text); | ||
this.__string = "".concat(this.__string).concat(COLORS.conceal).concat(text); | ||
return this; | ||
@@ -179,3 +223,3 @@ }; | ||
Colorizer.prototype.toString = function () { | ||
return "".concat(this._colors, "\u001B[0m"); | ||
return "".concat(this.__string, "\u001B[0m"); | ||
}; | ||
@@ -187,3 +231,4 @@ /** | ||
Colorizer.prototype.resolve = function () { | ||
console.warn('Coloring.prototype.resolve() is deprecated. Use Coloring.prototype.toString() instead.'); | ||
((typeof console === 'undefined' ? function () { } : console.warn) || | ||
(typeof console === 'undefined' ? function () { } : console.log))('Coloring.prototype.resolve() is deprecated. Use Coloring.prototype.toString() instead.'); | ||
return this.toString(); | ||
@@ -195,5 +240,4 @@ }; | ||
/** | ||
* @deprecated Use {@link Colorizer} instead. | ||
* @deprecated This is a deprecated alias for the `Colorizer` class. Use {@link Colorizer} instead. | ||
*/ | ||
var Coloring = Colorizer; | ||
exports.Coloring = Coloring; | ||
exports.Coloring = Colorizer; |
@@ -1,17 +0,32 @@ | ||
type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'bold' | 'blink' | 'conceal'; | ||
type Effect = 'bold' | 'blink' | 'conceal'; | ||
type Parameter = Color | Effect; | ||
type ParameterArray = Parameter[]; | ||
declare type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'bold' | 'blink' | 'conceal'; | ||
declare type Effect = 'bold' | 'blink' | 'conceal'; | ||
declare type ColorOrEffect = Color | Effect; | ||
declare type ArrayOfColorsOrEffects = ColorOrEffect[]; | ||
/** | ||
* Color `string` with color(s) `color`. | ||
* Color `str` with color `color`. | ||
* | ||
* @param string The string to paint in color! | ||
* @param color The colors to paint the string in. | ||
* @param str The string to paint in color! | ||
* @param color The color to paint the string in. | ||
*/ | ||
declare function coloring(str: string, color: ParameterArray | Parameter): string; | ||
export declare function coloring(str: string, color: ColorOrEffect): string; | ||
/** | ||
* Color `str` with colors `colors`. | ||
* | ||
* @param str The string to paint in color! | ||
* @param colors The colors to paint the string in. | ||
*/ | ||
export declare function coloring(str: string, colors: ArrayOfColorsOrEffects): string; | ||
/** | ||
* Colors `text` in a rainbow pattern. | ||
* | ||
* @param str The string to paint in color! | ||
* @returns The colored string. | ||
*/ | ||
export declare function rainbowify(str: string): string; | ||
/** | ||
* @class This is a colorizer class. | ||
*/ | ||
declare class Colorizer { | ||
private _colors; | ||
export declare class Colorizer { | ||
private __string; | ||
constructor(); | ||
/** | ||
@@ -107,5 +122,5 @@ * Paints `text` in black. | ||
/** | ||
* @deprecated Use {@link Colorizer} instead. | ||
* @deprecated This is a deprecated alias for the `Colorizer` class. Use {@link Colorizer} instead. | ||
*/ | ||
declare const Coloring: typeof Colorizer; | ||
export { coloring, Colorizer, Coloring }; | ||
export declare const Coloring: typeof Colorizer; | ||
export type { ArrayOfColorsOrEffects, ColorOrEffect, Color, Effect }; |
{ | ||
"name": "@santi100/coloring-lib", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"repository": { | ||
"url": "https://github.com/santi100a/coloring-lib" | ||
}, | ||
"main": "index.js", | ||
"keywords": [ | ||
"coloring", "ansi", "escape-sequences", "es3" | ||
], | ||
"description": "Santi's Coloring Library: Make your text look really cool!", | ||
"main": "cjs/index.js", | ||
"module": "./index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@santi100/assertion-lib": "^1.0.3" | ||
"@santi100/assertion-lib": "^1.0.6" | ||
}, | ||
"type": "module", | ||
"devDependencies": { | ||
"@types/jest": "^29.4.0", | ||
"jest": "^29.4.2", | ||
"typescript": "^4.9.5" | ||
"typescript": "4.8.4" | ||
}, | ||
@@ -18,0 +22,0 @@ "scripts": { |
@@ -1,38 +0,80 @@ | ||
# Santi's Coloring Library (JavaScript) | ||
[![Build Status](https://github.com/santi100a/coloring-lib/actions/workflows/main.yml/badge.svg)](https://github.com/santi100a/santitools-python/actions) | ||
[![GitHub stars](https://img.shields.io/github/stars/santi100a/coloring-lib.svg)](https://github.com/santi100a/santitools-python) | ||
[![License](https://img.shields.io/github/license/santi100a/coloring-lib.svg)](https://github.com/santi100a/santitools-python) | ||
# Santi's Coloring Library | ||
[![Build Status](https://github.com/santi100a/coloring-lib/actions/workflows/test.yml/badge.svg)](https://github.com/santi100a/coloring-lib/actions) | ||
[![npm homepage](https://img.shields.io/npm/v/@santi100/coloring-lib)](https://npmjs.org/package/@santi100/coloring-lib) | ||
[![GitHub stars](https://img.shields.io/github/stars/santi100a/coloring-lib.svg)](https://github.com/santi100a/coloring-lib) | ||
[![License](https://img.shields.io/github/license/santi100a/coloring-lib.svg)](https://github.com/santi100a/coloring-lib) | ||
[![Bundlephobia stats](https://img.shields.io/bundlephobia/min/@santi100/coloring-lib)](https://bundlephobia.com/package/@santi100/coloring-lib@latest) | ||
- 🚀 Lightweight and fast^ | ||
- 👴 ES3-compliant* | ||
- 💻 Portable between the browser and Node.js | ||
**Hasn't been tested in an actual ES3 environment. Feel free to open an issue or pull request if you find any non-ES3 thing. See "Contribute" for instructions on how to do so.* | ||
*^The source code is about 2 kilobytes.* | ||
## What's this? | ||
This is a coloring library that uses ANSI sequences to color text on a terminal. | ||
Be aware there might be bugs hidden in this code. Pull requests and issues are welcome! | ||
## Usage | ||
## Contribute | ||
### Importing from a JavaScript File | ||
Wanna contribute? [File an issue](https://github.com/santi100a/coloring-lib/issues) or [pull request](https://github.com/santi100a/coloring-lib/pulls)! | ||
Make sure you follow the [contribution Code of Conduct](https://github.com/santi100a/coloring-lib/blob/main/CODE_OF_CONDUCT.md). | ||
## Installation | ||
- Via NPM: `npm install @santi100/coloring-lib` | ||
- Via Yarn: `yarn add @santi100/coloring-lib` | ||
- Via PNPM: `pnpm install @santi100/coloring-lib` | ||
#### Via NPM | ||
Import ```@santi100/coloring-lib``` (for ESM) or ```@santi100/coloring-lib/cjs``` (for CJS). | ||
#### Via Source Control | ||
## API | ||
Import ```./coloring-lib/index.js``` (ESM) or ```./coloring-lib/cjs/index.js``` (CJS). | ||
### Installation | ||
### Functions | ||
- `coloring(str: string, color: ColorOrEffect): string;` | ||
Color `str` with color `color`. | ||
#### Via NPM | ||
| Parameter | Type | Description | | ||
|-----------|-----------------|-------------------------------------| | ||
| str | `string` | The string to paint in color. | | ||
| color | `ColorOrEffect` | The color to paint the string in. | | ||
- Run ```npm install @santi100/coloring-lib```, ```yarn add @santi100/coloring-lib```, or ```pnpm install @santi100/coloring-lib```, depending on what package manager you use in your project. | ||
Returns a string of the colored text. | ||
#### Via Source Control | ||
- `coloring(str: string, colors: ArrayOfColorsOrEffects): string;` | ||
Color `str` with colors `colors`. | ||
- Run ```git clone https://github.com/santi100a/coloring-lib```. | ||
- Enter the directory and run ```npm install```, ```yarn install```, or ```pnpm install``` to install dependencies. | ||
| Parameter | Type | Description | | ||
|-----------|--------------------------|-----------------------------------------------| | ||
| `str` | `string` | The string to paint in color. | | ||
| `colors` | `ArrayOfColorsOrEffects` | The colors to paint the string in. | | ||
Returns a string of the colored text. | ||
### Functions | ||
- `rainbowify(str: string): string;` | ||
Colors `str` in a rainbow pattern. | ||
```coloring(string: string, color: Color): string;``` This function takes in a string to color, and a color, which | ||
can be one of 11 colors/effects: red, yellow, green, black, blue, magenta, cyan, white, bold, conceal and blink. | ||
| Parameter | Type | Description | | ||
|-----------|-------------------------|----------------------------------------------| | ||
| `str` | `string` | The string to paint in color. | ||
```coloring(string: string, color: Color[]): string;``` This function takes in a string to color, and an array of the aforementioned colors/effects. If you put two colors, the latter one/s will override the former/s. | ||
Returns a string of the rainbow colored text. | ||
```class Coloring```: This is a colorizer object class. Its instance methods are called the same as the | ||
colors/effects, and you can chain them. To convert the object to a string, call the ```Coloring.prototype.resolve()``` method. | ||
### Classes | ||
- `class Colorizer`: This is a colorizer object class. | ||
Its instance methods are called the same as the colors/effects, and you can chain them. | ||
To convert the object to a string, call the `Colorizer.prototype.toString()` method. | ||
`Colorizer.prototype.resolve()` **is a deprecated alias for** `Colorizer.prototype.toString()`. | ||
**Use** `Colorizer.prototype.toString()` **instead.** | ||
**The** `Coloring` **class is a deprecated alias for the** `Colorizer` **class.** | ||
**Use** `Colorizer` **instead.** | ||
## Types | ||
- `type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'bold' | 'blink' | 'conceal';` | ||
A color to paint a string in. | ||
- `type Effect = 'bold' | 'blink' | 'conceal';` | ||
An effect you can give to a string. | ||
- `type ColorOrEffect = Color | Effect;` | ||
A valid color or effect. | ||
- `type ArrayOfColorsOrEffects = ColorOrEffect[];` | ||
An array of valid colors or effects. |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26208
14
503
80
0
No