{"id":23274632,"url":"https://github.com/adversing/decisions.js","last_synced_at":"2025-04-06T10:46:27.804Z","repository":{"id":170131050,"uuid":"602337304","full_name":"Adversing/decisions.js","owner":"Adversing","description":"decisions.js is a JavaScript library for creating decision trees. It provides a simple API for making decisions based on custom rules and conditions. Ideal for lightweight, simple decision-making applications.","archived":false,"fork":false,"pushed_at":"2024-02-02T23:29:43.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T16:32:23.844Z","etag":null,"topics":["decision-making","decision-tree","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Adversing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-02-16T01:49:38.000Z","updated_at":"2024-06-27T05:48:06.000Z","dependencies_parsed_at":"2024-02-03T00:29:08.953Z","dependency_job_id":"e0443dd2-6eb3-4748-9c50-911d12e5e378","html_url":"https://github.com/Adversing/decisions.js","commit_stats":null,"previous_names":["adversing/decisions.js"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2Fdecisions.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2Fdecisions.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2Fdecisions.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adversing%2Fdecisions.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Adversing","download_url":"https://codeload.github.com/Adversing/decisions.js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471396,"owners_count":20944154,"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":["decision-making","decision-tree","javascript"],"created_at":"2024-12-19T20:14:51.662Z","updated_at":"2025-04-06T10:46:27.758Z","avatar_url":"https://github.com/Adversing.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# decisions.js\n`decisions.js` is a JavaScript library that allows you to define a set of rules to apply to an object and execute the appropriate action based on the first matching rule. It's useful when you need to make a decision based on a complex set of conditions.\n\n---\n\n## Usage\nTo use `decisions.js`, create a new instance of it by passing the object you want to evaluate as its argument:\n```javascript\nconst person = {\n  name: 'Alice',\n  age: 30\n};\n\nconst tree = new DecisionTree(person); // in this case we're evaluating a person object\n```\n\n### Adding rules\nOnce you have a `decisions.js` instance, you can add rules to it. A rule consists of a `condition` and an `action`. The `condition` is a `function` that returns a `boolean` and evaluates to true if the `rule` should be applied, and the `action` is a `function` that specifies what should be done if the rule is applied.\n\nTo add a rule to the `DecisionTree`, call the `caseCondition()` method with the condition and the `action` as arguments. Here's an example:\n```javascript\ntree.caseCondition((p) =\u003e p.age \u003e 18, (p) =\u003e console.log(p.name + ' is an adult'));\n```\nIn this example, we're adding a rule that says \"if the person's age is greater than 18, then print 'Alice is an adult'\" (where \"Alice\" is the person's name).\n\n### Adding a default rule\nIf none of the rules match, you can specify a default action to be executed by calling the `setDefaultRule()` method:\n```javascript\ntree.setDefaultRule((p) =\u003e console.log(p.name + ' is a minor'));\n```\nIn this example, we're specifying that if none of the other rules match, the action should be to print \"Alice is a minor\".\n\n### Applying the rules\nOnce you've added all the rules, you can apply them to the object by calling the `decide()` method:\n```javascript\ntree.decide();\n```\nThis will evaluate the object and execute the first matching rule.\n\n---\n\n## Example\nHere's an example of using `decisions.js` to evaluate a `Person` object:\n```javascript\n// Create a new DecisionTree with a Person object\nconst person = {\n  name: 'Alice',\n  age: 30\n};\nconst tree = new DecisionTree(person);\n\n// Add a rule\ntree.caseCondition((p) =\u003e p.age \u003e 18, (p) =\u003e console.log(p.name + ' is an adult'));\n\n// Add a default rule\ntree.setDefaultRule((p) =\u003e console.log(p.name + ' is a minor'));\n\n// Apply the rules\ntree.decide();\n```\nIn this example, we're creating a new `DecisionTree` instance with a `Person` object. We're adding a `rule` that says \"if the person's age is greater than 18, then print 'Alice is an adult'\" (where \"Alice\" is the person's name). We're also adding a default `rule` that says \"print 'Alice is a minor' if none of the other rules match\". Finally, we're applying the rules to the person object by calling `decide()`.\n\nYou may also use this syntax:\n```javascript\n// Create a Person object\nconst person = { name: 'Alice', age: 30 };\n\n// Create a DecisionTree object and apply rules\nconst decision = new DecisionTree(person)\n  .caseCondition((p) =\u003e p.age \u003e 18, () =\u003e console.log(`${person.name} is an adult`))\n  .setDefaultRule(() =\u003e console.log(`${person.name} is a minor`))\n  .decide();\n```\n\n---\n\n## Error handling \u0026 known issues\n`decisions.js` provides some basic error handling. If you call `setDefaultRule()` with a null action, you'll get an error.\n\nKnown issues:\n  - If you don't add any rules to the `DecisionTree`, calling `decide()` will NOT result in an error. Similarly, if you don't call `decide()` after adding rules, you won't get an error as well. I might try to fix this issue as soon as possible.\n\n---\n###### check out the java version [here](https://github.com/Adversing/Decisions4J/) and the python version [here](https://github.com/Adversing/decisions.py/) :)   \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadversing%2Fdecisions.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadversing%2Fdecisions.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadversing%2Fdecisions.js/lists"}