{"id":17148043,"url":"https://github.com/dpnishant/tweezr","last_synced_at":"2025-04-13T11:40:44.784Z","repository":{"id":57382656,"uuid":"89872092","full_name":"dpnishant/tweezr","owner":"dpnishant","description":"a javascript library to reverse lookup values within a javascript object \u0026 generate the dot-notation query selectors in context to the specified object. You may also use it to \"walk\" through the structure one-step-at-a-time.","archived":false,"fork":false,"pushed_at":"2018-02-14T19:14:07.000Z","size":224,"stargazers_count":5,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T02:43:42.101Z","etag":null,"topics":["generate","javascript","json","library","lookup","queryselector","reverse-search"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tweezr","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/dpnishant.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":"2017-04-30T19:27:03.000Z","updated_at":"2024-06-04T21:03:15.000Z","dependencies_parsed_at":"2022-09-01T04:01:59.251Z","dependency_job_id":null,"html_url":"https://github.com/dpnishant/tweezr","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/dpnishant%2Ftweezr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpnishant%2Ftweezr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpnishant%2Ftweezr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpnishant%2Ftweezr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dpnishant","download_url":"https://codeload.github.com/dpnishant/tweezr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248709330,"owners_count":21149169,"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":["generate","javascript","json","library","lookup","queryselector","reverse-search"],"created_at":"2024-10-14T21:26:59.926Z","updated_at":"2025-04-13T11:40:44.754Z","avatar_url":"https://github.com/dpnishant.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM](https://nodei.co/npm/tweezr.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/tweezr/)\n\n# **tweezr**\n\na module to reverse search values within a JSON structure or a javascript object \u0026 generate the dot-notation query selectors in context of the specified object. You may also use it to \\\"walk\\\" through the structure one step at a time to read/write values. Think of it as an XPath generator for a matched keyword in a JS object (or a deserialized JSON).\n\n|![](https://raw.githubusercontent.com/dpnishant/tweezr/master/res/tweezr_intro.png)|\n|:-:|\n\n## Install\n\n`npm install tweezr`\n\n## Quickstart\n\n### Example JSON\n```javascript\n$ cat auto.json\n \n{\n  \"list\": \"automobiles\",\n  \"cars\": [\n    {\n      \"make\": \"bmw\",\n      \"model\": \"Q3\",\n      \"year\": 2012\n    },\n    {\n      \"make\": \"honda\",\n      \"model\": \"city\",\n      \"year\": [\n        2001,\n        2004,\n        2009\n      ]\n    },\n    {\n      \"make\": \"audi\",\n      \"model\": \"a4\",\n      \"year\": [\n        2013,\n        2014,\n        2015,\n        2019,\n        2021\n      ]\n    }\n  ],\n  \"bikes\": [\n    {\n      \"make\": \"kawasaki\",\n      \"model\": \"ninja300\",\n      \"year\": 2013\n    }\n  ],\n  \"dealers\": \"\"\n}\n```\n\n\n\n### Example Usage\n\n```javascript\n$ cat runner.js\n\nvar fs = require('fs');\nvar tweezr = require('tweezr').init({debug: false});\n \nfs.readFile('auto.json', 'utf8', function(err, data) {\n  var myObj = JSON.parse(data);\n  var keyword = 2015;\n  var result = tweezr.findAll(keyword, myObj, 'myObj');\n  \n  console.log('1. path of 1st node found: ' + result[0].path);\n  console.log('2. path of parent node: ' + result[0].parent().path);\n  console.log('2.1. key of parent node: ' + result[0].parent().key());\n  console.log('3. parent node serialized: ' + JSON.stringify(result[0].parent().val()));\n  console.log('4. path of previous sibling: ' + result[0].prev().path);\n  console.log('5. raw value of previous sibling: ' + result[0].prev().val());\n  console.log('6. path of previous to previous sibling: ' + result[0].prev(2).path);\n  console.log('7. raw value of previous to previous sibling: ' + result[0].prev(2).val());\n  console.log('8. path of next sibling: ' + result[0].next().path);\n  console.log('8.1. key of next sibling: ' + result[0].next().key());\n  console.log('9. raw value of next sibling: ' + result[0].next().val());\n  console.log('10. path of next to next sibling: ' + result[0].next(2).path);\n  console.log('11. raw value of next to next sibling: ' + result[0].next(2).val());\n  console.log('12. addAfter: ' + result[0].addAfter(1111).path);\n  console.log('13. add before next sibling: ' + result[0].next().addBefore(2222).path);\n  console.log('14. updated object serialized: ' + JSON.stringify(myObj));\n  console.log('15. replace a value: ' + result[0].replace([1,2,3]).val());\n  console.log('16. updated object: ' + JSON.stringify(myObj));\n});\n```\n\n### Output\n```shell\n$ node runner.js\n\n#console.log('1. path of 1st node found: ' + result[0].path);\n1. path of 1st node found: myObj['cars'][2]['year'][2]\n\n#console.log('2. path of parent node: ' + result[0].parent().path);\n2. path of parent node: myObj['cars'][2]['year']\n\n#console.log('2.1. key of parent node: ' + result[0].parent().key());\n2.1. key of parent node: year\n\n#console.log('3. parent node serialized: ' + JSON.stringify(result[0].parent().val()));\n3. parent node serialized: [2013,2014,2015,2019,2021]\n\n#console.log('4. path of previous sibling: ' + result[0].prev().path);\n4. path of previous sibling: myObj['cars'][2]['year'][2]\n\n#console.log('5. raw value of previous sibling: ' + result[0].prev().val());\n5. raw value of previous sibling: 2015\n\n#console.log('6. path of previous to previous sibling: ' + result[0].prev(2).path);\n6. path of previous to previous sibling: myObj['cars'][2]['year'][2]\n\n#console.log('7. raw value of previous to previous sibling: ' + result[0].prev(2).val());\n7. raw value of previous to previous sibling: 2015\n\n#console.log('8. path of next sibling: ' + result[0].next().path);\n8. path of next sibling: myObj['cars'][2]['year'][3]\n\n#console.log('8.1. key of next sibling: ' + result[0].next().key());\n8.1. key of next sibling: 3\n\n#console.log('9. raw value of next sibling: ' + result[0].next().val());\n9. raw value of next sibling: 2019\n\n#console.log('10. path of next to next sibling: ' + result[0].next(2).path);\n10. path of next to next sibling: undefined\n\n#console.log('11. raw value of next to next sibling: ' + result[0].next(2).val());\n11. raw value of next to next sibling: undefined\n\n#console.log('12. addAfter: ' + result[0].addAfter(1111).path);\n12. addAfter: myObj['cars'][2]['year'][3]\n\n#console.log('13. add before next sibling: ' + result[0].next().addBefore(2222).path);\n13. add before next sibling: myObj['cars'][2]['year'][2]\n\n#console.log('14. updated object serialized: ' + JSON.stringify(myObj));\n14. updated object serialized: {\"list\":\"automobiles\",\"cars\":[{\"make\":\"bmw\",\"model\":\"Q3\",\"year\":2012},{\"make\":\"honda\",\"model\":\"city\",\"year\":[2001,2004,2009]},{\"make\":\"audi\",\"model\":\"a4\",\"year\":[2013,2014,2222,2015,1111,2019,2021]}],\"bikes\":[{\"make\":\"kawasaki\",\"model\":\"ninja300\",\"year\":2013}],\"dealers\":\"\"}\n\n#console.log('15. replace a value: ' + result[0].replace([1,2,3]).val());\n15. replace a value: 1,2,3\n\n#console.log('16. updated object: ' + JSON.stringify(myObj));\n16. updated object: {\"list\":\"automobiles\",\"cars\":[{\"make\":\"bmw\",\"model\":\"Q3\",\"year\":2012},{\"make\":\"honda\",\"model\":\"city\",\"year\":[2001,2004,2009]},{\"make\":\"audi\",\"model\":\"a4\",\"year\":[2013,2014,[1,2,3],2015,1111,2019,2021]}],\"bikes\":[{\"make\":\"kawasaki\",\"model\":\"ninja300\",\"year\":2013}],\"dealers\":\"\"}\n```\n\n# Documentation\n\n## .init( _{debug: boolean}_ )\n\n## .findAll(varKeyword, objContextObject, strContextObjectLiteral)\n**Returns:** An array of selector objects    \n**Arguments**:    \n    - **varKeyword**: The item to be searched, sensitive of data-type and value    \n    - **objContextObject**: The identifier of the _haystack_ object (i.e. the object to search in)    \n    - **strContextObjectLiteral**: The literal value of the _haystack_ object identifier     \n\n## .path\n**Type:** Attribute    \n**Value:** the dot-notation selector query in context to objContextObject    \n\n## .obj\n**Type:** Attribute    \n**Value:** unserialized value of objContextObject    \n\n## .key()\n**Type:** Getter     \n**Returns:** the key of the key/value pair of the current selector\n\n## .val()\n**Type:** Getter    \n**Returns:** the value of the key/value of the current selector    \n\n## .parent(*[n]*)\n**Type:** Getter     \n**Returns:** the Nth parent node of the current selector. **Default** is 1     \n\n## .prev(*[n]*)\n**Type:** Getter    \n**Returns:** the previous Nth sibling from the current selector. **Default** is 1     \n\n## .next(*[n]*)\n**Type:** Getter    \n**Returns:** the next Nth sibling from the current selector. **Default** is 1    \n\n## .addAfter(varToInsert)\n**Type:** Setter    \n**Sets:** Sets varToInsert as the raw value of the next node of the current selector      \n**Returns:** the selector object of the newly added node    \n\n## .addBefore(varToInsert)\n**Type:** Setter    \n**Sets:** Sets varToInsert as the raw value of the previous node of the current selector    \n**Returns:** the selector object of the newly added node    \n\n## .replace(varToReplace)\n**Type:** Setter    \n**Sets:** Sets varToInsert as the raw value of the currently selected node    \n**Returns:** the selector object of the same node        \n\n\n\n# Acknowledgments/Credits\n* [Piyush Pattanayak](https://www.linkedin.com/in/piyush-pattanayak-0341a59/) - Thank you for helping with method chaining \u0026 several other valuable brainstorming sessions.\n* [Somasish Sahoo](https://www.linkedin.com/in/somasish/) -  Thank you for the numerous hours of valuable brainstorming sessions.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdpnishant%2Ftweezr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdpnishant%2Ftweezr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdpnishant%2Ftweezr/lists"}