{"id":19250754,"url":"https://github.com/oakfang/adt","last_synced_at":"2026-06-12T01:31:09.207Z","repository":{"id":238185172,"uuid":"796048574","full_name":"oakfang/adt","owner":"oakfang","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-14T20:07:59.000Z","size":42,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-05T06:26:47.864Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oakfang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-05-04T19:26:44.000Z","updated_at":"2024-10-14T10:47:53.000Z","dependencies_parsed_at":"2024-05-08T12:25:32.048Z","dependency_job_id":"560873b2-ced4-4f7a-b787-d0602cfb985a","html_url":"https://github.com/oakfang/adt","commit_stats":null,"previous_names":["oakfang/adt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oakfang%2Fadt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oakfang%2Fadt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oakfang%2Fadt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oakfang%2Fadt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oakfang","download_url":"https://codeload.github.com/oakfang/adt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240347430,"owners_count":19787225,"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":[],"created_at":"2024-11-09T18:18:34.391Z","updated_at":"2026-06-12T01:31:09.145Z","avatar_url":"https://github.com/oakfang.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Algebraic Data Types for typescript\n\nThis is a somewhat naive implementation of ADTs for typescript.\n\n## `option` - the \"maybe\" type module\n\n### Main Exports\n\n- `none(): None` create an instance of `Option\u003cT\u003e` that holds no value (unit type)\n- `some\u003cT\u003e(value: T): Some\u003cT\u003e` create an instance of `Option\u003cT\u003e` that holds a value\n- `type Option\u003cT\u003e = None | Option\u003cT\u003e`\n\n### Utilities\n\n- `isNone(value)` - is provided argument of type `None`\n- `getOrElse\u003cT\u003e(option: Option\u003cT\u003e, default: T): T` - get an optional value if exists, fallback to default otherwise\n- `fromNullable\u003cT\u003e(value: T | null | undefined): Option\u003cT\u003e` create an `Option\u003cT\u003e` from a possibly nullable value, where nullish become `None` and other values become `Some\u003cT\u003e`\n\n### Example Usage\n\nLet's say we have a sparse array of numbers. We'd like to sum it, but some members of the array might be nullish. We can't use falsy values as indication (because 0), so let's try to use options instead.\n\n```ts\nimport { option } from \"@oakfang/adt\";\n\nconst numbers: Array\u003cnull | number\u003e = [\n  /*...*/\n];\nconst sum = numbers\n  // Array\u003cnull | number\u003e -\u003e Array\u003cOption\u003cnumber\u003e\u003e\n  .map(fromNullable)\n  // Array\u003cOption\u003cnumber\u003e\u003e -\u003e number[]\n  .map((o) =\u003e getOrElse(o, 0))\n  // number[] -\u003e number\n  .reduce((x, y) =\u003e x + y, 0);\n```\n\n## `Result\u003cLeft, Right\u003e` - the \"computation that might fail\" type\n\nSome function might throw, but the issue with that is that neither JS nor TS provide us with a way to account for these thrown errors in a type-safe manner. The `Result` type aims to remove the need for thrown exception, as it encapsulates both a function that follows the \"happy flow\" (i.e., returns) and one that doesn't.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foakfang%2Fadt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foakfang%2Fadt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foakfang%2Fadt/lists"}