{"id":13448629,"url":"https://github.com/armanozak/snq","last_synced_at":"2025-03-22T09:31:31.087Z","repository":{"id":47572744,"uuid":"182461273","full_name":"armanozak/snq","owner":"armanozak","description":"A utility function to avoid type errors when traversing over arrays and object properties.","archived":false,"fork":false,"pushed_at":"2023-03-02T21:39:09.000Z","size":1096,"stargazers_count":30,"open_issues_count":4,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T13:58:14.325Z","etag":null,"topics":["elvis-operator","exception-handling","null-coalescing-operator","null-conditional-operator","null-safety","optional-chaining","safe-navigation","type-error","typescript"],"latest_commit_sha":null,"homepage":"https://stackblitz.com/edit/snq","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/armanozak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-04-20T22:44:49.000Z","updated_at":"2024-09-06T12:48:49.000Z","dependencies_parsed_at":"2024-01-21T04:39:59.568Z","dependency_job_id":"fd0929e6-8d57-43f4-92e6-3e6be477d6f0","html_url":"https://github.com/armanozak/snq","commit_stats":{"total_commits":29,"total_committers":2,"mean_commits":14.5,"dds":"0.13793103448275867","last_synced_commit":"8726b29089ea8ad820478b395203d18266f86426"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armanozak%2Fsnq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armanozak%2Fsnq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armanozak%2Fsnq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armanozak%2Fsnq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/armanozak","download_url":"https://codeload.github.com/armanozak/snq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244937751,"owners_count":20535124,"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":["elvis-operator","exception-handling","null-coalescing-operator","null-conditional-operator","null-safety","optional-chaining","safe-navigation","type-error","typescript"],"created_at":"2024-07-31T05:01:51.059Z","updated_at":"2025-03-22T09:31:30.626Z","avatar_url":"https://github.com/armanozak.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# snq (Safe Navigation Query)\n\n[![Bundle Size](https://img.shields.io/bundlephobia/minzip/snq.svg)](https://bundlephobia.com/result?p=snq)\n[![MIT License](https://img.shields.io/github/license/armanozak/snq.svg)](./LICENSE)\n[![Follow the Author on Twitter](https://img.shields.io/twitter/follow/armanozak.svg?label=Follow)](https://twitter.com/armanozak)\n\n\u003e Now that [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) and [nullish coalescing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) operators are available, libraries like snq have become redundant. Please use them instead.\n\n**snq** is a utility function to safely navigate arrays and object properties without getting type errors. It is **not an original idea at all** and is actually adapted and only slightly different from [idx](https://github.com/facebookincubator/idx). The main differences are as follows:\n\n- snq returns `undefined` whenever a `TypeError` happens, regardless of the reason for the error and throws an error only if it is not a `TypeError`. idx returns `null`, if the cause of the error is a `null` value and throws an error if the error is not caused by an `undefined` or `null` value.\n- snq has an optional second parameter which works as **default value** to return instead of `undefined`.\n- idx requires the source object as a first parameter. snq does not.\n- idx has a Babel plugin for replacing idx instances with conventional traversing in order to improve performance. Although it is not benchmarked yet, due to lack of reason checks, it is safe to say that snq is faster than idx. Thus, a Babel plugin could prove insignificant for snq.\n- snq is written in TypeScript and, unlike idx, it does not support Flow types.\n\n## Installation\n\nRun the following code in your terminal:\n\n```shell\nyarn add snq\n```\n\nor if you are using npm:\n\n```shell\nnpm install --save snq\n```\n\n## Setup\n\n```typescript\nimport snq from 'snq';\n```\n\n## Usage\n\nConsider the following interfaces as `products` list:\n\n```typescript\ninterface Price {\n  amount: number;\n  currency: string;\n  symbol?: string;\n}\n\ninterface Product {\n  id: number;\n  name: string;\n  inStock: boolean;\n  price?: {\n    final: Price;\n    original?: Price;\n  };\n}\n```\n\nThis is how it would probably look like when you want to get original price symbol of first product:\n\n```typescript\nproducts.length \u0026\u0026\n  products[0].price \u0026\u0026\n  products[0].price.original \u0026\u0026\n  products[0].price.original.symbol;\n```\n\nOtherwise, you will get a type error. Using `snq`, it is safe to write the following:\n\n```typescript\nconst symbol = snq(() =\u003e products[0].price.original.symbol);\n\n// symbol is undefined if a type error happens, actual value if not\n```\n\nThere is an optional second argument which represents the default value to return when a type error happens.\n\n```typescript\nconst symbol = snq(() =\u003e products[0].price.original.symbol, '$');\n\n// symbol is \"$\" if a type error happens, actual value if not\n```\n\nThe type of the symbol returned will be inferred as string in both cases.\n\nCheck the [demo application](https://stackblitz.com/edit/snq) out.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmanozak%2Fsnq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farmanozak%2Fsnq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmanozak%2Fsnq/lists"}