{"id":17651328,"url":"https://github.com/nash403/get-safe","last_synced_at":"2025-03-30T09:12:57.621Z","repository":{"id":57250413,"uuid":"68194427","full_name":"nash403/get-safe","owner":"nash403","description":"Safe access to nested properties in JS objects without getting a TypeError but undefined instead.","archived":false,"fork":false,"pushed_at":"2017-11-18T12:11:09.000Z","size":43,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-06T01:05:49.016Z","etag":null,"topics":["access","js-objects","nested-properties","typeerror"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nash403.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-14T09:48:57.000Z","updated_at":"2017-11-18T12:03:51.000Z","dependencies_parsed_at":"2022-08-24T16:52:00.952Z","dependency_job_id":null,"html_url":"https://github.com/nash403/get-safe","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/nash403%2Fget-safe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nash403%2Fget-safe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nash403%2Fget-safe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nash403%2Fget-safe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nash403","download_url":"https://codeload.github.com/nash403/get-safe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246296615,"owners_count":20754635,"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":["access","js-objects","nested-properties","typeerror"],"created_at":"2024-10-23T11:41:48.894Z","updated_at":"2025-03-30T09:12:57.599Z","avatar_url":"https://github.com/nash403.png","language":"JavaScript","readme":"# get-safe\r\n\r\n\u003e Safe access to deeply nested properties or functions in JS objects without getting a TypeError but undefined instead.\r\n\r\n\r\nYou can even call a nested function in objects if the last nested key ends with `()`. You can pass arguments by adding them as last parameters of the get-safe function call.\r\n\r\n#### Install:\r\n\r\nUsing npm\r\n```\r\nnpm install get-safe --save\r\n```\r\n\r\nUsing yarn:\r\n```bash\r\nyarn add navscroll\r\n```\r\n\r\nDirectly include it in html:\r\n```html\r\n\u003c!-- Browsers with ES module support load this file. --\u003e\r\n\u003cscript type=\"module\" src=\"node_modules/get-safe/browser-version/get-safe.js\"\u003e\u003c/script\u003e\r\n\r\n\u003c!-- Older browsers load this file (and module-supporting --\u003e\r\n\u003c!-- browsers know when *not* to load this file). --\u003e\r\n\u003cscript nomodule src=\"node_modules/get-safe/browser-version/get-safe-legacy.js\"\u003e\u003c/script\u003e\r\n```\r\n\r\n\u003cp class=\"warning\" style=\"background: rgba(0,255,0,.05);border-radius: 3px;padding: 1.5em;\"\u003e\r\nWarning! The only gotcha here is Safari 10 doesn’t support the nomodule attribute, but you can solve this by \u003ca href=\"https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc\"\u003einlining a JavaScript snippet\u003c/a\u003e in your HTML prior to using any \u003ccode\u003e\u0026#x3C;script nomodule\u0026#x3E;\u003c/code\u003e tags. (Note: this has been fixed in Safari 11).\r\n\u003c/p\u003e\r\n\r\n\u003cp class=\"tip\" style=\"background-color: #DCF2FD;color: #618ca0;padding: 0.75em 1em;\"\u003e\r\nThe browser version adds the `getSafe` function to the 'window' object that you can use !\r\n\u003c/p\u003e\r\n\r\n#### How to use\r\n ```JavaScript\r\n\r\nconst _ = require ('get-safe');\r\nconst myObj = {\r\n  foo: {\r\n    bar: {\r\n      baz: ['winter','is','coming'],\r\n      fifo (arg1, arg2) {\r\n        console.log(\"I'am a function, arguments are:\",...arguments);\r\n        return 42;\r\n      }\r\n    }\r\n  }\r\n};\r\n\r\n// Tests\r\nconsole.log(_('foo.bar.baz.2',myObj)); // logs 'coming'\r\nconsole.log(_('foo.bar.fifo()',myObj,'arg1','arg2')); // calls the nested function 'fifo' and logs its result\r\nconsole.log(_('foo.inexistant.property.baz',myObj)); // logs 'undefined'\r\n```\r\n\r\n_NOTE:_ If you are **NOT** making a function call and just accessing a property, you can pass a default value as the third argument, this will be returned instead of `undefined` if the nested property doesn't exsit.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnash403%2Fget-safe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnash403%2Fget-safe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnash403%2Fget-safe/lists"}