{"id":44631653,"url":"https://github.com/bluet/obj-filter","last_synced_at":"2026-02-14T16:35:11.721Z","repository":{"id":20349653,"uuid":"89725871","full_name":"bluet/obj-filter","owner":"bluet","description":"JavaScript Object Filter. Deep filtering key/content recursively. Support wildcard, nested, and filter function in template.","archived":false,"fork":false,"pushed_at":"2024-12-02T04:17:28.000Z","size":808,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-13T09:51:53.666Z","etag":null,"topics":["browser","filter","fossa-status","function","hacktoberfest","javascript","nested","nodejs","object","template","wildcard"],"latest_commit_sha":null,"homepage":"","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/bluet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"bluet","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2017-04-28T16:44:50.000Z","updated_at":"2025-08-11T16:51:11.000Z","dependencies_parsed_at":"2024-06-19T17:35:33.721Z","dependency_job_id":"f7575f00-c5be-4a3e-943d-59dc4adb5ef4","html_url":"https://github.com/bluet/obj-filter","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/bluet/obj-filter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluet%2Fobj-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluet%2Fobj-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluet%2Fobj-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluet%2Fobj-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bluet","download_url":"https://codeload.github.com/bluet/obj-filter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluet%2Fobj-filter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29449374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T15:52:44.973Z","status":"ssl_error","status_checked_at":"2026-02-14T15:52:11.208Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["browser","filter","fossa-status","function","hacktoberfest","javascript","nested","nodejs","object","template","wildcard"],"created_at":"2026-02-14T16:35:10.909Z","updated_at":"2026-02-14T16:35:11.704Z","avatar_url":"https://github.com/bluet.png","language":"JavaScript","funding_links":["https://ko-fi.com/bluet"],"categories":[],"sub_categories":[],"readme":"[![Build](https://github.com/bluet/obj-filter/actions/workflows/sonarcloud.yml/badge.svg)](https://github.com/bluet/obj-filter/actions/workflows/sonarcloud.yml)\n[![CodeQL](https://github.com/bluet/obj-filter/actions/workflows/codeql.yml/badge.svg)](https://github.com/bluet/obj-filter/actions/workflows/codeql.yml)\n[![Node CI](https://github.com/bluet/obj-filter/actions/workflows/nodejs.yml/badge.svg)](https://github.com/bluet/obj-filter/actions/workflows/nodejs.yml)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fbluet%2Fobj-filter.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fobj-filter?ref=badge_shield)\n\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obj-filter\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=obj-filter)\n[![Open Source Helpers](https://www.codetriage.com/bluet/obj-filter/badges/users.svg)](https://www.codetriage.com/bluet/obj-filter)  \n[![npm version](https://img.shields.io/npm/v/obj-filter.svg)](https://www.npmjs.org/package/obj-filter)\n[![install size](https://packagephobia.now.sh/badge?p=obj-filter)](https://packagephobia.now.sh/result?p=obj-filter)\n[![npm downloads](https://img.shields.io/npm/dm/obj-filter.svg)](http://npm-stat.com/charts.html?package=obj-filter)\n[![GitHub license](https://img.shields.io/github/license/BlueT/obj-filter.svg)](https://github.com/BlueT/obj-filter/blob/master/LICENSE)\n\n# obj-filter - JavaScript Object Filter / Merger.\n\nJavaScript Object Filter. **Deep** filtering key/content *recursively*.  \nSupport **type checking**, **wildcard**, **nested**, and **filter function** in *template*.\n\n## INSTALL\n\n`npm i obj-filter`\n\nOr find help from:\n- https://www.npmjs.com/package/obj-filter\n- https://github.com/BlueT/obj-filter\n\n## SYNOPSIS\n\n~~~~ js\n\"use strict\";\n\nconst {filter, merge, exist, ArrayIter} = require('obj-filter');\n\nconst template = {\n    \"runtime\": {\n        \"connectionState\": undefined,   // excluded\n        \"powerState\": function (args) {return \"HELLO WORLD \" + args},   // pass into the checker function\n        \"bootTime\": \"my boot time\",     // included\n        \"paused\": false,\n        \"snapshotInBackground\": 1111111,\n        \"numbers\": ArrayIter(filter, Number),   // value of \"numbers\" must be an array, and will check all elements in the array\n    },\n    \"running\": Boolean\n};\n\nlet clean_data = filter( template, fetchData() );\nlet updated_data = filter.merge( clean_data, newUpdates() );\nlet clean_full_data = filter.exist( template, fetchData() );\n~~~~\n\n## Template Object\nAccording to the **Template Object structure**, `obj-filter` supports the following types of value with different behaviors to build the result object.\n\n### undefined\nIf the *value* of the key is `undefined`, the key will be **filtered** (skipped) and will not included in result object.\n\n### object\nIf the *value* of the key is an `object`, `obj-filter` will _dive into it and check the **deeper** level of keys_.  \n\n### function\nIf the *value* of the key is an `function`, `obj-filter` will _pass the **value** of the same key in **input data** to the **function**_, and includes it's returned data in result.  \nSo it's your call to customize how you would like to handle, define what you want to do with the input data. Be sure to **return something** from your function.\n\n- If return `undefined`, the key will be **filtered** (skipped).\n- If return anything else, the key will be **included**.\n\n### DataTypes / Constructors\n`String`, `Number`, `Boolean`, `Array`, `Symbol`, `Map`, `Set`, `WeakMap`, `WeakSet`, `Object`, `Function` in template will do type checking on target object.\n\nSuccess if type matches and fails if they don't.\n\n### Anything else (string, array, number, etc)\nThe value of the key will be **included**.\n\n### onException callback function\nYou can pass an additional `onException` callback function into `filter()`, `filter.merge()`, and `filter.exist()` to handle exceptions.\n\n`onException(template, input, error_msg)` will be called when data expected but type mismatch or undefined.\n\n~~~~ js\nfilter(template, data, (tpl, obj, err) =\u003e { console.dir({tpl, obj, err}); return undefined; });\n~~~~\n\n\n## ArrayIter Check Array Elements\n\nIf you want to check `values of array`, use `ArrayIter`. It makes sure that value must be an Array and checks all elements in the array.\n\nThe first two arguments are required:\n- (required) filter / merge / exist\n- (required) template\n- (optional) option\n    - min: an Integer, default `0`. indicates at least how many elements must be valid. If the result array contains elements fewer than that number, the whole result will be `undefined`.\n    - onException: a Function, will be called when exception occurred.\n\n~~~~ js\nconst template = {\n\t\"a1\": ArrayIter(filter, {\n\t\t\"a\": String,\n\t\t\"a11\": ArrayIter(\n            exist,\n            Number,\n            {\"onException\": () =\u003e undefined}\n        ),\n\t\t\"a12\": ArrayIter(merge, {\n\t\t\t\"a121\": filter.ArrayIter(filter, Number, {\"min\": 3})\n\t\t}),\n\t}),\n\t\"a2\": ArrayIter(filter, Number)\n};\n~~~~\n\n\n## Default Function\n\n### Keep only wanted data\n\nWhen fetching data through API, sometimes the returned data could be Huge. You need many of them, but there are also too many trash included in returned data.  \nCopying with `result[xxx] = input[xxx];` each by each, line by line, is a hell.  \nNow you can copy one returned data structure (in JSON) to your favorite text editor, delete all unwanted lines, paste it back to your code, and use it as template.\n\n~~~~ js\n\"use strict\";\n\nvar filter = require('obj-filter');\n\nvar template = {\n    \"runtime\": {\n        \"connectionState\": undefined,       // In Template, when the value is undefined, the key will be ignored.\n        \"powerState\": function (args) {return \"HELLO WORLD \" + args},    // pass data into your function, and use it as result value\n        \"bootTime\": \"my boot time\",         // The string is just for your own note. Will keep whatever input is in result.\n        \"paused\": false,                    // Will keep whatever input is in result.\n        \"snapshotInBackground\": 1111111     // Will keep whatever input is in result.\n    }\n};\n\nvar data = function_or_somewhere();\n\n// Assume:\n// var data = {\n//    \"vm\": {\n//        \"type\": \"VirtualMachine\"\n//    },\n//    \"runtime\": {\n//        \"device\": 9999,\n//        \"connectionState\": \"connected\",\n//        \"powerState\": \"poweredOn\",\n//        \"bootTime\": \"2017-04-20T13:56:19.377Z\",\n//        \"paused\": false,\n//        \"snapshotInBackground\": true\n//    }\n//};\n\n\nvar clean_data = filter(template, data);\n\n// clean_data is:\n{\n    \"runtime\": {\n        \"powerState\": \"HELLO WORLD poweredOn\",\n        \"bootTime\": \"2017-04-20T13:56:19.377Z\",\n        \"paused\": false,\n        \"snapshotInBackground\": true\n    }\n};\n~~~~\n\n### User Data Checks\n\nValidate user input data in browser (before send to server), or check them at server-side.\n\n~~~~ js\nvar template = {\n    email: validateEmail(email),                // call function validateEmail and use it's return value as value\n    username: function (username) {\n        if (/^[a-zA-Z_]+$/.test(username)) {    // check if username contains only a-z or underscore\n            return username;\n        } else {\n            throw new Error('Invalid username');\n        }\n    },\n    password: \"original password\"               // keep whatever user inputs\n}\n\nsave_or_send( filter(template, inputData) );\n~~~~\n\n### Separated template file\n\nYou can save template into separated files.\n\nSay _data_template/vmInfo.js_\n\n~~~~ js\n{\n    \"runtime\": {\n        \"connectionState\": undefined,\n        \"powerState\": function (args) {return \"HELLO WORLD \" + args},\n        \"bootTime\": \"my boot time\",\n        \"paused\": false,\n        \"snapshotInBackground\": 1111111\n    }\n};\n~~~~\n\nRequire it as template\n\n~~~~ js\nvar vm_tpl = require('data_template/vmInfo.js');\n\nvar vmData = filter(vm_tpl, yourData)\n~~~~\n\n## `merge` Function\n\n### Keep template keys when not provided in input data.\n\n~~~~ js\n\"use strict\";\n\nvar filter = require('obj-filter');\n\nvar template = {\n    \"runtime\": {\n        \"connectionState\": undefined,\n        \"powerState\": function (args) {return \"HELLO WORLD \" + args},\n        \"CoffeeTeaOrMe\": \"Me\"\n    }\n};\n\nvar newUpdates = fetchChanges();\n\n// Assume:\n// var newUpdates = {\n//    \"runtime\": {\n//        \"connectionState\": \"connected\",\n//        \"powerState\": \"poweredOn\"\n//    }\n//};\n\n\nvar updated_data = filter.merge(template, newUpdates);\n\n// updated_data is:\n{\n    \"runtime\": {\n        \"powerState\": \"HELLO WORLD poweredOn\",\n        \"bootTime\": \"2017-04-20T13:56:19.377Z\",\n        \"CoffeeTeaOrMe\": \"Me\"\n   }\n};\n~~~~\n\n\n## `exist` Function\n\n### Similar to default `filter`, but All Keys in template must also exists in input data.\n\n~~~~ js\n\"use strict\";\n\nvar filter = require('obj-filter');\n\nvar template = {\n\t\"vm\": undefined,\n\t\"runtime\": {\n\t\t\"connectionState\": undefined,\n\t\t\"powerState\": function (args) {return \"HELLO WORLD \" + args},\n\t\t\"bootTime\": \"my boot time\",\n\t\t\"obj jj\": { \"kk\": \"yy\" }\n\t}\n};\n\nvar data = fetch_from_somewhere();\n\n// Assume:\n// var data = {\n// \t\"runtime\": {\n// \t\t\"device\": 9999,\n// \t\t\"connectionState\": \"connected\",\n// \t\t\"powerState\": \"poweredOn\",\n// \t\t\"bootTime\": 2,\n// \t\t\"obj jj\": { \"kk\": \"zz\" }\n// \t}\n// };\n\n\nvar clean_full_data = filter.exist(template, data);\n\n// clean_full_data is:\n{\n\t\"runtime\": {\n\t\t\"powerState\": \"HELLO WORLD poweredOn\",\n\t\t\"bootTime\": 2,\n\t\t\"obj jj\": { \"kk\": \"zz\" }\n\t}\n};\n~~~~\n\n## Contribute\n\nPRs welcome!  \nIf you use/like this module, please don't hesitate to give me a **Star**. I'll be happy whole day!\n\n_Hope this module can save your time, a tree, and a kitten._\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fbluet%2Fobj-filter.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fobj-filter?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluet%2Fobj-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbluet%2Fobj-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluet%2Fobj-filter/lists"}