{"id":13847326,"url":"https://github.com/thames-technology/monads","last_synced_at":"2025-04-07T21:14:16.078Z","repository":{"id":38049539,"uuid":"85808607","full_name":"thames-technology/monads","owner":"thames-technology","description":"Option, Result, and Either types for TypeScript - Inspired by Rust 🦀","archived":false,"fork":false,"pushed_at":"2024-04-29T16:26:30.000Z","size":1336,"stargazers_count":703,"open_issues_count":12,"forks_count":30,"subscribers_count":10,"default_branch":"main","last_synced_at":"2024-04-30T21:29:10.432Z","etag":null,"topics":["either","javascript","monads","node","nodejs","option","result","rust","types","typescript"],"latest_commit_sha":null,"homepage":"https://thames-technology.github.io/monads/","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/thames-technology.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","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":{"patreon":"thames"}},"created_at":"2017-03-22T09:22:27.000Z","updated_at":"2024-05-06T17:38:53.011Z","dependencies_parsed_at":"2024-01-14T04:41:08.866Z","dependency_job_id":"73ca54d6-537f-4c8d-9b9b-47d27a9d7adf","html_url":"https://github.com/thames-technology/monads","commit_stats":{"total_commits":284,"total_committers":14,"mean_commits":"20.285714285714285","dds":0.4859154929577465,"last_synced_commit":"fe0b45709cf3efa0a82b2602c3ef619553fbe21e"},"previous_names":["threestup/monads","qworks-io/monads","hqoss/monads","thames-technology/monads","sniptt-official/monads"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thames-technology%2Fmonads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thames-technology%2Fmonads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thames-technology%2Fmonads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thames-technology%2Fmonads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thames-technology","download_url":"https://codeload.github.com/thames-technology/monads/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247730069,"owners_count":20986404,"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":["either","javascript","monads","node","nodejs","option","result","rust","types","typescript"],"created_at":"2024-08-04T18:01:16.840Z","updated_at":"2025-04-07T21:14:16.052Z","avatar_url":"https://github.com/thames-technology.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://thames-technology.github.io/monads\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/thames-technology/monads/main/.github/assets/monads-cover.png\" alt=\"Monads Logo\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"right\"\u003e\n  \u003ci\u003eIf you use this repo, star it ✨\u003c/i\u003e\n\u003c/p\u003e\n\n---\n\n\u003ch2 align=\"center\"\u003eOption, Result, and Either types for JavaScript\u003c/h2\u003e\n\n\u003cp align=\"center\"\u003e\n  🦀 Inspired by \u003ca href=\"https://doc.rust-lang.org/stable/std/option/\" target=\"_blank\"\u003eRust\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cb\u003eZero dependencies\u003c/b\u003e • \u003cb\u003eLightweight\u003c/b\u003e • \u003cb\u003eFunctional\u003c/b\u003e\n\u003c/p\u003e\n\n---\n\n## Install\n\n```sh\nnpm install @thames/monads\n```\n\n## Getting started\n\n### The `Option\u003cT\u003e` type\n\nOption represents an optional value: every Option is either Some and contains a value, or None, and does not.\n\n\u003e [!NOTE]\n\u003e Full documentation here: [Option](https://thames-technology.github.io/monads/interfaces/Option.html)\n\n```ts\nimport { Option, Some, None } from '@thames/monads';\n\nconst divide = (numerator: number, denominator: number): Option\u003cnumber\u003e =\u003e {\n  if (denominator === 0) {\n    return None;\n  } else {\n    return Some(numerator / denominator);\n  }\n};\n\n// The return value of the function is an option\nconst result = divide(2.0, 3.0);\n\n// Pattern match to retrieve the value\nconst message = result.match({\n  some: (res) =\u003e `Result: ${res}`,\n  none: 'Cannot divide by 0',\n});\n\nconsole.log(message); // \"Result: 0.6666666666666666\"\n```\n\n### The `Result\u003cT, E\u003e` type\n\nResult represents a value that is either a success (Ok) or a failure (Err).\n\n\u003e [!NOTE]\n\u003e Full documentation here: [Result](https://thames-technology.github.io/monads/interfaces/Result.html)\n\n```ts\nimport { Result, Ok, Err } from '@thames/monads';\n\nconst getIndex = (values: string[], value: string): Result\u003cnumber, string\u003e =\u003e {\n  const index = values.indexOf(value);\n\n  switch (index) {\n    case -1:\n      return Err('Value not found');\n    default:\n      return Ok(index);\n  }\n};\n\nconst values = ['a', 'b', 'c'];\n\ngetIndex(values, 'b'); // Ok(1)\ngetIndex(values, 'z'); // Err(\"Value not found\")\n```\n\n### The `Either\u003cL, R\u003e` type\n\nEither represents a value that is either Left or Right. It is a powerful way to handle operations that can result in two distinctly different types of outcomes.\n\n\u003e [!NOTE]\n\u003e Full documentation here: [Either](https://thames-technology.github.io/monads/interfaces/Either.html)\n\n```ts\nimport { Either, Left, Right } from '@thames/monads';\n\nconst divide = (numerator: number, denominator: number): Either\u003cstring, number\u003e =\u003e {\n  if (denominator === 0) {\n    return Left('Cannot divide by 0');\n  } else {\n    return Right(numerator / denominator);\n  }\n};\n\nconst result = divide(2.0, 3.0);\n\nconst message = result.match({\n  left: (err) =\u003e `Error: ${err}`,\n  right: (res) =\u003e `Result: ${res}`,\n});\n\nconsole.log(message); // \"Result: 0.6666666666666666\"\n```\n","funding_links":["https://patreon.com/thames"],"categories":["TypeScript","typescript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthames-technology%2Fmonads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthames-technology%2Fmonads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthames-technology%2Fmonads/lists"}