Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/wookieb/assert-generator
- Owner: wookieb
- License: mit
- Created: 2014-10-30T19:16:11.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-11T20:36:54.000Z (about 10 years ago)
- Last Synced: 2023-03-11T23:52:41.274Z (almost 2 years ago)
- Language: JavaScript
- Size: 145 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
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 Planetvar 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
```