You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/util/support/string.ts

50 lines
1.3 KiB

/**
* If `string` is less than `length` characters long, add however many `padWith` characters
* are necessary to make up the difference to the END of the string.
* @param string
* @param length
* @param padWith
*/
export function padRight(string: string, length: number, padWith = ' '): string {
while ( string.length < length ) {
string += padWith
}
return string
}
/**
* If `string` is less than `length` characters long, add however many `padWith` characters
* are necessary to make up the difference to the BEGINNING of the string.
* @param string
* @param length
* @param padWith
*/
export function padLeft(string: string, length: number, padWith = ' '): string {
while ( string.length < length ) {
string = `${padWith}${string}`
}
return string
}
/**
* If `string` is less than `length` characters long, add however many `padWith` characters
* are necessary to make up the difference evenly split between the beginning and end of the string.
* @param string
* @param length
* @param padWith
*/
export function padCenter(string: string, length: number, padWith = ' '): string {
const bit = false
while ( string.length < length ) {
if ( bit ) {
string = `${padWith}${string}`
} else {
string += padWith
}
}
return string
}