An open API service indexing awesome lists of open source software.

https://github.com/georgesalkhouri/ts-specification

Specification pattern implementation for NodeJS in TypeScript
https://github.com/georgesalkhouri/ts-specification

javascript specification-pattern typescript

Last synced: 10 months ago
JSON representation

Specification pattern implementation for NodeJS in TypeScript

Awesome Lists containing this project

README

          

Specification pattern for JavaScript implemented with TypeScript
================================================================

[![Build Status](https://travis-ci.org/GeorgesAlkhouri/ts-specification.svg?branch=master)](https://travis-ci.org/GeorgesAlkhouri/ts-specification)

> In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic.
>
> -- Wikipedia.org ( https://en.wikipedia.org/wiki/Specification_pattern )

Usage With Node
---------------

To use ts-specification with Node just run:

`npm install ts-specification`

and import the package afterwards:

`import Specification from 'ts-specification'`

The test cases are written in JavaScript and can be tested with:

`npm test`

Create Custom Specs
-------------------

To create custom specifications just extend from the `CompositeSpecification` class and implement the `isSatisfiedBy` function.

**TypeScript**

```
class RangeSpecification extends CompositeSpecification {

private a: T
private b: T

constructor(a: T, b: T) {
super()
this.a = a
this.b = b
}

isSatisfiedBy(candidate: T): boolean {
return candidate >= this.a && candidate <= this.b
}
}
```

**JavaScript**

```
import {CompositeSpecification} from 'ts-specification';

class TrueSpecification extends CompositeSpecification {

isSatisfiedBy(candidate) {
return true;
}
}
```