{"id":24318636,"url":"https://github.com/jtaylorchang/js-optchain","last_synced_at":"2025-03-10T18:41:02.863Z","repository":{"id":53213732,"uuid":"249760533","full_name":"jtaylorchang/js-optchain","owner":"jtaylorchang","description":"Schema-based optional chaining for JavaScript. Used in production Node.js servers. Version 2.0.8 is available for installation on npm","archived":false,"fork":false,"pushed_at":"2021-04-22T18:54:29.000Z","size":120,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T19:13:57.763Z","etag":null,"topics":["javascript","js-optchain","optional","optional-chaining","schema-properties","wrapper"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/js-optchain","language":"JavaScript","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/jtaylorchang.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":"2020-03-24T16:33:15.000Z","updated_at":"2021-04-22T18:54:31.000Z","dependencies_parsed_at":"2022-09-08T13:32:10.837Z","dependency_job_id":null,"html_url":"https://github.com/jtaylorchang/js-optchain","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtaylorchang%2Fjs-optchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtaylorchang%2Fjs-optchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtaylorchang%2Fjs-optchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtaylorchang%2Fjs-optchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jtaylorchang","download_url":"https://codeload.github.com/jtaylorchang/js-optchain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242402896,"owners_count":20122342,"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":["javascript","js-optchain","optional","optional-chaining","schema-properties","wrapper"],"created_at":"2025-01-17T14:40:20.714Z","updated_at":"2025-03-10T18:41:02.832Z","avatar_url":"https://github.com/jtaylorchang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-optchain\n\n![](https://img.shields.io/npm/v/js-optchain) ![](https://img.shields.io/bundlephobia/min/js-optchain) ![](https://img.shields.io/npm/l/js-optchain) ![](https://img.shields.io/npm/dt/js-optchain) [![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/jtaylorchang/js-optchain.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/jtaylorchang/js-optchain/context:javascript)\n\nThis package adds schema-based optional chaining to javascript. Simply wrap the object with the `oc` and define your default schema. This will create an object containing all fields **specified in the schema** with the existing values or default ones from the schema if the data is undefined. The optional chain is recursively generated so schemas can be multi-level. See [Usage](#Usage) for examples.\n\n## Installation\n\nNPM:\n\n```bash\nnpm install js-optchain\n```\n\nYarn:\n\n```bash\nyarn add js-optchain\n```\n\n### Importing:\n\nWith **import**:\n\n```javascript\nimport oc from 'js-optchain';\n```\n\nWith **require**:\n\n```javascript\nconst oc = require('js-optchain').default;\n```\n\n## Usage\n\n```javascript\n/**\n * Wrap an object with optional properties which should have default values if undefined.\n *\n * To use partials, set the element in the schema to `{}`\n *\n * Partials are enabled by default. If partials are not enabled, the output will strictly match the schema shape.\n *\n * @param {*} optionalObj The object to wrap\n * @param {*} schema The schema with default values which should be returned\n * @param {*} allowPartials (Optional) True if elements in the wrapped object should be included even if they don't appear in the schema\n */\nconst ocObj = oc(obj, schema, allowPartials);\n```\n\n- The first argument is the object you want to explore that may or may not be `undefined`.\n- The second argument is the schema of its optional chain with default values.\n- There is an optional third argument that defaults to true and allows partial schemas (see handler/ocBody 6 below for an example)\n- The returned result is an object that will match the schema and have default values if the found values are `undefined`. This can include nested objects, see examples below.\n\n## Examples\n\nBelow are a series of examples that match the following. To view an example, see the `handler`, `oc()` and `console.log` for each number to see what the expected inputs and outputs are.\n\n- `ocBody1`: Passing an undefined object into the `oc` wrapper (object is undefined)\n- `ocBody2`: Passing an empty object into the `oc` wrapper (object has none of the schema properties)\n- `ocBody3`: Passing a partial object into the `oc` wrapper (object only has some of the schema properties)\n- `ocBody4`: Passing a complete object into the `oc` wrapper (object has all the schema properties)\n- `ocBody5`: Passing a nested object into the `oc` wrapper (schema contains multiple layers)\n- `ocEvent5`: Passing a more complex object into the `oc` wrapper\n- `ocEvent6`: Passing an object into the `oc` wrapper with partials\n- `ocEvent6NoPartials`: Passing an object into the `oc` wrapper with partials disabled\n\nTherefore, all of these inputs are valid:\n\n```javascript\n// import oc from \"js-optchain\";\nconst oc = require('js-optchain').default;\n\nconst handler1 = {\n  event: {}\n};\n\nconst handler2 = {\n  event: {\n    body: {}\n  }\n};\n\nconst handler3 = {\n  event: {\n    body: {\n      username: 'jeff'\n    }\n  }\n};\n\nconst handler4 = {\n  event: {\n    body: {\n      username: 'jeff',\n      password: 'password'\n    }\n  }\n};\n\nconst handler5 = {\n  event: {\n    body: {\n      user: {\n        username: 'jeff'\n      }\n    }\n  }\n};\n\nconst handler6 = {\n  event: {\n    body: {\n      user: {\n        username: 'jeff',\n        password: 'password'\n      }\n    }\n  }\n};\n\nconst ocBody1 = oc(handler1.event.body, {\n  username: 'defaultUsername',\n  password: 'defaultPassword'\n});\nconst ocBody2 = oc(handler2.event.body, {\n  username: 'defaultUsername',\n  password: 'defaultPassword'\n});\nconst ocBody3 = oc(handler3.event.body, {\n  username: 'defaultUsername',\n  password: 'defaultPassword'\n});\nconst ocBody4 = oc(handler4.event.body, {\n  username: 'defaultUsername',\n  password: 'defaultPassword'\n});\nconst ocBody5 = oc(handler5.event.body, {\n  user: {\n    username: 'defaultUsername',\n    password: 'defaultPassword'\n  }\n});\nconst ocEvent5 = oc(handler5.event, {\n  body: {\n    user: {\n      username: 'defaultUsername',\n      password: 'defaultPassword'\n    },\n    config: {\n      isAdmin: true\n    }\n  }\n});\nconst ocEvent6 = oc(\n  handler6.event,\n  {\n    body: {\n      user: {}\n    }\n  },\n  false\n);\nconst ocEvent6NoPartials = oc(\n  handler6.event,\n  {\n    body: {\n      user: {}\n    }\n  },\n  false\n);\n\nconsole.log(ocBody1.username === 'defaultUsername' \u0026\u0026 ocBody1.password === 'defaultPassword');\nconsole.log(ocBody2.username === 'defaultUsername' \u0026\u0026 ocBody2.password === 'defaultPassword');\nconsole.log(ocBody3.username === 'jeff' \u0026\u0026 ocBody3.password === 'defaultPassword');\nconsole.log(ocBody4.username === 'jeff' \u0026\u0026 ocBody4.password === 'password');\nconsole.log(ocBody5.user.username === 'jeff' \u0026\u0026 ocBody5.user.password === 'defaultPassword');\nconsole.log(\n  ocEvent5.body.user.username === 'jeff' \u0026\u0026\n    ocEvent5.body.user.password === 'defaultPassword' \u0026\u0026\n    ocEvent5.body.config.isAdmin === true\n);\nconsole.log(ocEvent6.body.user.username === 'jeff' \u0026\u0026 ocEvent6.body.user.password === 'password');\nconsole.log(ocEvent6NoPartials.body.user === {});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjtaylorchang%2Fjs-optchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjtaylorchang%2Fjs-optchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjtaylorchang%2Fjs-optchain/lists"}