-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ieedan/add-pad
feat: Add `pad` for left and right pad utilities
- Loading branch information
Showing
6 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-blocks": minor | ||
--- | ||
|
||
Add `pad` utility for left and right pad functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect, test } from 'vitest'; | ||
import { leftPad, rightPad } from './pad'; | ||
|
||
// leftPad | ||
|
||
test('leftPad: Correct padding', () => { | ||
expect(leftPad('Hello', 3)).toBe(' Hello'); | ||
}); | ||
|
||
test('leftPad: Correct padding `padWith`', () => { | ||
expect(leftPad('Hello', 3, '.')).toBe('...Hello'); | ||
}); | ||
|
||
test('leftPad: Correct padding with 0', () => { | ||
expect(leftPad('Hello', 0)).toBe('Hello'); | ||
}); | ||
|
||
// rightPad | ||
|
||
test('rightPad: Correct padding', () => { | ||
expect(rightPad('Hello', 3)).toBe('Hello '); | ||
}); | ||
|
||
test('rightPad: Correct padding `padWith`', () => { | ||
expect(rightPad('Hello', 3, '.')).toBe('Hello...'); | ||
}); | ||
|
||
test('rightPad: Correct padding with 0', () => { | ||
expect(rightPad('Hello', 0)).toBe('Hello'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** Adds the `padWith` (default `' '`) to the string the amount of times specified by the `space` argument | ||
* | ||
* @param str String to add padding to | ||
* @param space Whitespace to add | ||
* @param padWith Character to use to pad the string | ||
* @returns | ||
* | ||
* ## Usage | ||
* ```ts | ||
* const padded = leftPad("Hello", 3, "."); | ||
* | ||
* console.log(padded); // '...Hello' | ||
* ``` | ||
*/ | ||
const leftPad = (str: string, space: number, padWith = ' ') => { | ||
return padWith.repeat(space) + str; | ||
}; | ||
|
||
/** Adds the `padWith` (default `' '`) to the string the amount of times specified by the `space` argument | ||
* | ||
* @param str String to add padding to | ||
* @param space Whitespace to add | ||
* @param padWith Character to use to pad the string | ||
* @returns | ||
* | ||
* ## Usage | ||
* ```ts | ||
* const padded = rightPad("Hello", 3, "."); | ||
* | ||
* console.log(padded); // 'Hello...' | ||
* ``` | ||
*/ | ||
const rightPad = (str: string, space: number, padWith = ' ') => { | ||
return str + padWith.repeat(space); | ||
}; | ||
|
||
export { leftPad, rightPad }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters