{"id":25765471,"url":"https://github.com/yayoc/optional-chain","last_synced_at":"2025-02-26T22:19:30.686Z","repository":{"id":31875097,"uuid":"129890949","full_name":"yayoc/optional-chain","owner":"yayoc","description":"⛓Optional chaining implementation in TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-04T02:33:52.000Z","size":1788,"stargazers_count":19,"open_issues_count":15,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-18T20:45:12.287Z","etag":null,"topics":["option-type","optional-chaining","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/yayoc/optional-chain","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/yayoc.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":"2018-04-17T10:58:47.000Z","updated_at":"2021-06-29T12:35:02.000Z","dependencies_parsed_at":"2023-01-14T19:58:51.496Z","dependency_job_id":null,"html_url":"https://github.com/yayoc/optional-chain","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yayoc%2Foptional-chain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yayoc%2Foptional-chain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yayoc%2Foptional-chain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yayoc%2Foptional-chain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yayoc","download_url":"https://codeload.github.com/yayoc/optional-chain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240941882,"owners_count":19882115,"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-type","optional-chaining","typescript"],"created_at":"2025-02-26T22:19:30.194Z","updated_at":"2025-02-26T22:19:30.680Z","avatar_url":"https://github.com/yayoc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# optional-chain [![travis-ci](https://travis-ci.org/yayoc/optional-chain.svg?branch=master)](https://travis-ci.org/yayoc/optional-chain) [![Greenkeeper badge](https://badges.greenkeeper.io/yayoc/optional-chain.svg)](https://greenkeeper.io/)\n\nOptional chaining implementation in TypeScript.  \nUses [`option type`](https://en.wikipedia.org/wiki/Option_type)\n\n## Requirement\n\nThis library requires TS 2.8+ version to use [conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html)\n\n## Install\n\n```shell\nnpm install optional-chain\n```\n\n## Usage\n\n```typescript\nimport { optional } from \"optional-chain\";\n\ntype User = {\n  name?: {\n    first: string\n  }\n}\nconst user: User = getUser(); // { name: null }\nconst optionalUser = optional(user);\noptionalUser.k(\"name\").k(\"first\").get(); // undefined, does not throw an exception.\n```\n\n## API\n\n`optional-chain` library exports below APIs.\n\n### `optional`\n\n`optional` is a fuctory function to creates a `Option` instance.\n\n### `Option\u003cT\u003e`\n\nAn instance of `Option` can be constructed with a value for the souce of `T`. `Option` class can hold a type of `Some` or `None` based on given source.\n\n#### `.k(name: string)`\n\n```typescript\ntype User = {\n  name: string;\n  sns: SNS;\n  followers: User[];\n};\n\ntype SNS = {\n  twitter?: {\n    username: string;\n  };\n  facebook?: {\n    url: string;\n  };\n};\n\nconst user: User = {\n  name: \"yayoc\",\n  sns: {\n    twitter: {\n      username: \"@yayoc_\"\n    }\n  },\n  followers: []\n};\nconst optionalUser = optional(user);\noptionalUser.k(\"name\"); // Option\u003cstring\u003e\noptionalUser.k(\"sns\"); // Option\u003c{twitter: { username: string }}\u003e\noptionalUser.k(\"sns\").k(\"facebook\"); // None\noptionalUser.k(\"foo\"); // compile error\n```\n\nReturns a `Option\u003cT\u003e` narrowed by specified property of Object.\n\n#### `.i(index: number)`\n\nReturns a `Option\u003cT\u003e` narrowed by specified index of Array. If index is not in array, this returns `Option\u003cundefined\u003e`.\n\n```typescript\noptionalUser\n  .k(\"followers\")\n  .i(0)\n  .k(\"name\"); // None\n```\n\n#### `.get()`\n\nReturns a value of `Option`.\n\n```typescript\noptionalUser.k(\"name\").get(); // yayoc\n```\n\n#### `.match({ some: T =\u003e any, none: T =\u003e any })`\n\nA public method of `Option` to do pattern matching.\nIf target `Option` is `Some` type, this funciton returns a result of given `some` function. Otherwise, this function returns a result of given `none` function.\n\n```typescript\noptionalUser\n  .k(\"sns\")\n  .k(\"twitter\")\n  .match({\n    some: v =\u003e v,\n    none: v =\u003e `there is no account: ${v}`\n  }); // @yayoc_\n```\n\n#### `.getOrElse(value: any)`\n\nA public method to return `T` value when the instance contains some value. Otherwise, this function will return given value.\n\n```typescript\noptionalUser\n  .k(\"sns\")\n  .k(\"facebook\")\n  .k(\"url\")\n  .getOrElse(\"https://facebook.com\"); // https://facebook.com\n```\n\n## Credits\n\nThis library is highly inspired by [`lens.ts`](https://github.com/utatti/lens.ts) [@utatti](https://github.com/utatti/) created.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyayoc%2Foptional-chain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyayoc%2Foptional-chain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyayoc%2Foptional-chain/lists"}