1.4.0 • Published 3 years ago

mrules-engine v1.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

MRulesEngine

A package for generating objects via a set of rules.

Please use version 1.3.2 and above as below will be unstable

Installation

npm install mrules-engine

Standard Example:

const engine = require('mrules-engine');

const exampleRules1 = [{
    pathTo: 'data.name',
    pathFor: 'name'
}];

const example1 = engine({ data: { name: 'Name' } }, exampleRules1);
console.log(example1);

> { name: 'Name' };

selectorFn example

const engine = require('mrules-engine');

const exampleRules2 = [{
    pathTo: 'data.name',
    pathFor: 'name'
    selectorFn: (name) => `My Name: ${name}`
}];

const example2 = engine({ data: { name: 'Name' } }, exampleRules2);
console.log(example2);

> { name: 'My Name: Name' };

Fallback example

const engine = require('mrules-engine');

const exampleRules3 = [{
    pathTo: 'data.nameDoesNotExist',
    pathFor: 'name',
    fallback: 'Fallback Name'
}];

const example3 = engine({ data: { name: 'Name' } }, exampleRules3);
console.log(example3);

> { name: 'Fallback Name' };

Fallback with selectorFn example

const engine = require('mrules-engine');

const exampleRules4 = [{
    pathTo: 'data.nameDoesNotExist',
    pathFor: 'name',
    selectorFn: (name) => `Your Fallback Name: ${name}`,
    fallback: 'Fallback Name'
}];

const example4 = engine({ data: { name: 'Name' } }, exampleRules4);
console.log(example4);

> { name: 'Your Fallback Name: Fallback Name' };

Multiple Rules with selectorFn

const engine = require('mrules-engine');

const exampleRules5 = [{
    pathTo: 'data.name',
    pathFor: 'name',
    selectorFn: (name) => `Forename: ${name}`
}, {
    pathTo: 'data.surname',
    pathFor: 'surname',
    selectorFn: (name) => `Surname: ${name}`
}];

const example5 = engine({ data: { name: 'Foo', surname: 'Bar' } }, exampleRules5);
console.log(example5);

> { name: 'Forename: Foo', surname: 'Surname: Bar'  };
1.4.0

3 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.0

4 years ago

1.3.0

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.4.0

4 years ago