Standard libraries that lift up your code.
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.
 
 
 

63 lines
1.6 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
}
/**
* Convert a string to PascalCase.
* @param input
*/
export function stringToPascal(input: string): string {
return input.split(/[\s_]+/i)
.map(part => {
return part[0].toUpperCase() + part.substr(1)
})
.join('')
.split(/\W+/i)
.join('')
}