A lightweight utility to pad and center strings within a given width
npm install @cafeine-software/padCenterimport { padCenter } from '@cafeine-software/padCenter';
// Basic centering with spaces
padCenter('hello', 11);
// => ' hello '
// Custom padding character
padCenter('hello', 11, '*');
// => '***hello***'
// Odd width distribution
padCenter('test', 9);
// => ' test 'Pads a string to center it within a given width using a specified character.
| Parameter | Type | Default | Description |
|---|---|---|---|
str |
string |
- | The string to pad (required) |
width |
number |
- | The final width (required) |
char |
string |
' ' |
The padding character (optional) |
string- The padded and centered string
Error- Ifstris not a valid stringError- Ifwidthis not a finite numberError- Ifcharis not a stringError- Ifcharis not a single character
- If the string length is greater than or equal to the width, the original string is returned unchanged
- When padding is odd, the extra character is added to the end
Developed by Quentin Lamamy.
- Email: contact@quentin-lamamy.fr
- GitHub: github.com/quentin-lamamy
- Cafeine Github: github.com/Cafeine-Software