spoeck
v0.0.11
Published
Specification pattern implementation
Downloads
2
Readme
Spoeck
Specification pattern library written in typescript.
Create a specification :
import { createSpec } from 'spoeck';
const dragon = {
color: 'blue',
element: 'ice',
age: 1000,
};
const dragonIsBlue = createSpec({
name: 'dragonIsBlue',
desc: 'Dragon is blue',
isSatisfiedBy: (dragon: Dragon) => dragon.color === 'blue'
});
const result = dragonIsBlue.isSatisfiedBy(dragon);
const result.value // => true
Result format :
The specification isSatisifiedBy
function will always return an object with these properties.
name
: Is the name of the specification.desc
: Is the description of the specification.value
: Is the boolean that indicate if the specification is respected.details
: Is the list of all sub specifications results.
// With a simple rule :
const result ={
name: 'dragonIsBlue',
desc: 'Dragon is blue',
value: true,
details: [
{ name: 'dragonIsBlue', desc: 'Dragon is blue', value: true },
],
};
// With a composite rule :
const dragonIsBlueIceYoung = dragonIsBlue
.and(dragonIsIce, 'dragonIsBlueIce')
.and(dragonIsYoung, 'dragonIsBlueIceYoung');
const compositeRuleResult = {
name: 'dragonIsBlueIceYoung',
desc: 'Dragon is blue AND (Dragon is ice) AND (NOT (Dragon is old))',
value: false,
details: [
{ name: 'dragonIsBlue', desc: 'Dragon is blue', value: true },
{ name: 'dragonIsIce', desc: 'Dragon is ice', value: true },
{ name: 'dragonIsYoung', desc: 'NOT (Dragon is old)', value: false },
],
}
Combining specifications with operator :
AND operator:
import { createSpec } from 'spoeck';
const dragon = {
color: 'blue',
element: 'ice',
age: 1000,
};
const dragonIsBlue = createSpec({
name: 'dragonIsBlue',
desc: 'Dragon is blue',
isSatisfiedBy: (dragon: Dragon) => entity.color === 'blue'
});
const dragonIsIce = createSpec({
name: 'dragonIsIce',
desc: 'Dragon is ice',
isSatisfiedBy: (dragon: Dragon) => entity.element === 'ice'
});
const dragonIsBlueANDIce = dragonIsBlue.and(dragonIsIce, 'dragonIsBlueANDIce');
const result = dragonIsBlueANDIce.isSatisfiedBy(dragon);
result.value; // => true
OR operator :
const dragonIsBlueORIce = dragonIsBlue.or(dragonIsIce, 'dragonIsBlueORIce');
const result = dragonIsBlueORIce.isSatisfiedBy(dragon);
result.value; // => true
XOR operator :
const dragonIsBlueXORIce = dragonIsBlue.xor(dragonIsIce, 'dragonIsBlueXORIce');
const result = dragonIsBlueXORIce.isSatisfiedBy(dragon);
result.value; // => false
NOT operator :
const dragonIsBlueAndNotIce = dragonIsBlue.and(dragonIsIce.not('dragonIsNotIce'), 'dragonIsBlueAndNotIce');
const result = dragonIsBlueAndNotIce.isSatisfiedBy(dragon);
result.value; // => false