{"id":13548811,"url":"https://github.com/jclem/steeltoe","last_synced_at":"2025-04-13T15:52:55.787Z","repository":{"id":5230779,"uuid":"6407428","full_name":"jclem/steeltoe","owner":"jclem","description":"Don't shoot yourself in the foot traversing objects.","archived":false,"fork":false,"pushed_at":"2014-02-25T09:09:54.000Z","size":207,"stargazers_count":94,"open_issues_count":2,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-10T17:31:12.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.org/package/steeltoe","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"TimothyDJones/awesome-laravel","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jclem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-26T17:08:48.000Z","updated_at":"2022-06-03T16:00:42.000Z","dependencies_parsed_at":"2022-09-19T02:00:51.170Z","dependency_job_id":null,"html_url":"https://github.com/jclem/steeltoe","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/jclem%2Fsteeltoe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsteeltoe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsteeltoe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsteeltoe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jclem","download_url":"https://codeload.github.com/jclem/steeltoe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741153,"owners_count":21154249,"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-08-01T12:01:14.754Z","updated_at":"2025-04-13T15:52:55.761Z","avatar_url":"https://github.com/jclem.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# [SteelToe](http://jclem.github.io/steeltoe/)\n\nDon't shoot yourself in the foot while traversing JavaScript objects.\n\n## Get It\n\n```sh\n$ npm install steeltoe --save\n```\n\n## Usage\n\nSteelToe is a tiny JavaScript function that makes it safe to traipse about\nobjects without worrying about whether keys may or may not exist, and whether\nit's safe to try and look inside of them. It also provides basic\n[autovivification](http://en.wikipedia.org/wiki/Autovivification) of objects\nthrough the `set` function.\n\n### Getting Values\n\n#### Method #1\n```javascript\nvar object = { info: { name: { first: 'Jonathan', last: 'Clem' } } }\n\nsteelToe(object)('info')('name')('last')();           // 'Clem'\nsteelToe(object)('info')('features')('hairColor')();  // undefined\n```\n\n#### Method #2\n```javascript\nvar object = { info: { name: { first: 'Jonathan', last: 'Clem' } } }\n\nsteelToe(object).get('info.name.last');          // 'Clem'\nsteelToe(object).get('info.features.hairColor'); // undefined\n```\n\n### Setting Values\n\n```javascript\nvar jonathan = { info: { name: { first: 'Jonathan', last: 'Clem' } } },\n\nsteelToe(jonathan).set('info.name.middle', 'Tyler');\nsteelToe(jonathan).set('info.favorites.movie', 'Harold \u0026 Maude');\n\njonathan.info.name.middle; // Tyler\njonathan.info.favorites.movie; // Harold \u0026 Maude\n````\n\n## Details\n\nLet's say you've got some deeply nested data in JavaScript objects that you've\njust parsed from a JSON API response. For each result, you need to do something\nif there's some sort of data present:\n\n```javascript\nvar fatherFirstNames = [];\n\nfor (var i = 0; i \u003c families.length; i ++) {\n  var first = families[i].father.info.name.first;\n\n  if (first) {\n    fatherFirstNames.push(first);\n  }\n}\n\n// TypeError: 'undefined' is not an object (evaluating 'family.father.info.name.first')\n```\n\nWhoops! You shot yourself in the foot. You got a `TypeError` because you had no\nguarantee that the family had a father, or that the father had info present, or\nthat his name was returned! You fix it by writing this monstrosity:\n\n```javascript\nvar farherFirstNames = [];\n\nfor (var i = 0; i \u003c families.length; i++) {\n  var father = families[i].father;\n\n  if (father \u0026\u0026 father.info \u0026\u0026 father.info.name \u0026\u0026 father.info.name.first) {\n    fatherFirstNames.push(father.info.name.first);\n  }\n}\n```\n\nOr, you could use SteelToe and write this:\n\n```javascript\nvar fatherFirstNames = [];\n\nfor (var i = 0; i \u003c families.length; i++) {\n  var name = steelToe(families[i]).get('father.info.name.first');\n\n  if (name) {\n    fatherFirstNames.push(name);\n  }\n}\n\nfatherFirstNames; // [\"Hank\", \"Dale\", \"Bill\"]\n```\n\n## The End\n\nSteelToe was made when a coworker of mine said that he wished someone would\nwrite a JavaScript library that would allow him to traverse objects without\nshooting himself in the foot, and that the library was called SteelToe.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fsteeltoe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjclem%2Fsteeltoe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fsteeltoe/lists"}