@cafeine-software/padcenter
v1.0.4
Published
A lightweight utility to pad and center strings within a given width
Downloads
29
Maintainers
Readme
Pad Center
A lightweight utility to pad and center strings within a given width
Installation
npm install @cafeine-software/padcenterUsage
import { 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 'Documentation
padCenter(str, width, [char])
Pads a string to center it within a given width using a specified character.
Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| str | string | - | The string to pad (required) |
| width | number | - | The final width (required) |
| char | string | ' ' | The padding character (optional) |
Returns
string- The padded and centered string
Throws
Error- Ifstris not a valid stringError- Ifwidthis not a finite numberError- Ifcharis not a stringError- Ifcharis not a single character
Edge Cases
- 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
License
Contact
Developed by Quentin Lamamy.
- Email: [email protected]
- GitHub: github.com/quentin-lamamy
- Cafeine Github: github.com/Cafeine-Software
