-
Notifications
You must be signed in to change notification settings - Fork 16
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 #8 from ieedan/improved-documentation
feat: Add tests that can optionally ship with the code
- Loading branch information
Showing
22 changed files
with
1,194 additions
and
38 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 | ||
--- | ||
|
||
Fix `init` command in CLI to allow for fully automated use. |
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 tests to code and optionally allow including them when adding blocks |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# ts-blocks | ||
|
||
Well documented self owned building blocks for typescript applications. | ||
**Well documented**, **tested**, **self owned** building blocks for typescript applications. | ||
|
||
```bash | ||
npx ts-blocks init | ||
|
@@ -16,19 +16,56 @@ npx ts-blocks init | |
|
||
## Adding Blocks | ||
|
||
### Single | ||
|
||
```bash | ||
npx ts-blocks add result | ||
|
||
┌ ts-block | ||
┌ ts-blocks | ||
│ | ||
◇ Added result | ||
│ | ||
└ All done! | ||
``` | ||
|
||
### Multiple | ||
|
||
```bash | ||
npx ts-blocks add result array-sum | ||
|
||
┌ ts-blocks | ||
│ | ||
◇ Added result | ||
│ | ||
◇ Added array-sum | ||
│ | ||
└ All done! | ||
``` | ||
|
||
# Blocks | ||
|
||
All blocks can be found under `./blocks` and are shipped with documentation. | ||
All blocks can be found under `./blocks`. | ||
|
||
## Documentation | ||
|
||
Each block is well documented including examples of usage. | ||
|
||
## Tests | ||
|
||
Each block is tested using [vitest](https://vitest.dev/). By default we add these tests to your repository to disable this behavior configure `includeTests` in your `blocks.json` file. | ||
|
||
> [!NOTE] | ||
> If [vitest](https://vitest.dev/) isn't already installed in your project we will attempt to install it for you. | ||
```json | ||
{ | ||
"$schema": "https://unpkg.com/[email protected]/schema.json", | ||
// ... | ||
"includeTests": false // disable including tests | ||
} | ||
``` | ||
|
||
# Development | ||
|
||
## Adding New Blocks | ||
|
||
|
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,20 @@ | ||
import { expect, test } from 'vitest'; | ||
import { Err, Ok, type Result } from './result'; | ||
|
||
const failingFunction = <E>(err: E): Result<boolean, E> => Err(err); | ||
|
||
const passingFunction = <T>(val: T): Result<T, string> => Ok(val); | ||
|
||
test('Expect correct passed result', () => { | ||
const [val, err] = passingFunction(true); | ||
|
||
expect(val).toBe(true); | ||
expect(err).toBe(null); | ||
}); | ||
|
||
test('Expect correct failed result', () => { | ||
const [val, err] = failingFunction('I failed!'); | ||
|
||
expect(val).toBe(null); | ||
expect(err).toBe('I failed!'); | ||
}); |
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,14 @@ | ||
import { expect, test } from 'vitest'; | ||
import { arraySum } from './array-sum'; | ||
|
||
test('Correct Sum Of All Elements', () => { | ||
const total = arraySum([1, 2, 3, 4, 5], (num) => num); | ||
|
||
expect(total).toBe(15); | ||
}); | ||
|
||
test('Expect 0 on empty', () => { | ||
const total = arraySum([], (num) => num); | ||
|
||
expect(total).toBe(0); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test } from 'vitest'; | ||
import { arrayToMap } from './array-to-map'; | ||
|
||
test('Correct returned map', () => { | ||
const expected = new Map(); | ||
expected.set(0, 1); | ||
expected.set(1, 2); | ||
expected.set(2, 3); | ||
|
||
const map = arrayToMap([1, 2, 3], (item, index) => [index, item]); | ||
|
||
expect(map).toStrictEqual(expected); | ||
}); | ||
|
||
test('Expect empty map', () => { | ||
const expected = new Map(); | ||
|
||
const map = arrayToMap([], (item, index) => [index, item]); | ||
|
||
expect(map).toStrictEqual(expected); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test } from 'vitest'; | ||
import { mapToArray } from './map-to-array'; | ||
|
||
test('Correct Sum Of All Elements', () => { | ||
const initialMap = new Map<number, number>(); | ||
initialMap.set(0, 1); | ||
initialMap.set(1, 2); | ||
initialMap.set(2, 3); | ||
|
||
const arr = mapToArray(initialMap, (_, value) => value); | ||
|
||
expect(arr).toStrictEqual([1, 2, 3]); | ||
}); | ||
|
||
test('Returns empty array', () => { | ||
const initialMap = new Map<number, number>(); | ||
|
||
const arr = mapToArray(initialMap, (_, value) => value); | ||
|
||
expect(arr).toStrictEqual([]); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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(); | ||
|
||
const duration = 25; | ||
|
||
await sleep(duration); | ||
|
||
const end = Date.now(); | ||
|
||
expect(end - start).toBeGreaterThanOrEqual(duration); | ||
}); |
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,26 @@ | ||
import { expect, test } from 'vitest'; | ||
import { truncate } from './truncate'; | ||
|
||
test('Correct forward', () => { | ||
const str = truncate('Hello World!', 5); | ||
|
||
expect(str).toBe('Hello'); | ||
}); | ||
|
||
test('Correct reverse', () => { | ||
const str = truncate('Hello World!', 6, { reverse: true }); | ||
|
||
expect(str).toBe('World!'); | ||
}); | ||
|
||
test('Correct forward with ending', () => { | ||
const str = truncate('Hello World!', 5, { ending: '...' }); | ||
|
||
expect(str).toBe('Hello...'); | ||
}); | ||
|
||
test('Correct reverse with ending', () => { | ||
const str = truncate('Hello World!', 6, { ending: '...', reverse: true }); | ||
|
||
expect(str).toBe('...World!'); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
{ | ||
"name": "ts-blocks", | ||
"description": "A CLI to add ts-blocks code to your project.", | ||
"description": "A CLI to blocks to your project.", | ||
"version": "0.1.0", | ||
"packageManager": "[email protected]", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ieedan/ts-blocks" | ||
}, | ||
"keywords": [ | ||
"changelog", | ||
"date" | ||
], | ||
"keywords": ["changelog", "date"], | ||
"author": "Aidan Bleser", | ||
"license": "MIT", | ||
"bugs": { | ||
|
@@ -36,19 +34,23 @@ | |
"lint": "biome lint --write", | ||
"check": "pnpm biome check", | ||
"ci:release": "unbuild && changeset publish", | ||
"changeset": "changeset" | ||
"changeset": "changeset", | ||
"test": "vitest" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.8.3", | ||
"@types/node": "^22.5.4", | ||
"typescript": "^5.5.3", | ||
"unbuild": "^2.0.0" | ||
"unbuild": "^2.0.0", | ||
"vitest": "^2.0.5" | ||
}, | ||
"dependencies": { | ||
"@changesets/cli": "^2.27.8", | ||
"@clack/prompts": "^0.7.0", | ||
"chalk": "^5.3.0", | ||
"commander": "^12.1.0", | ||
"execa": "^9.3.1", | ||
"package-manager-detector": "^0.2.0", | ||
"ts-morph": "^23.0.0", | ||
"valibot": "^0.41.0" | ||
} | ||
|
Oops, something went wrong.