{"id":15834048,"url":"https://github.com/kaelzhang/node-object-access","last_synced_at":"2025-03-15T08:31:55.286Z","repository":{"id":65411492,"uuid":"45017043","full_name":"kaelzhang/node-object-access","owner":"kaelzhang","description":"Access(read and write) an object hierachically.","archived":false,"fork":false,"pushed_at":"2020-09-26T16:10:35.000Z","size":17,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-10T23:16:42.710Z","etag":null,"topics":["access","get","nodejs","object"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaelzhang.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":"2015-10-27T04:29:14.000Z","updated_at":"2018-08-12T04:47:38.000Z","dependencies_parsed_at":"2023-01-23T10:54:58.896Z","dependency_job_id":null,"html_url":"https://github.com/kaelzhang/node-object-access","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-object-access","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-object-access/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-object-access/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-object-access/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaelzhang","download_url":"https://codeload.github.com/kaelzhang/node-object-access/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243707282,"owners_count":20334613,"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","get","nodejs","object"],"created_at":"2024-10-05T14:00:42.047Z","updated_at":"2025-03-15T08:31:54.732Z","avatar_url":"https://github.com/kaelzhang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/kaelzhang/node-object-access.svg?branch=master)](https://travis-ci.org/kaelzhang/node-object-access)\n\u003c!-- optional npm version\n[![NPM version](https://badge.fury.io/js/object-access.svg)](http://badge.fury.io/js/object-access)\n--\u003e\n\u003c!-- optional npm downloads\n[![npm module downloads per month](http://img.shields.io/npm/dm/object-access.svg)](https://www.npmjs.org/package/object-access)\n--\u003e\n\u003c!-- optional dependency status\n[![Dependency Status](https://david-dm.org/kaelzhang/node-object-access.svg)](https://david-dm.org/kaelzhang/node-object-access)\n--\u003e\n\n# object-access\n\nAccess(read, write and delete) an object by hierarchical keys. And it can be used as an alternative for the null propagation operator\n\n## Synopsis\n\n```js\nconst access = require('object-access')\nconst obj = {\n  one: {\n    two: 2\n  }\n}\n```\n\nIf we use a null operator like many other languages and the [tc39 proposal](https://docs.google.com/presentation/d/11O_wIBBbZgE1bMVRJI8kGnmC6dWCBOwutbN9SWOK0fU/view#slide=id.g1c161255c9_0_55)\n\n```js\n// Use Null Propagation Operator\nobj.one?.two                  // 2\n\n// Use object-access\naccess(obj, 'one.two', 2))    // 2\n```\n\n## Usage\n\n```js\n// Get\naccess(obj, 'one.two')                    // 2\naccess(obj, ['one', 'two'])               // 2\naccess(obj, ['one', 'three'])             // undefined\naccess(obj, ['one', 'three', 'four'])     // undefined\naccess(obj, ['one', 'three', 'four'], 4)  // 4\n\n// Set\naccess.set(obj, 'one.two', 3)             // 3\n// then obj.one.two.three is 3\n\naccess.set(obj, ['one', 'two'], 4)        // 4\n// then obj.one.two.three is 4\n\n// If the subtle object is not found, it will create one\naccess.set(obj, ['three', 'four'], 6)     // 6\n// then obj.three.four is 6\n\n// If we try to set a property on a none-object value, it will thrown.\naccess.set(obj, 'one.two.tree', 3)        // Error thrown!\n\n// Force setting\naccess.set(obj, 'one.two.tree', 3, true)  // true\n// obj.one.two is {three: 3}\n\naccess.delete(obj, 'one.two.four')        // obj.one.two.four   -\u003e undefined\naccess.delete(obj, 'one.two.tree')        // obj.one.two        -\u003e {}\naccess.delete(obj, 'one.two')             // obj.one            -\u003e {}\n```\n\n### access(obj, key_list [, default_value])\n### access.get(obj, key_list [, default_value])\n\nGet value\n\n- obj `Object`\n- key_list `Array|string` see the example above\n- default_value `any=` if key_list not found, then returns the `default_value`, if `default_value` is not specified, `undefined` will be returned.\n\n### access.set(obj, key_list, value [, force])\n\nIf the property already exists and is not an object, `access.set()` will throw an error whose `code` is `SET_ON_NONE_OBJECT`.\n\nIf you want to force setting new values on none object value and override it, or to prevent any errors, use `force=true`.\n\nReturns `value`\n\n### access.delete(obj, key_list) : boolean\n\nRemoves a key by deleting it if exists.\n\nReturns `true` if succeeded otherwise `false`\n\n### ~~access.remove(obj, key_list)~~ DEPRECATED in `1.2.0`\n\n## Upgrade from `1.x` to `2.x`\n\nMethod  | `1.x` | `2.x`\n---- | ---- | ----\nset | no return value | has return value if there is no error\nset | never thrown | thrown if you try to set property on none object\nremove | no return value | has return value\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fnode-object-access","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaelzhang%2Fnode-object-access","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fnode-object-access/lists"}