{"id":21961832,"url":"https://github.com/trapcodeio/proxy-undefined","last_synced_at":"2026-05-04T23:38:20.278Z","repository":{"id":57331886,"uuid":"191954403","full_name":"trapcodeio/proxy-undefined","owner":"trapcodeio","description":"Proxify javascript values to return a default value if undefined.","archived":false,"fork":false,"pushed_at":"2019-06-15T21:49:46.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-11T01:47:25.148Z","etag":null,"topics":[],"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/trapcodeio.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":"2019-06-14T14:12:34.000Z","updated_at":"2023-01-14T13:05:42.000Z","dependencies_parsed_at":"2022-09-21T03:25:54.704Z","dependency_job_id":null,"html_url":"https://github.com/trapcodeio/proxy-undefined","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/trapcodeio%2Fproxy-undefined","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trapcodeio%2Fproxy-undefined/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trapcodeio%2Fproxy-undefined/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trapcodeio%2Fproxy-undefined/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trapcodeio","download_url":"https://codeload.github.com/trapcodeio/proxy-undefined/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244219727,"owners_count":20418027,"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":[],"created_at":"2024-11-29T10:18:32.361Z","updated_at":"2026-05-04T23:38:20.238Z","avatar_url":"https://github.com/trapcodeio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## ProxyUndefined\n\nReturn default value for object properties that does not exists i.e is **undefined**.\n\n### Install\n```console\nnpm install proxy-undefined\n\nyarn add proxy-undefined\n```\n\n### Usage\n```javascript\nconst {optional, optionalFn} = require(\"proxy-undefined\");\n// Or\nimport {optional, optionalFn} from \"proxy-undefined\";\n```\n\n### Why ProxyUndefined\nWhen working with api's or objects, in some cases you may not know if a value exists in an object\nand it will result to doing something like\n\n```javascript\nlet requiredValue = SomeObject.name;\n\nif(!requiredValue) { requiredValue = \"Guest\" }\n\n// Or\n\nif(!SomeObject.hasOwnProperty('name')) { requiredValue = \"Guest\" }\n```\n\nWith **ProxyUndefined** using [Javascript Proxy Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy),\nall undefined paths will be replaced with the value you set as default.\n\n```javascript\nconst {optional, optionalFn} = require('proxy-undefined');\n\nlet requiredValue = optional(SomeObject, 'default value').name;\n```\n\n### Example\n```javascript\nconst {optional} = require('proxy-undefined');\n\nconst data = {\n    message: \"Hello World\",\n\n    foo: () =\u003e {\n        console.log('Foo function was called');\n    },\n\n    bar: () =\u003e {\n        console.log('Bar function was called');\n    }\n};\n\nconsole.log(data.message);;\n// =\u003e Hello World\n\ndata.foo();\n// =\u003e Foo function was called\n\n//////////////////////////\n// Using data.name (undefined)\nconsole.log(data.name);\n// =\u003e undefined\n\nconsole.log(optional(data).name);\n// =\u003e undefined\n\nconsole.log(optional(data, 'John Doe').name);\n// =\u003e John Doe\n\n\n//////////////////////////\n// Using  data.message (defined)\nconsole.log(optional(data).message);\n// =\u003e Hello World\n\nconsole.log(optional(data, 'John Doe').message);\n// =\u003e Hello World\n```\n\n### Function Example\nIn Javascript if you call `data.getFullName()` and `getFullName` does not exists on the object we get an error.\n\nwith `optionalFn` you can set a default function if the one being called does not exists.\n```javascript\n// Using same data above.\ndata.bar();\n// =\u003e Bar function was called\n\ndata.getFullName();\n// =\u003e THIS WILL CAUSE AN ERROR\n// Error: data.getFullName is not a function\n\n// Using `optionalFn`\noptionalFn(data).getFullName();\n// =\u003e undefined\n// No Fatal Errors.\n\noptionalFn(data, () =\u003e console.log('John P. Doe')).getFullName();\n// =\u003e John P. Doe\n// Default function ran instead.\n\nconsole.log(optionalFn(data, 'john@doe.com').getEmail());\n// =\u003e john@doe.com\n// If default is not a function, it is returned directly.\n```\n\n#### Fn: exists/optional\nChecks if path exists in object else returns `[default=undefined]`\n```javascript\nconst {exist} = require(\"proxy-undefined\");\n\n// is same as below, just an alias.\n\n\nconst {optional} = require(\"proxy-undefined\");\n```\n\n#### Fn: fnExists/optionalFn\nChecks if function exists else returns `[default=() =\u003e undefined]`\n```javascript\nconst {fnExist} = require(\"proxy-undefined\");\n\n// is same as below, just an alias.\n\nconst {optionalFn} = require(\"proxy-undefined\");\n```\n\nHope you find this Useful. :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrapcodeio%2Fproxy-undefined","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrapcodeio%2Fproxy-undefined","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrapcodeio%2Fproxy-undefined/lists"}