Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/wookieb/assert-generator

Simple wrapper function to create custom assertions
https://github.com/wookieb/assert-generator

Last synced: about 1 month ago
JSON representation

Simple wrapper function to create custom assertions

Awesome Lists containing this project

README

        

#assert-generator

Very simple helper to generate assert functions for arguments checking, tests etc.

## Install
```
npm install assert-generator
```

## Examples

```js
var generateAssert = require('assert-generator');

var assertIsDefined = generateAssert(function(value) { return typeof value !== 'undefined'; }, 'Value cannot be empty');

assertIsDefined(1);
assertIsDefined(undefined); // Error: Value cannot be empty
```

Works fantastic with https://github.com/wookieb/predicates

```js
var is = require('predicates');
var Planet = function() {};

var assertPlanet = generateAssert(is.instanceOf(Planet), 'Value must be an instance of Planet');

assertPlanet(new Planet());
assertPlanet({}); // Error: Value must be an instance of Planet

var assertArrayOfPlanets = generateAssert(
is.arrayOf(is.instance(Planet)),
'Array must contain planets only'
);

assertArrayOfPlanets([new Planet(), new Planet()]);
assertArrayOfPlanets([new Planet(), {}]); // Array must contain planets only
```