{"id":20471337,"url":"https://github.com/statewalker/statewalker-getset","last_synced_at":"2026-07-14T07:32:52.436Z","repository":{"id":57862196,"uuid":"528629833","full_name":"statewalker/statewalker-getset","owner":"statewalker","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-18T17:41:54.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-26T04:33:25.547Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/statewalker.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-24T23:44:54.000Z","updated_at":"2022-08-24T23:47:02.000Z","dependencies_parsed_at":"2024-09-18T16:34:43.581Z","dependency_job_id":"b1a43e9b-d0b7-404c-8966-877b1638a85c","html_url":"https://github.com/statewalker/statewalker-getset","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"7805a2350ea9f09215d84b8c921484da9a337fda"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/statewalker/statewalker-getset","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-getset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-getset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-getset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-getset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/statewalker","download_url":"https://codeload.github.com/statewalker/statewalker-getset/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-getset/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35451878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-14T02:00:06.603Z","response_time":114,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-15T14:15:49.214Z","updated_at":"2026-07-14T07:32:52.416Z","avatar_url":"https://github.com/statewalker.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# @statewalker/getset: tools to read/update JSON objects\n\nThis library contains methods to read/update/clone JSON objects.\n\nFor working examples see this Observable Notebook: https://observablehq.com/@kotelnikov/statewalker-getset.\n\n- `buildCloneSetter(path)` \n- `buildGetter(path)`\n- `buildSetter(path)`\n- `_compile(code)`\n- `escape(pathSegment)`\n- `get(obj, path)`\n- `newCloneSetter(path)`\n- `newGetter(path)`\n- `newSetter(path)`\n- `set(obj, path, value)`\n- `toPath(path)`\n\n\n## `buildCloneSetter(path)`\n\nGenerates code for a JS method updating JSON objects with new field values.\nThis method used internally by the `newCloneSetter` method.\n\nExample: \n```javascript\nimport { buildCloneSetter } from '@statewalker/getset';\n\nconst code = buildCloneSetter('userInfo.firstName');\nconsole.log(code);\n// Result:\n/* \nfunction(obj, value){ \"use strict\";\nvar newObj = Object.assign({}, obj || {});\nvar o = newObj;\no = o[\"userInfo\"] = (typeof o[\"userInfo\"] === \"object\")\n  ? Object.assign({}, o[\"userInfo\"])\n  : {};\nif (value === undefined) {\n  delete o[\"firstName\"]; \n} else {\n  o[\"firstName\"] = value; \n}\nreturn newObj;\n}\n*/\n```\n\n## `buildGetter(path)`\n\nGenerates code for a JS method returning JSON objects fields corresponding to the specified path.\nThis method used internally by the `newGetter` method.\n\nExample: \n```javascript\nimport { buildGetter } from '@statewalker/getset';\n\nconst code = buildGetter('userInfo.firstName');\nconsole.log(code);\n// Result:\n/* \nfunction (obj){ \"use strict\";\ntry{ return obj[\"userInfo\"][\"firstName\"]; } catch (err) {}\n}\n*/\n```\n\n## `buildSetter(path)`\n\nGenerates code for a JS method updating JSON objects field values.\nThis method used internally by the `newSetter` method.\n\nExample: \n```javascript\nimport { buildSetter } from '@statewalker/getset';\n\nconst code = buildSetter('userInfo.firstName');\nconsole.log(code);\n// Result:\n/* \nfunction(obj, value){ \"use strict\"; \nobj = obj || {};\nvar o = obj;\no = o[\"userInfo\"] = (typeof o[\"userInfo\"] !== \"object\") ? {} : o[\"userInfo\"]\nif (value === undefined) {\n  delete o[\"firstName\"]; \n} else {\n  o[\"firstName\"] = value; \n}\nreturn obj;\n}\n*/\n```\n\n\n## `_compile(code)`\n\nAn internal method transforming the given function code to a compiled run-time method.\nBasically it performs the following operation:\n\n```javascript\n const f = (new Function([], `return ${code}`))();\n ...\n```\n\n## `escape(pathSegment)`\n\nEscapes individual path segment. The returned value can be used to generate JS code accessing object field\nwith the escaped name.\n\n## `get(obj, path)`\n\nReturns the object value corresponding to the specified path. This method splits and validates each time the path to the field value and iteratively walks over the tree to access to the field value.\n\nSee the `newGetter(path)` method for more efficient/fast access to object fields.\n\nExample: \n```javascript\nimport { get } from '@statewalker/getset';\n\nconst obj = {\n    userInfo : {\n        firstName: 'John',\n        lastName : 'Smith'\n    }\n}\nconst firstName = get(obj, 'userInfo.firstName');\nconsole.log(firstName);\n/*\n\"John\"\n*/\n```\n\n## `newCloneSetter(path)`\n\nCreates and returns a method updating object hierarchies if the field value was changed.\nInternally this method generates and compiles JS code updating objects fields and returning a copy of the original instance.\n\nSee the `newSetter` method to update an object without creation of clones.\n\nExample: \n```javascript\nimport { newCloneSetter } from '@statewalker/getset';\n\n// Generate a function updating user's last name and returning a copy of the original object:\nconst updateLastName = newCloneSetter('userInfo.lastName');\n\nconst obj = {\n    userInfo : {\n        firstName: 'John',\n        lastName : 'Smith'\n    }\n}\n// Now we can use/reuse the generated method to update objects:\nconst newObj = updateLastName(obj, \"SMITH\");\n\n// The returned object is a clone of the original \"obj\" instance.\nconsole.log(newObj === obj);\n// false\n\nconsole.log(JSON.stringify(newObj, null, 2));\n/*\n{\n  \"userInfo\" : {\n    \"firstName\": \"John\",\n    \"lastName\" : \"SMITH\"\n  }\n}\n*/\n```\n\n\n## `newGetter(path)`\n\nThis method returns a new function providing field value of the given object.\nInternally this method generates and compiles JS code providing direct access to nested object fields.\n\nExample: \n```javascript\nimport { newGetter } from '@statewalker/getset';\n\n// Generate a function providing direct access to the user's first name:\nconst getter = newGetter('userInfo.firstName');\n\nconst obj = {\n    userInfo : {\n        firstName: 'John',\n        lastName : 'Smith'\n    }\n}\n// Now we can use/reuse the generated method to retrieve nested object fields:\nconst firstName = getter(obj);\nconsole.log(firstName);\n/*\nJohn\n*/\n```\n\n## `newSetter(path)`\n\nCreates and returns a method updating objects if the field value was changed.\nInternally this method generates and compiles JS code updating objects fields *without changing the object hierarchy*.\n\nSee the `newCloneSetter` method to create object clones on updates.\n\nExample: \n```javascript\nimport { newCloneSetter } from '@statewalker/getset';\n\n// Generates a function updating user's last name without changing the hierarchy of objects:\nconst updateLastName = newSetter('userInfo.lastName');\n\nconst obj = {\n    userInfo : {\n        firstName: 'John',\n        lastName : 'Smith'\n    }\n}\n// Now we can use/reuse the generated method to update objects:\nconst sameObject = updateLastName(obj, \"SMITH\");\n\n// The returned object is the same as the original \"obj\" instance:\nconsole.log(sameObject === obj);\n// true\n\nconsole.log(JSON.stringify(obj, null, 2));\n/*\n{\n  \"userInfo\" : {\n    \"firstName\": \"John\",\n    \"lastName\" : \"SMITH\"\n  }\n}\n*/\n```\n\n## `set(obj, path, value)`\n\nSets a new field value for the given object. This method recursively updates the object by adding missing internal instances and sets the field value corresponding to the specified path.\n\nThis is not the most efficient method to update object fields. See the `newSetter` and `newCloneSetter` methods.\n\nExample: \n```javascript\nimport { set } from '@statewalker/getset';\n\nconst obj = {\n    userInfo : {\n        firstName: 'John',\n        lastName : 'Smith'\n    }\n}\nconst newObj = set(obj, 'userInfo.lastName', 'SMITH');\n// The returned object is the same as the original \"obj\" instance:\nconsole.log(newObj === obj);\n// true\n\nconsole.log(JSON.stringify(obj, null, 2));\n/*\n{\n  \"userInfo\" : {\n    \"firstName\": \"John\",\n    \"lastName\" : \"SMITH\"\n  }\n}\n*/\n```\n\n\n## `toPath(path)`\n\nAn internal utility method splitting/normalizing the given string path and transforming it to an array with individual path segments.\n\n\nExample: \n```javascript\nimport { toPath } from '@statewalker/getset';\n\nconst pathSegments = toPath(\"userInfo.address.city\")\nconsole.log(pathSegments);\n// [\"userInfo\", \"address\", \"city\"]\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-getset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatewalker%2Fstatewalker-getset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-getset/lists"}