0.2.1 • Published 5 years ago

@pakal/assertify v0.2.1

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

@pakal/assertify

Source Code Version MIT License Bundle Size TypeScript

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.