@pakal/assertify
v0.2.1
Published
Assertify is package from Pakal library
Downloads
60
Maintainers
Readme
@pakal/assertify
Assertify is package from Pakal library
Install
$ yarn add @pakal/assertify
Or
$ npm install --save @pakal/assertify
Use
Module
import {
default as assertify
} from '@pakal/assertify';
Browser
<script src="https://unpkg.com/@pakal/assertify/bundle.umd.min.js"></script>
let {
assertify
} = _;
Examples
let isFoo = (value: string) => value === 'bar';
let assertFoo = assertify(isFoo, 'The value is not foo');
assertFoo('bar'); // => 'bar'
assertFoo('some'); // throw Error('The value is not foo')
let isEquals = (value: any, other: any) => value === other;
let assertEquals = assertify(isEquals, (value: any, other: any) => {
return `${value} !== ${other}`;
}, false);
assertEquals(true, true); // => void
assertEquals(0, 2); // throw Error('0 !== 2')
let isString = (value: any) => typeof value === 'string';
let assertString = assertify(isString, (value) => TypeError(`type of '${value}' is not string.`), 0);
assertString('some'); // => 'some'
assertString(true); // throw TypeError(`type of 'true' is not string`)
let contains = (items: any[], value: any) => items.indexOf(value) > -1;
let assertContains = assertify(contains, (items, value) => ({message: (`'${value}' not in ['${items.join(', ')}']`}), contains), 1);
assertContains(['red', 'black', 'green'], 'black'); // => 'black'
assertContains(['tea', 'beer'], 'coffee'); // throw {message: `'coffee' not in ['tea', 'beer']`}
License
Copyright © Yisrael Eliev, Licensed under the MIT license.