Skip to content

Commit

Permalink
Merge pull request #30 from ieedan/add-pad
Browse files Browse the repository at this point in the history
feat: Add `pad` for left and right pad utilities
  • Loading branch information
ieedan authored Sep 16, 2024
2 parents 3a44f8f + 261cccf commit 842b23e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
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 };

0 comments on commit 842b23e

Please sign in to comment.