{"id":15939570,"url":"https://github.com/prashanthr/ts-utils","last_synced_at":"2025-04-03T20:21:36.886Z","repository":{"id":193951763,"uuid":"679359750","full_name":"prashanthr/ts-utils","owner":"prashanthr","description":"A typescript utility library for functional programming","archived":false,"fork":false,"pushed_at":"2023-12-01T04:46:11.000Z","size":65,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-29T14:20:28.825Z","etag":null,"topics":["functional-programming","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prashanthr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"prashanthr","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://www.buymeacoffee.com/TGuwXOA","https://paypal.me/prashanthr"]}},"created_at":"2023-08-16T16:56:52.000Z","updated_at":"2023-11-27T16:54:57.000Z","dependencies_parsed_at":"2023-12-01T05:41:08.102Z","dependency_job_id":null,"html_url":"https://github.com/prashanthr/ts-utils","commit_stats":null,"previous_names":["prashanthr/ts-utils"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashanthr%2Fts-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashanthr%2Fts-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashanthr%2Fts-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prashanthr%2Fts-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prashanthr","download_url":"https://codeload.github.com/prashanthr/ts-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247070970,"owners_count":20878592,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional-programming","typescript"],"created_at":"2024-10-07T06:05:08.491Z","updated_at":"2025-04-03T20:21:36.863Z","avatar_url":"https://github.com/prashanthr.png","language":"TypeScript","funding_links":["https://github.com/sponsors/prashanthr","https://www.buymeacoffee.com/TGuwXOA","https://paypal.me/prashanthr"],"categories":[],"sub_categories":[],"readme":"# ts-utils\n\nA typescript utility library to help utilize functional programming concepts by re-creating monads like Option (Scala), Either (Scala) and Result (Rust) in Typescript.\n\nCheck out the [blog post](https://dev.to/prashanthr/level-up-your-typescript-game-functionally-part-1-57g4) for a good overview.\n\n# Installation\n\n[NPM package](https://www.npmjs.com/package/@universal-apps/ts-utils)\n\n```\nnpm i @universal-apps/ts-utils\n# OR\npnpm i @universal-apps/ts-utils\n```\n\n## Library\n\n[Option types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/option.ts)\n\n[Either types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/either.ts)\n\n[Result types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/result.ts)\n\n[Result tuple types](https://github.com/prashanthr/ts-utils/tree/main/src/lib/result-tuple.ts)\n\n## Usage \u0026 Examples\n\n```typescript\n// Option\nconst someValue: Option\u003cnumber\u003e = some(42) // { value: 42 }\nconst noValue: Option\u003cnumber\u003e = none() // {}\n\n// Utilities to match on the Option type\nmatchOptionF({\n    some: (value) =\u003e value,\n    none: () =\u003e undefined,\n})(someValue)\n\n// Either\nconst rightFn = (): Either\u003cError, number\u003e =\u003e right(42) // { value: 42 }\nconst leftFn = (): Either\u003cError, number\u003e =\u003e left(new Error('bad')) // { error: Error('bad) }\n\n// Utilities to match on the Either type\nmatchEitherF\u003cError, number, Error, number\u003e({\n    right: (result) =\u003e result,\n    left: (err) =\u003e err,\n})(rightFn)\n\n// Result\nconst okFn = (): Result\u003cnumber, Error\u003e =\u003e ok(42) // { value: 42 }\nconst errFn = (): Result\u003cnumber, Error\u003e =\u003e err(new Error('bad')) // { error: Error('bad') }\n\n// Utilities to match on the Result type\nmatchResultF({\n    ok: (result) =\u003e result,\n    err: (err) =\u003e err,\n})(okFn())\n// OR\nmatchResultF\u003cnumber, Error, number, Error\u003e({\n    ok: (result) =\u003e result,\n    err: (err) =\u003e err,\n})(okFn())\n\n// Result Tuple\nconst result: ResultT\u003cnumber\u003e = 42 // 1\nconst error: ErrorT = new Error('bad') // Error('bad')\n\nconst resultTuple: ResultTuple\u003cnumber\u003e = toTuple({ result }) // [42, undefined]\n// OR\nconst resultTuple: ResultTuple\u003cnumber\u003e = toTuple({ error }) // [undefined, new Error('bad')]\n\n// Say goodbye to try/catch and use it this way\nconst aPromise = async (): Promise\u003cResultTuple\u003cnumber\u003e\u003e =\u003e\n    Promise.resolve(toTuple({ result: 42 }))\n\nconst fn = async (): Promise\u003cany\u003e =\u003e {\n    const [result, error] = await aPromise()\n    if (error) {\n        // do something with error\n        throw error\n    } else {\n        // do something with result\n        console.log(`Result is ${result}`)\n    }\n}\n```\n\nMore examples can be found [here](https://github.com/prashanthr/ts-utils/tree/main/src/examples/)\n\n## Tests\n\nThe tests can be found [here](https://github.com/prashanthr/ts-utils/tree/main/src/test/)\n\n```\npnpm run test\n# or\npnpm run test:ui\n```\n\n## Inspiration\n\nInspired by [this article](https://imhoff.blog/posts/using-results-in-typescript) by Dan Imhoff.\n\n## Resources\n\n[fp-ts](https://github.com/gcanti/fp-ts) library\n\n[EffectTS](https://github.com/Effect-TS/effect) library\n\n[pratica](https://github.com/rametta/pratica)\n\n[Functional Programming Series](https://www.youtube.com/playlist?list=PLuPevXgCPUIMbCxBEnc1dNwboH6e2ImQo) on Youtube\n\n## Publishing\n\n```\npnpm version \u003cmajor|minor|patch\u003e\npnpm publish --dry-run --publish-branch \u003ccurrent-branch\u003e\npnpm publish\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashanthr%2Fts-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprashanthr%2Fts-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprashanthr%2Fts-utils/lists"}