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)
npm i @shedevro/assert --save
First you should import the library
import { Assert } from '@shedevro/assert';
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';
Then you can choice needed function.
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 result -
all
– applies an assert function to a provided array -
nullOr
– runs assertion only if input value is defined -
not
– 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
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) |
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 |
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 |
Method | Description |
---|---|
object(value, message?: string) |
Ensures that value is an object ({} , not Array or null ) |
Method | Description |
---|---|
function(value, message?: string) |
Ensures that value is a function |
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 |
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 |
Method | Description |
---|---|
match(value, regExp: RegExp, message?: string) |
Ensures that value is match a regular expression |
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 |
All credits for great idea to @webmozart with his webmozart/assert for PHP!
If you have ideas on how to improve this library, you are welcome!
Please read readme of root project directory.
The content of this package is licensed under the MIT license.