Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prashanthr/ts-utils
A typescript utility library for functional programming
https://github.com/prashanthr/ts-utils
functional-programming typescript
Last synced: 18 days ago
JSON representation
A typescript utility library for functional programming
- Host: GitHub
- URL: https://github.com/prashanthr/ts-utils
- Owner: prashanthr
- License: mit
- Created: 2023-08-16T16:56:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-01T04:46:11.000Z (12 months ago)
- Last Synced: 2024-05-02T06:07:41.229Z (7 months ago)
- Topics: functional-programming, typescript
- Language: TypeScript
- Homepage:
- Size: 63.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ts-utils
A typescript utility library to help utilize functional programming concepts by re-creating monads like Option (Scala), Either (Scala) and Result (Rust) in Typescript.
Check out the [blog post](https://dev.to/prashanthr/level-up-your-typescript-game-functionally-part-1-57g4) for a good overview.
# Installation
[NPM package](https://www.npmjs.com/package/@universal-apps/ts-utils)
```
npm i @universal-apps/ts-utils
# OR
pnpm i @universal-apps/ts-utils
```## Library
[Option types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/option.ts)
[Either types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/either.ts)
[Result types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/result.ts)
[Result tuple types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/result-tuple.ts)
## Usage & Examples
```typescript
// Option
const someValue: Option = some(42) // { value: 42 }
const noValue: Option = none() // {}// Utilities to match on the Option type
matchOptionF({
some: (value) => value,
none: () => undefined,
})(someValue)// Either
const rightFn = (): Either => right(42) // { value: 42 }
const leftFn = (): Either => left(new Error('bad')) // { error: Error('bad) }// Utilities to match on the Either type
matchEitherF({
right: (result) => result,
left: (err) => err,
})(rightFn)// Result
const okFn = (): Result => ok(42) // { value: 42 }
const errFn = (): Result => err(new Error('bad')) // { error: Error('bad') }// Utilities to match on the Result type
matchResultF({
ok: (result) => result,
err: (err) => err,
})(okFn())
// OR
matchResultF({
ok: (result) => result,
err: (err) => err,
})(okFn())// Result Tuple
const result: ResultT = 42 // 1
const error: ErrorT = new Error('bad') // Error('bad')const resultTuple: ResultTuple = toTuple({ result }) // [42, undefined]
// OR
const resultTuple: ResultTuple = toTuple({ error }) // [undefined, new Error('bad')]// Say goodbye to try/catch and use it this way
const aPromise = async (): Promise> =>
Promise.resolve(toTuple({ result: 42 }))const fn = async (): Promise => {
const [result, error] = await aPromise()
if (error) {
// do something with error
throw error
} else {
// do something with result
console.log(`Result is ${result}`)
}
}
```More examples can be found [here](https://github.com/prashanthr/ts-utils/tree/main/src/examples/)
## Tests
The tests can be found [here](https://github.com/prashanthr/ts-utils/tree/main/src/test/)
```
pnpm run test
# or
pnpm run test:ui
```## Inspiration
Inspired by [this article](https://imhoff.blog/posts/using-results-in-typescript) by Dan Imhoff.
## Resources
[fp-ts](https://github.com/gcanti/fp-ts) library
[EffectTS](https://github.com/Effect-TS/effect) library
[pratica](https://github.com/rametta/pratica)
[Functional Programming Series](https://www.youtube.com/playlist?list=PLuPevXgCPUIMbCxBEnc1dNwboH6e2ImQo) on Youtube
## Publishing
```
pnpm version
pnpm publish --dry-run --publish-branch
pnpm publish
```