Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add pad for left and right pad utilities #30

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/ten-crabs-repeat.md
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.
30 changes: 30 additions & 0 deletions blocks/utilities/pad.test.ts
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');
});
37 changes: 37 additions & 0 deletions blocks/utilities/pad.ts
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 };
1 change: 0 additions & 1 deletion blocks/utilities/sleep.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect, test } from 'vitest';
import { sleep } from './sleep';

// This test takes a bit and its a very simple function so feel free to take it out
test('Expect time elapsed', async () => {
const start = Date.now();

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
"type": "git",
"url": "git+https://github.com/ieedan/ts-blocks"
},
"keywords": [
"changelog",
"date"
],
"keywords": ["changelog", "date"],
"author": "Aidan Bleser",
"license": "MIT",
"bugs": {
Expand Down
3 changes: 3 additions & 0 deletions src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const blocks: Record<string, Block> = {
sleep: {
category: 'utilities',
},
pad: {
category: 'utilities',
},
};

export { blocks };