@pakal/assertify
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

@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.

Package Sidebar

Install

npm i @pakal/assertify

Weekly Downloads

7

Version

0.2.1

License

MIT

Unpacked Size

45.4 kB

Total Files

15

Last publish

Collaborators

  • yisraelx