@shedevro/assert
v1.3.0
Published
Assertion typescript library for better, comfortable and safe coding. See readme!
Downloads
342
Maintainers
Readme
Assert
Library provides assertion utility for better and safe coding.
Features:
Right assertion return types:
Assert.someFn(value): asserts value is string
orAssert.is.someFn(value): value is string
etc.Assert functions can be chained with operators (
is
,all
,nullOr
,not
)throws WebUtilsAssertionError (extends from Error). Provides right stacktrace (especialy helpfull then coding with Angular to see links to .ts files)
Installation
npm i @shedevro/assert --save
Usage
First you should import the library
import { Assert } from '@shedevro/assert';
Custom error messages
Each function can be customized with you own error message.
Error message supports params injecting of assert functions arguments like input or expected values etc.
Most often the order of params matches the function arguments order.
For example:
const someValue = 3;
Assert.greaterThan(someValue, 10, 'This value should be greater than {2}, but got {1}');
// throw error with message 'This value should be greater than 10, but got 3';
Functions
Then you can choice needed function.
Operators
Almost all functions can be chained with different operators.
Moreover, you can combine them each other.
is
– modifies assert functions to returntrue
orfalse
as a resultall
– applies an assert function to a provided arraynullOr
– runs assertion only if input value is definednot
– inverts assertion action
For example:
// is
const someNumber = 10;
if (Assert.is.range(someNumber, 10, 15)) {
...
}
// all
class User {
id: number;
name: string;
}
const someArray = [new User(), new User];
Assert.all.instanceOf(someArray, User);
// ...futher actions
// nullOr
const someBoolean = null; // or undefined
Assert.nullOr.boolean(someBoolean);
// ...futher actions
// not
const someFunction = () => 123;
Assert.not.throws(someFunction, TypeError);
// combinations
Assert.is.nullOr.not.emptyString('some string');
Assert.not.endsWith('str', 'some suffix');
Assert.nullOr.all.greaterThan([10, 11, 12, 13], 5);
// ...and so on
String Assertions
| Method | Description |
|--------------------------------------------------------|---------------------------------------------------------------|
| string(value, message?: string)
| Ensures that value is a string |
| emptyString(value, message?: string)
| Ensures that value is an empty string |
| contains(value, subString: string, message?: string)
| Ensures that value contains substring |
| startsWith(value, prefix: string, message?: string)
| Ensures that value starts with some prefix |
| endsWith(value, suffix: string, message?: string)
| Ensures that value ends with some suffix |
| oneOf(value, values, message?: string | Ensures that value is one of values (
string/
number` array) |
Number Assertions
| Method | Description |
|--------------------------------------------------------------|-------------------------------------------------|
| number(value, message?: string)
| Ensures that value is a number |
| natural(value, message?: string)
| Ensures that value is a natural number |
| greaterThan(value, limit: number, message?: string)
| Ensures that value is greater than limit |
| greaterThanOrEqual(value, limit: number, message?: string)
| Ensures that value is greater or equal to limit |
| lessThan(value, limit: number, message?: string)
| Ensures that value is less than limit |
| lessThanOrEqual(value, limit: number, message?: string)
| Ensures that value is less or equal to limit |
| range(value, min: number, max: number, message?: string)
| Ensures that value is in range of min and max |
Boolean Assertions
| Method | Description |
|------------------------------------|---------------------------------|
| boolean(value, message?: string)
| Ensures that value is a boolean |
| true(value, message?: string)
| Ensures that value is true |
| false(value, message?: string)
| Ensures that value is false |
Object Assertions
| Method | Description |
|-----------------------------------|---------------------------------------------------------------|
| object(value, message?: string)
| Ensures that value is an object ({}
, not Array
or null
) |
Function Assertions
| Method | Description |
|-------------------------------------|----------------------------------|
| function(value, message?: string)
| Ensures that value is a function |
Array Assertions
| Method | Description |
|-------------------------------------------------------------------------|-----------------------------------------------------|
| array(value, message?: string)
| Ensures that value is an array |
| arrayLength(value, expectedLength: number, message?: string)
| Ensures that array length is equal to number |
| arrayMinLength(value, limit: number, message?: string)
| Ensures that array length is not less than limit |
| arrayMaxLength(value, limit: number, message?: string)
| Ensures that array length is not greater than limit |
| arrayLengthBetween(value, min: number, max: number, message?: string)
| Ensures that array length is inside a min and max |
Instance Assertions
| Method | Description |
|-----------------------------------------------------------|-------------------------------------------------|
| instanceOf(value, instanceClass, message?: string)
| Ensures that value is an instance of some class |
| instanceOfAny(value, instanceClasses, message?: string)
| Ensures that value is an instance of any class |
RexExp Assertions
| Method | Description |
|--------------------------------------------------|--------------------------------------------------|
| match(value, regExp: RegExp, message?: string)
| Ensures that value is match a regular expression |
Other Assertions
| Method | Description |
|----------------------------------------------------------------|----------------------------------------------------------------------|
| defined(value, message?: string)
| Ensures that value is defined (not null
, undefined
or NaN
) |
| equal(value, expect, message?: string)
| Ensures that value is equal to expec (value === expect
) |
| throws(expression: () => any, errorClass?, message?: string)
| Ensures that expression throws some error |
Authors
All credits for great idea to @webmozart with his webmozart/assert for PHP!
Contribute
If you have ideas on how to improve this library, you are welcome!
Please read readme of root project directory.
License
The content of this package is licensed under the MIT license.