{"id":28256731,"url":"https://github.com/zigotica/json-based-conditions-and-rules-logic-evaluator","last_synced_at":"2026-05-02T02:32:02.405Z","repository":{"id":57150172,"uuid":"419859858","full_name":"zigotica/json-based-conditions-and-rules-logic-evaluator","owner":"zigotica","description":"A script that parses some customizable conditions and rules, then outputs a predefined value of that matching rule, or a default value","archived":false,"fork":false,"pushed_at":"2024-07-04T14:21:22.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-02T08:29:59.164Z","etag":null,"topics":["custom-conditions","custom-rules","javascript","json-based-rules-engine","nodejs","rules-engine"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zigotica.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","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,"zenodo":null}},"created_at":"2021-10-21T19:57:05.000Z","updated_at":"2024-07-04T14:21:27.000Z","dependencies_parsed_at":"2025-06-17T12:35:06.217Z","dependency_job_id":"e3518cc7-a7e5-44ea-ae19-f6bb578a2d65","html_url":"https://github.com/zigotica/json-based-conditions-and-rules-logic-evaluator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zigotica/json-based-conditions-and-rules-logic-evaluator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigotica%2Fjson-based-conditions-and-rules-logic-evaluator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigotica%2Fjson-based-conditions-and-rules-logic-evaluator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigotica%2Fjson-based-conditions-and-rules-logic-evaluator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigotica%2Fjson-based-conditions-and-rules-logic-evaluator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zigotica","download_url":"https://codeload.github.com/zigotica/json-based-conditions-and-rules-logic-evaluator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zigotica%2Fjson-based-conditions-and-rules-logic-evaluator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32520835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T01:12:54.858Z","status":"online","status_checked_at":"2026-05-02T02:00:05.923Z","response_time":132,"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":["custom-conditions","custom-rules","javascript","json-based-rules-engine","nodejs","rules-engine"],"created_at":"2025-05-19T23:13:46.905Z","updated_at":"2026-05-02T02:32:02.389Z","avatar_url":"https://github.com/zigotica.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON based conditions and rules logic evaluator\n\n## What\n\nA script that parses some customizable conditions and rules, then outputs a predefined value of that matching rule, or a default value.\n\n## Why\n\nMy [z12 micropad](https://github.com/zigotica/mechanical-keyboards/tree/main/z12) uses 4 layers: vim, browser, figma and default. Encoders do different things on each layer. More often than not, when working in Figma I wanted to zoom using the right encoder, but if I had the default layer active, the encoder would scroll instead. I created a [node script (active-app-qmk-layer-updater)](https://github.com/zigotica/active-app-qmk-layer-updater) that checks the active app every half a second, and uses this script as a dependency to match the app name and title to a set of conditions and rules. This way I can get an output (in my case, a layer index) to change the kaymap layer in the QMK keyboard.\n\n## Setup\n\nTo determine which output to return, we need to match a set of conditions and rules, which are completely configurable using a simple JSON file, against a literals object.\n\n### Literals\n\nLiterals hold values to be compared against, by reference. These are not part of the main JSON file, because they are the dymamic values that the main script will be using to match against the conditions and rules. This object will be sent to the parser as a second argument.\n\nBe sure to `.trim().toLowerCase()` each value while building the literal object if you want to remove spaces from both ends of the string and make it case insensitive.\n\n### Conditions\n\nCONDITONS is an object that includes one object per condition to be parsed by the rules.\n\nEach condition has an id, and requires two values and an operator that will calculate if the condition is fulfilled.\n\nThe left hand side value is a reference to literals explained above.\n\nThe right hand value is the string we want to compare against. It can also be an array of strings. In that case, for the condition to be fulfilled, at least one of the strings in the array need to satisfy the operation.\n\nThe operator is any of the conditions defined in the conditions folder: `contains`, `ends`,  `equals` or `starts`.\n\nA condition returns a boolean, being true when it fulfills.\n\n### Rules\n\nRULES is an array that includes one object per set of conditions.\n\nEach set of conditions can have one or several conditions. In case of having more than one condition, the operator will define their logic. `or` operator will define that at least one of the conditions needs to be met in order to satisfy the rule. `and` operator  will define that all the conditions must be met in order to satisfy the rule.\n\nRules are evaluated top down.\n\nThe first rule that fulfills will break the loop and return the output value.\n\n### Default value\n\nIf no rule is satisfied, default value will be returned.\n\n## How to run it\n\nImport the parser in the script running this dependency:\n\n```\nconst { parser } = require('json-based-conditions-and-rules-logic-evaluator');\n```\n\nThen load the config JSON file (or create a static JSON file):\n\n```\nconst configfile = fs.readFileSync(path);\nconst configObject = JSON.parse(configfile);\n```\n\nCreate a literals object that will include the key/value pairs to be used as references in the conditions lhs (left hand side):\n\n```\nconst literals = {\n  'app': 'Terminal',\n  'title': 'VIM',\n};\n```\n\nIn my case, that literals object is updated every half a second. It's up to your app's purpose to update this.\n\nThen call the parser, passing the config and literals objects as arguments:\n\n```\nconst output = parser(configObject, literals);\n```\n\nThis script will evaluate the output according to the predefined conditons and rules. Your app can then consume the output and do whatever action with it.\n\n## To Do\n\n* [x] Documentation\n  * [x] Basic use\n  * [x] Configuration file\n* [x] Examples\n* [x] Tests\n\n## Users\n\n- [active-app-qmk-layer-updater](https://github.com/zigotica/active-app-qmk-layer-updater) - Sends the active app info to a QMK device to change keymap layers automatically.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzigotica%2Fjson-based-conditions-and-rules-logic-evaluator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzigotica%2Fjson-based-conditions-and-rules-logic-evaluator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzigotica%2Fjson-based-conditions-and-rules-logic-evaluator/lists"}