Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ts/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export function leftPad(str: string, space: number, padWith = ' '): string {
* ```
*/
export function leftPadMin(str: string, length: number, padWith = ' '): string {
if (stripAsni(str).length > length)
const strippedLength = stripAsni(str).length;

if (strippedLength > length)
throw new Error('String length is greater than the length provided.');

return padWith.repeat(length - stripAsni(str).length) + str;
return padWith.repeat(length - strippedLength) + str;
}

/** Adds the `padWith` (default `' '`) to the string the amount of times specified by the `space` argument
Expand Down Expand Up @@ -70,10 +72,12 @@ export function rightPad(str: string, space: number, padWith = ' '): string {
* ```
*/
export function rightPadMin(str: string, length: number, padWith = ' '): string {
if (stripAsni(str).length > length)
const strippedLength = stripAsni(str).length;

if (strippedLength > length)
throw new Error('String length is greater than the length provided.');

return str + padWith.repeat(length - stripAsni(str).length);
return str + padWith.repeat(length - strippedLength);
}

/** Pads the string with the `padWith` so that it appears in the center of a new string with the provided length.
Expand All @@ -93,11 +97,13 @@ export function rightPadMin(str: string, length: number, padWith = ' '): string
* ```
*/
export function centerPad(str: string, length: number, padWith = ' '): string {
if (stripAsni(str).length > length) {
const strippedLength = stripAsni(str).length;

if (strippedLength > length) {
throw new Error('String length is greater than the length provided.');
}

const overflow = length - stripAsni(str).length;
const overflow = length - strippedLength;

const paddingLeft = Math.floor(overflow / 2);

Expand Down
Loading