{"id":17321668,"url":"https://github.com/beizhedenglong/js-lens","last_synced_at":"2025-04-14T15:54:34.243Z","repository":{"id":57283268,"uuid":"151924154","full_name":"beizhedenglong/js-lens","owner":"beizhedenglong","description":"Safely accessing and updating nested object with functional features.","archived":false,"fork":false,"pushed_at":"2019-05-03T05:01:28.000Z","size":1747,"stargazers_count":3,"open_issues_count":10,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T04:41:49.777Z","etag":null,"topics":["accessing","compose","immutable","nested-objects","ramda","set"],"latest_commit_sha":null,"homepage":"","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/beizhedenglong.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":"2018-10-07T09:12:34.000Z","updated_at":"2023-03-04T05:19:49.000Z","dependencies_parsed_at":"2022-09-17T14:02:13.521Z","dependency_job_id":null,"html_url":"https://github.com/beizhedenglong/js-lens","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beizhedenglong%2Fjs-lens","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beizhedenglong%2Fjs-lens/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beizhedenglong%2Fjs-lens/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beizhedenglong%2Fjs-lens/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beizhedenglong","download_url":"https://codeload.github.com/beizhedenglong/js-lens/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248581381,"owners_count":21128161,"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":["accessing","compose","immutable","nested-objects","ramda","set"],"created_at":"2024-10-15T13:38:49.481Z","updated_at":"2025-04-14T15:54:34.206Z","avatar_url":"https://github.com/beizhedenglong.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Js Lens\n\n[![Build Status](https://travis-ci.org/beizhedenglong/js-lens.svg?branch=master)](https://travis-ci.org/beizhedenglong/js-lens)\n[![Coverage Status](https://coveralls.io/repos/github/beizhedenglong/js-lens/badge.svg?branch=master)](https://coveralls.io/github/beizhedenglong/js-lens?branch=master)\n[![Greenkeeper badge](https://badges.greenkeeper.io/beizhedenglong/js-lens.svg)](https://greenkeeper.io/)\n[![Dev Dependencies](https://david-dm.org/beizhedenglong/js-lens/dev-status.svg)](https://david-dm.org/beizhedenglong/js-lens?type=dev)\n\nJs Lens is a tiny library that help you accessing and updating deep nested object safely with functional features(immutability and composition). \n\nAccessing and updating nested object directly in JavaScript maybe dangerous. Like this:\n```js\nlet nestedObj = {\n  user: {\n    name: 'Victor',\n    favoriteColors: [\"black\", \"white\", \"grey\"],\n    // contact info doesn't appear here\n    // contact: {\n    //   phone: 123,\n    //   email: \"123@example.com\"\n    // }\n  }\n}\n\n// if we want to get the user's phone number, we will get an TypeError: Cannot read property 'phone' of undefined\nconst phoneNumber = nestedObj.user.contact.phone\n\n// How about set the phone number? \nnestedObj.user.contact.phone = 123 // we also got an TypeError: Cannot set property 'phone' of undefined\n\n```\nWith Js Lens, we can get rid of this risk.\n\n## Installation\n`npm install js-lens --save`\n\n## Accessing And Updating\n```js\nimport { lensPath, view, set, over } from \"js-lens\"\n\nconst contactPhoneLens = lensPath([\"user\", \"contact\", \"phone\"])\nconst colorsLens = lensPath([\"user\", \"favoriteColors\"])\n\n// get phone number\nview(contactPhoneLens, nestedObj) // undefined\n\n// set phone number \nlet newNestedObj = set(contactPhoneLens, 123, nestedObj) // return a new object, leaving original object untouched.\nview(contactPhoneLens, newNestedObj) // 123\n\n// update value base on previous value by using over\nnewNestedObj = over(colorsLens, colors =\u003e [...colors, \"brown\"], nestedObj)\nview(colorsLens, newNestedObj) // [ 'black', 'white', 'grey', 'brown' ]\n\n```\n\n## Lens Can Compose\n\n```js\nimport  { lensPath, view, set, lensCompose } from  \"js-lens\"\n\nconst contactLens = lensPath([\"user\", \"contact\"])\nconst emailLens = lensPath([\"email\"])\nconst addressLens = lensPath([\"addressLens\"])\n\nconst contactAddressLens = lensCompose(contactLens, addressLens)\nconst contactEmailLens = lensCompose(contactLens, emailLens)\n\n// get address\nview(contactAddressLens, newNestedObj) // undefined\n\n// set address\nnewNestedObj = set(contactAddressLens, \"Mars\", newNestedObj)\nview(contactAddressLens, newNestedObj) // Mars\n\n// set email\nnewNestedObj = set(contactEmailLens, \"hello@example.com\", newNestedObj)\nview(contactEmailLens, newNestedObj) // hello@example.com\n  \n```\n\nTODO API DOC","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeizhedenglong%2Fjs-lens","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeizhedenglong%2Fjs-lens","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeizhedenglong%2Fjs-lens/lists"}