elite-libs/rules-machine

Serializable rules with an example ? And support Functions etc ?

iangregsondev opened this issue · 2 comments

Hi,

I came across this library and it looks like its just what I need, I found some other previously but they was either not typescript or didn't support serializing the rules and if they did support serializing then you could use any functions as part of the ruleset as underneath it was using js EVAL.

I was wondering if yours support serializing ? I notice its on your list.

Do you have an example ?

And also does it serialize all the rules and any functions part of the rule ?

I would like to store it in a database

Thanks in advance.

Are you looking for something like this?

import { ruleFactory } from '@elite-libs/rules-machine';

//get these from your db
const serializedFishRules = JSON.stringify([
  {if: 'fish == "oneFish"', then: 'fish = "twoFish"' },
  {if: 'fish == "redFish"', then: 'fish = "blueFish"' },
])

const fishRules = JSON.parse(serializedFishRules)
const fishRhyme = ruleFactory(fishRules);
// Equivalent to:
// if (fish == "oneFish") fish = "twoFish"
// if (fish == "redFish") fish = "blueFish"

fishyRhyme({fish: 'oneFish'}); // {fish: 'twoFish'}

Hi @iangregsondev - sorry for the delay.

I specifically didn't want to support serializing arbitrary functions themselves. (Mostly due to security risks.)

That said, the library has an extensible langauge engine. You can add your own custom functions here.

I'm looking at making that file configurable, so each project can have it's own extended behavior.