Skip to content

Returns Affect Subsequent RulesΒ #35

@dara-rockwell

Description

@dara-rockwell

This one may be by design, but it makes removal of an item via rules a little clumsy.

Perhaps there's a better example that can be added to the docs for removing an object from an array by rules that check properties of the objects.

Succeeds

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

const things = [
  {
    name: "ABC",
    value: "A Thing"
  },
  {
    name: "DEF",
    value: "A Stuff"
  },
  {
    name: "GHI",
    value: "A Thing"
  },
  {
    name: "JKL",
    value: "A Thing"
  }];

const thingRules = ([
  {
    if: {
      and: [
        'CONTAINS(thing.name, ["ABC", "GHI"])',
        'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
      ]
    }, then: "keep = false"
  },
  {
    if: 'LAST(SPLIT("", thing.name)) == "C"', then: 'keep = false'
  },
  {
    return: "keep"
  }
]);

const filterWithRules = ruleFactory(thingRules)

function isRateFiltered(thing: Record<string, string>): boolean {
  const results = filterWithRules({
    thing,
    keep: true
  });
  return results;
}

console.dir(things.filter(isRateFiltered));

Fails

PARSER FAIL: Error: Expected string, found: undefined undefined
import { ruleFactory } from '@elite-libs/rules-machine';

const things = [
  {
    name: "ABC",
    value: "A Thing"
  },
  {
    name: "DEF",
    value: "A Stuff"
  },
  {
    name: "GHI",
    value: "A Thing"
  },
  {
    name: "JKL",
    value: "A Thing"
  }];

const thingRules = ([
  {
    if: {
      and: [
        'CONTAINS(thing.name, ["ABC", "GHI"])',
        'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
      ]
    }, then: "thing = false"
  },
  {
    if: 'LAST(SPLIT("", thing.name)) == "C"', then: 'thing = false'
  },
  {
    return: "thing"
  }
]);

const filterWithRules = ruleFactory(thingRules)

function isRateFiltered(thing: Record<string, string>): boolean {
  const results = filterWithRules({
    thing
  });
  return results;
}

console.dir(things.filter(isRateFiltered));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions