ansi-center-align
v1.0.3
Published
Center-align the text in a string. Supports ANSI styles.
Downloads
63
Readme
ansi-center-align
Center-align the text in a string. Handles ANSI styles.
This package was based on the wonderful center-align package created by Jon Schlinkert
Install
Install with your favorite package manager:
npm install --save ansi-center-align
Usage
import { centerAlign } from 'ansi-center-align';
centerAlign(val, width);
Params
val
{string | string[]}: the string or array of strings to center alignwidth
{number} (optional): the total width of each line in the expected result, after it's centered.
Examples
console.log(centerAlign('foo')); //=> 'foo' (does nothing)
console.log(centerAlign('foo', 12)); //=> ' foo '
console.log(centerAlign('foo', 10)); //=> ' foo '
console.log(centerAlign('foo', 8)); //=> ' foo '
Multiple lines
If expected width
is not provided, the length of the longest line will be used.
Example
If used on the following:
// value can be a string, or an array of strings
centerAlign([
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing',
'elit, sed do eiusmod tempor incididunt',
'ut labore et dolore',
'magna aliqua. Ut enim ad minim',
'veniam, quis'
]);
The result would be:
Lorem ipsum dolor sit amet,
consectetur adipiscing
elit, sed do eiusmod tempor incididunt
ut labore et dolore
magna aliqua. Ut enim ad minim
veniam, quis
About
Related projects
- center-align: Center-align the text in a string. (but doesn't handle ANSI styles). | homepage
- align-text: Align the text in a string. | homepage
- justified: Wrap words to a specified length and justified the text. | homepage
- right-align: Right-align the text in a string. | homepage
- word-wrap: Wrap words to a specified length. | homepage