New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

spoeck

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spoeck

Specification pattern implementation

latest
Source
npmnpm
Version
0.0.11
Version published
Maintainers
1
Created
Source

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

Keywords

DDD

FAQs

Package last updated on 22 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts