Skip to content

Cafeine-Software/padCenter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

padCenter

A lightweight utility to pad and center strings within a given width

Installation

npm install @cafeine-software/padCenter

Usage

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 - If str is not a valid string
  • Error - If width is not a finite number
  • Error - If char is not a string
  • Error - If char is 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.

Support

About

[NPM Package] padCenter : pad and center strings within a given width

Topics

Resources

Stars

Watchers

Forks