{"id":13437054,"url":"https://github.com/CacheControl/json-rules-engine","last_synced_at":"2025-03-19T06:30:33.904Z","repository":{"id":41086507,"uuid":"50392353","full_name":"CacheControl/json-rules-engine","owner":"CacheControl","description":"A rules engine expressed in JSON","archived":false,"fork":false,"pushed_at":"2025-02-20T13:48:34.000Z","size":1288,"stargazers_count":2753,"open_issues_count":61,"forks_count":486,"subscribers_count":70,"default_branch":"master","last_synced_at":"2025-03-18T16:15:31.136Z","etag":null,"topics":["business-rules","engine","json","rule-engine","rules","rules-engine","rules-processor"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CacheControl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"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}},"created_at":"2016-01-26T00:55:41.000Z","updated_at":"2025-03-18T08:59:50.000Z","dependencies_parsed_at":"2024-06-18T11:14:53.420Z","dependency_job_id":"ae9db280-1f9c-4f40-b78b-2b1e161fb7ba","html_url":"https://github.com/CacheControl/json-rules-engine","commit_stats":{"total_commits":417,"total_committers":31,"mean_commits":"13.451612903225806","dds":0.657074340527578,"last_synced_commit":"ae8fa0553968ce3ac2dd5dfa9508ce208a852047"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CacheControl%2Fjson-rules-engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CacheControl%2Fjson-rules-engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CacheControl%2Fjson-rules-engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CacheControl%2Fjson-rules-engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CacheControl","download_url":"https://codeload.github.com/CacheControl/json-rules-engine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244370748,"owners_count":20442299,"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":["business-rules","engine","json","rule-engine","rules","rules-engine","rules-processor"],"created_at":"2024-07-31T03:00:54.029Z","updated_at":"2025-03-19T06:30:33.894Z","avatar_url":"https://github.com/CacheControl.png","language":"JavaScript","readme":"![json-rules-engine](http://i.imgur.com/MAzq7l2.png)\n[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)\n[![Build Status](https://github.com/cachecontrol/json-rules-engine/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/cachecontrol/json-rules-engine/workflows/Node.js%20CI/badge.svg?branch=master)\n\n[![npm version](https://badge.fury.io/js/json-rules-engine.svg)](https://badge.fury.io/js/json-rules-engine)\n[![install size](https://packagephobia.now.sh/badge?p=json-rules-engine)](https://packagephobia.now.sh/result?p=json-rules-engine)\n[![npm downloads](https://img.shields.io/npm/dm/json-rules-engine.svg)](https://www.npmjs.com/package/json-rules-engine)\n\nA rules engine expressed in JSON\n\n* [Synopsis](#synopsis)\n* [Features](#features)\n* [Installation](#installation)\n* [Docs](#docs)\n* [Examples](#examples)\n* [Basic Example](#basic-example)\n* [Advanced Example](#advanced-example)\n* [Debugging](#debugging)\n    * [Node](#node)\n    * [Browser](#browser)\n* [Related Projects](#related-projects)\n* [License](#license)\n\n## Synopsis\n\n```json-rules-engine``` is a powerful, lightweight rules engine.  Rules are composed of simple json structures, making them human readable and easy to persist.\n\n## Features\n\n* Rules expressed in simple, easy to read JSON\n* Full support for ```ALL``` and ```ANY``` boolean operators, including recursive nesting\n* Fast by default, faster with configuration; priority levels and cache settings for fine tuning performance\n* Secure; no use of eval()\n* Isomorphic; runs in node and browser\n* Lightweight \u0026 extendable; 17kb gzipped w/few dependencies\n\n## Installation\n\n```bash\n$ npm install json-rules-engine\n```\n\n## Docs\n\n- [engine](./docs/engine.md)\n- [rules](./docs/rules.md)\n- [almanac](./docs/almanac.md)\n- [facts](./docs/facts.md)\n\n## Examples\n\nSee the [Examples](./examples), which demonstrate the major features and capabilities.\n\n## Basic Example\n\nThis example demonstrates an engine for detecting whether a basketball player has fouled out (a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out).\n\n```js\nconst { Engine } = require('json-rules-engine')\n\n\n/**\n * Setup a new engine\n */\nlet engine = new Engine()\n\n// define a rule for detecting the player has exceeded foul limits.  Foul out any player who:\n// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)\nengine.addRule({\n  conditions: {\n    any: [{\n      all: [{\n        fact: 'gameDuration',\n        operator: 'equal',\n        value: 40\n      }, {\n        fact: 'personalFoulCount',\n        operator: 'greaterThanInclusive',\n        value: 5\n      }]\n    }, {\n      all: [{\n        fact: 'gameDuration',\n        operator: 'equal',\n        value: 48\n      }, {\n        fact: 'personalFoulCount',\n        operator: 'greaterThanInclusive',\n        value: 6\n      }]\n    }]\n  },\n  event: {  // define the event to fire when the conditions evaluate truthy\n    type: 'fouledOut',\n    params: {\n      message: 'Player has fouled out!'\n    }\n  }\n})\n\n/**\n * Define facts the engine will use to evaluate the conditions above.\n * Facts may also be loaded asynchronously at runtime; see the advanced example below\n */\nlet facts = {\n  personalFoulCount: 6,\n  gameDuration: 40\n}\n\n// Run the engine to evaluate\nengine\n  .run(facts)\n  .then(({ events }) =\u003e {\n    events.map(event =\u003e console.log(event.params.message))\n  })\n\n/*\n * Output:\n *\n * Player has fouled out!\n */\n```\n\nThis is available in the [examples](./examples/02-nested-boolean-logic.js)\n\n## Advanced Example\n\nThis example demonstates an engine for identifying employees who work for Microsoft and are taking Christmas day off.\n\nThis  demonstrates an engine which uses asynchronous fact data.\nFact information is loaded via API call during runtime, and the results are cached and recycled for all 3 conditions.\nIt also demonstates use of the condition _path_ feature to reference properties of objects returned by facts.\n\n```js\nconst { Engine } = require('json-rules-engine')\n\n// example client for making asynchronous requests to an api, database, etc\nimport apiClient from './account-api-client'\n\n/**\n * Setup a new engine\n */\nlet engine = new Engine()\n\n/**\n * Rule for identifying microsoft employees taking pto on christmas\n *\n * the account-information fact returns:\n *  { company: 'XYZ', status: 'ABC', ptoDaysTaken: ['YYYY-MM-DD', 'YYYY-MM-DD'] }\n */\nlet microsoftRule = {\n  conditions: {\n    all: [{\n      fact: 'account-information',\n      operator: 'equal',\n      value: 'microsoft',\n      path: '$.company' // access the 'company' property of \"account-information\"\n    }, {\n      fact: 'account-information',\n      operator: 'in',\n      value: ['active', 'paid-leave'], // 'status' can be active or paid-leave\n      path: '$.status' // access the 'status' property of \"account-information\"\n    }, {\n      fact: 'account-information',\n      operator: 'contains', // the 'ptoDaysTaken' property (an array) must contain '2016-12-25'\n      value: '2016-12-25',\n      path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of \"account-information\"\n    }]\n  },\n  event: {\n    type: 'microsoft-christmas-pto',\n    params: {\n      message: 'current microsoft employee taking christmas day off'\n    }\n  }\n}\nengine.addRule(microsoftRule)\n\n/**\n * 'account-information' fact executes an api call and retrieves account data, feeding the results\n * into the engine.  The major advantage of this technique is that although there are THREE conditions\n * requiring this data, only ONE api call is made.  This results in much more efficient runtime performance\n * and fewer network requests.\n */\nengine.addFact('account-information', function (params, almanac) {\n  console.log('loading account information...')\n  return almanac.factValue('accountId')\n    .then((accountId) =\u003e {\n      return apiClient.getAccountInformation(accountId)\n    })\n})\n\n// define fact(s) known at runtime\nlet facts = { accountId: 'lincoln' }\nengine\n  .run(facts)\n  .then(({ events }) =\u003e {\n    console.log(facts.accountId + ' is a ' + events.map(event =\u003e event.params.message))\n  })\n\n/*\n * OUTPUT:\n *\n * loading account information... // \u003c-- API call is made ONCE and results recycled for all 3 conditions\n * lincoln is a current microsoft employee taking christmas day off\n */\n```\n\nThis is available in the [examples](./examples/03-dynamic-facts.js)\n\n## Debugging\n\nTo see what the engine is doing under the hood, debug output can be turned on via:\n\n### Node\n\n```bash\nDEBUG=json-rules-engine\n```\n\n### Browser\n```js\n// set debug flag in local storage \u0026 refresh page to see console output\nlocalStorage.debug = 'json-rules-engine'\n```\n\n## Related Projects\n\nhttps://github.com/vinzdeveloper/json-rule-editor - configuration ui for json-rules-engine:\n\n\u003cimg width=\"1680\" alt=\"rule editor 2\" src=\"https://user-images.githubusercontent.com/61467683/82750274-dd3b3b80-9da6-11ea-96eb-434a6a1a9bc1.png\"\u003e\n\n\n## License\n[ISC](./LICENSE)\n","funding_links":[],"categories":["JavaScript","Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCacheControl%2Fjson-rules-engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCacheControl%2Fjson-rules-engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCacheControl%2Fjson-rules-engine/lists"}