{"id":15637426,"url":"https://github.com/anderscan/typed-option","last_synced_at":"2025-04-30T06:08:08.663Z","repository":{"id":23709551,"uuid":"99598179","full_name":"AndersCan/typed-option","owner":"AndersCan","description":"Library for working with Options in a type safe manner.","archived":false,"fork":false,"pushed_at":"2023-01-05T00:56:28.000Z","size":1025,"stargazers_count":5,"open_issues_count":14,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-30T06:08:02.273Z","etag":null,"topics":["option","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/AndersCan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-07T16:31:09.000Z","updated_at":"2021-06-05T13:40:58.000Z","dependencies_parsed_at":"2023-01-13T23:44:18.211Z","dependency_job_id":null,"html_url":"https://github.com/AndersCan/typed-option","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndersCan%2Ftyped-option","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndersCan%2Ftyped-option/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndersCan%2Ftyped-option/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndersCan%2Ftyped-option/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndersCan","download_url":"https://codeload.github.com/AndersCan/typed-option/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251651233,"owners_count":21621716,"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":["option","typescript"],"created_at":"2024-10-03T11:11:37.270Z","updated_at":"2025-04-30T06:08:08.637Z","avatar_url":"https://github.com/AndersCan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# typed-option\n[![Build Status](https://travis-ci.org/AndersCan/typed-option.svg?branch=master)](https://travis-ci.org/AndersCan/typed-option)\n[![codecov](https://codecov.io/gh/AndersCan/typed-option/branch/master/graph/badge.svg)](https://codecov.io/gh/AndersCan/typed-option)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n\nLibrary for working with `Option`s in a type safe manner.\n\n## tl;dr\n`Option`s makes working with, potentially, `undefined` values more concise and easy to read.\n\n**Example**\n\nGiven the function:\n```javascript\nfunction canFailFn(x: string): string | undefined {\n  if (Math.random() \u003e 0.5) {\n    return x\n  }\n  return undefined\n}\n```\n\nTurn something like this:\n```javascript\nlet result = 'FAILED'\nconst a = canFailFn('SUCCESS') // a is string | undefined\nif (a) {\n  const b = canFailFn(a) // b is string | undefined\n  if (b) {\n    const c = canFailFn(b) // c is string | undefined\n    if (c) {\n      result = c // c is string\n    }\n  }\n}\nconsole.log(result)\n```\n\nInto this:\n```javascript\nimport { Option } from 'typed-option'\n\nconst result2 = Option.from('SUCCESS')\n  .map(v =\u003e canFailFn(v)) // v is of type string.\n  .map(v =\u003e canFailFn(v)) // v is of type string.\n  .map(v =\u003e canFailFn(v)) // v is of type string.\n  .getOrElse('FAILED')\nconsole.log(result2)\n\n// or just\nconst result3 = Option.from('SUCCESS')\n  .map(canFailFn)\n  .map(canFailFn)\n  .map(canFailFn)\n  .getOrElse('FAILED')\nconsole.log(result3)\n```\n\n## Intro\nAn `Option` is either of type `Some` or `None`.\n* **Some** - represents a value of something (a *defined* value)\n* **None** - represents a value of nothing (an *undefined* value)\n\nIn the example above, we start with a `Some('SUCCESS')`. Then we apply the function `canFailFn` multiple times to the value of our `Some`. If the computation fails at any point, we are returned a `None`.\n\nWith Options, you do not need to know if you have a `Some` or `None` when applying functions. Only when you need the result do you have to deal with the potential `None` case. This is typically done by calling the `.getOrElse` function (as seen above).\n\n### Unions with undefined\nOne thing to note is that `undefined` is removed from union types:\n```javascript\ninterface Foo {\n  a?: number\n}\nconst myFoo: Foo = {\n  a: 5\n}\nmyFoo.a // 'number | undefined`'\nconst fooOption = Option.from(myFoo.a) // Option\u003cnumber\u003e\nfooOption.map(a =\u003e a) // typeof a is number\n```\nThis means that you will not have to worry about manually testing for `undefined`. A `Some` will never contain an `undefined` value.\n## Functions\n### Explanation\n\n#### Option.from\n\n`Option.from` gives you a `None` for `undefined` values and a `Some` for `defined` values.\n```javascript\nOption.from(true) // Some(true)\nOption.from(false) // Some(false)\nOption.from({}) // Some({})\nOption.from(0) // Some(0)\nOption.from('') // Some('')\nOption.from(undefined) // None\n```\nYou can also pass an additional predicate of what a legal value is.\n```javascript\nOption.from(false, () =\u003e true) // Some(false)\nOption.from(true, () =\u003e false) // None\nOption.from(undefined, () =\u003e true) // None\n\n```\n#### do\n\n`do` runs a fn on a Option if it is of type `Some`. Similar to `map`, but does not alter/return a new Option.\n```javascript\nsome('world').do(text =\u003e console.log(text)) // logs: 'world'\nnone().do((text) =\u003e console.log(text)) // logs:\n```\n\n#### map\n\n`map` apply a function to a Option if it is of type `Some`\n```javascript\nsome('world').map((text) =\u003e \"Hello, \" + text) // Some('Hello, world')\nnone().map((text) =\u003e \"Hello, \" + text) // None\n```\n\n#### flatMap\n`flatMap` Same as map, but for when your function returns an Option. `flatMap` will remove the nested option.\n```javascript\nfunction getOptionFn(...): Option\u003cstring\u003e\nconst option = ... // Some('Hello, Option')\noption.map(getOptionFn) // Some(Some('Hello, Option'))\noption.flatMap(getOptionFn) // Some('Hello, Option')\n```\n\n#### getOrElse\n\n`getOrElse` get the value from a `Some` or return the `else` value for a `None`\n```javascript\nsome(1).getOrElse(() =\u003e 999) // 1\nnone().getOrElse(() =\u003e 999) // 999\nsome(1).getOrElse(999) // 1\nnone().getOrElse(999) // 999\n```\n#### orElse\n\n`orElse` is the same as `getOrElse`, but `else` returns an Option\n```javascript\nsome(1).orElse(() =\u003e Some(999)) // Some(1)\nnone().orElse(() =\u003e Some(999)) // Some(999)\n```\n#### filter\n\n`filter` gives a predicate that a `Some` must hold. If not, returns `None`\n```javascript\nsome(1).filter((v) =\u003e false) // None()\nsome(100).filter((v) =\u003e true) // Some(100)\nnone().filter((v) =\u003e true) // None()\n```\n\n#### match\n`match` gives a `pattern matching` looking syntax to work with options.\n```javascript\nsome(1).match({\n  none: () =\u003e 'a none',\n  some: v =\u003e `a Some(${v})`\n}) // a Some(1)\n\nnone().match({\n  none: () =\u003e 'a none',\n  some: v =\u003e `a Some(${v})`\n}) // a none\n\noptionValueIgnored().match({\n  none: 'failure',\n  some: `success`\n})\n```\n#### isNone \u0026 isSome\n\n`isNone` tests if the given `Option` is of type `None`,\n`isSome` tests if the given `Option` is of type `Some`\n```javascript\nconst opt1 = ...\nif(opt1.isNone()){\n  // opt1 is None in this block\n}\nif(opt1.isSome()){\n  // opt1 is Some in this block\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanderscan%2Ftyped-option","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanderscan%2Ftyped-option","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanderscan%2Ftyped-option/lists"}