Asserts that two values are deeply equal using the deep-eql package. Returns the first value if the assertion passes.
Note that this check may be expensive, depending on what values it is passed. Whenever possible, use simpler equality checks instead (see the see section below).
Type guards the first value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.deepEquals('a', 'a'); // returns `'a'`
assertWrap.deepEquals('1', 1); // throws an error
assertWrap.deepEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
const objectExample = {a: 'a'};
assertWrap.deepEquals(objectExample, objectExample); // returns `{a: 'a'}`
AssertionError If both inputs are not deeply equal.
Asserts that a parent string or array ends with a specific child. This uses reference equality when the parent is an array. Returns the parent if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.endsWith('ab', 'b'); // returns `'ab'`
assertWrap.endsWith('ab', 'a'); // throws an error
assertWrap.endsWith(
[
'a',
'b',
],
'b',
); // returns `['a', 'b']`
assertWrap.endsWith(
[
'a',
'b',
],
'a',
); // throws an error
AssertionError If the parent does not end with the child.
Asserts that a parent string or array does not end with a specific child. This uses reference equality when the parent is an array. Returns the parent if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.endsWithout('ab', 'b'); // throws an error
assertWrap.endsWithout('ab', 'a'); // returns `'ab'`
assertWrap.endsWithout(
[
'a',
'b',
],
'b',
); // throws an error
assertWrap.endsWithout(
[
'a',
'b',
],
'a',
); // returns `['a', 'b']`
AssertionError If the parent ends with the child.
Asserts that two objects are deeply equal by checking only their top-level values for
strict (non-deep, reference, using
===
)
equality and, if so, returns the first object.
Type guards the first value.
The first input if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.entriesEqual({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
assertWrap.entriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // throws an error
const bExample = {b: 'b'};
assertWrap.entriesEqual({a: bExample}, {a: bExample}); // returns `{a: {b: 'b'}}`
AssertionError If both inputs are not equal.
Asserts that a parent value has the key. Returns the parent if the assertion passes.
Type guards the parent value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.hasKey({a: 0, b: 1}, 'a'); // returns `{a: 0, b: 1}`
assertWrap.hasKey({a: 0, b: 1}, 'c'); // throws an error
AssertionError If the parent does not have the key.
Asserts that a parent value has all the keys. Returns the parent if the assertion passes.
Type guards the parent value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.hasKeys({a: 0, b: 1}, [
'a',
'b',
]); // returns `{a: 0, b: 1}`
assertWrap.hasKeys({a: 0, b: 1}, [
'b',
'c',
]); // throws an error
AssertionError If the parent does not have all the keys.
Asserts that an object/array parent includes a child value through reference equality. Returns the parent value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
assertWrap.hasValue({child}, child); // returns `{child}`;
assertWrap.hasValue({child: {a: 'a'}}, child); // throws an error
assertWrap.hasValue([child], child); // returns `[child]`;
AssertionError If the assertion fails.
Asserts that an object/array parent includes all child values through reference equality. Returns the parent value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
const child2 = {b: 'b'};
assertWrap.hasValues({child, child2}, [
child,
child2,
]); // returns `{child, child2}`;
assertWrap.hasValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // throws an error
assertWrap.hasValues(
[child],
[
child,
child2,
],
); // returns `[child]`;
AssertionError If the assertion fails.
Asserts that a value is an instance of the given class constructor. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.instanceOf(/abc/, RegExp); // returns `/abc/`
assertWrap.instanceOf('abc', RegExp); // throws an error
AssertionError If the value is not an instance of the given class constructor.
Asserts that a number is above the expectation (actual > expected
). Returns the number
if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isAbove(10, 5); // returns `10`
assertWrap.isAbove(5, 5); // throws an error
assertWrap.isAbove(5, 10); // throws an error
AssertionError If the assertion fails.
Asserts that a number is within ±delta
of the expectation. Returns the number if the
assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isApproximately(10, 8, 4); // returns `10`
assertWrap.isApproximately(10, 12, 4); // returns `10`
assertWrap.isApproximately(10, 8, 1); // throws an error
assertWrap.isApproximately(10, 12, 1); // throws an error
AssertionError If the assertion fails.
Asserts that a value is an array. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isArray([]); // returns `[]`
assertWrap.isArray({length: 4}); // throws an error
AssertionError If the assertion failed.
Asserts that a number is at least the expectation (actual >= expected
). Returns the
number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isAtLeast(10, 5); // returns `10`
assertWrap.isAtLeast(5, 5); // returns `5`
assertWrap.isAtLeast(5, 10); // throws an error
AssertionError If the assertion fails.
Asserts that a number is at most the expectation (actual <= expected
). Returns the
number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isAtMost(10, 5); // throws an error
assertWrap.isAtMost(5, 5); // returns `5`
assertWrap.isAtMost(5, 10); // returns `5`
AssertionError If the assertion fails.
Asserts that a number is below the expectation (actual < expected
). Returns the number
if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isBelow(10, 5); // throws an error
assertWrap.isBelow(5, 5); // throws an error
assertWrap.isBelow(5, 10); // returns `5`
AssertionError If the assertion fails.
Asserts that a value is a BigInt. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isBigInt(123n); // returns `123n`
assertWrap.isBigInt(123); // throws an error
AssertionError If the assertion failed.
Asserts that a value is a boolean. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isBoolean(true); // returns `true`
assertWrap.isBoolean('true'); // throws an error
AssertionError If the assertion failed.
Asserts that a value is defined (not null
and not undefined
). Returns the value if
the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isDefined(0); // returns `0`
assertWrap.isDefined(''); // returns `''`
assertWrap.isDefined(null); // fails
assertWrap.isDefined(undefined); // fails
AssertionError If the assertion fails.
Asserts that a value is empty. Supports strings, Maps, Sets, objects, and arrays. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isEmpty({}); // returns `{}`;
assertWrap.isEmpty(''); // returns `''`;
assertWrap.isEmpty([]); // returns `[]`;
assertWrap.isEmpty('a'); // throws an error
assertWrap.isEmpty({a: 'a'}); // throws an error
AssertionError If the assertion fails.
Asserts that a child value is an enum member. Returns the child value if the assertion passes.
Type guards the child value.
The child value if it is an enum member.
import {assertWrap} from '@augment-vir/assert';
enum MyEnum {
A = 'a',
B = 'b',
}
assertWrap.isEnumValue('a', MyEnum); // returns `'a'`
assertWrap.isEnumValue('A', MyEnum); // throws an error
AssertionError If the child is not an enum member.
Asserts that a value is an instance of the built-in Error
class and compares it to the
given ErrorMatchOptions, if provided.
Type guards the input.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isError(new Error()); // returns the error instance
assertWrap.isError(new Error(), {matchMessage: 'hi'}); // throws an error
assertWrap.isError({message: 'not an error'}); // throws an error
AssertionError If the assertion fails.
Asserts that a value is exactly false
. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isFalse(true); // throws an error
assertWrap.isFalse(false); // returns `false`
assertWrap.isFalse(1); // throws an error
assertWrap.isFalse(0); // throws an error
AssertionError If the value is not false
.
Asserts that a value is falsy. Returns the value if the assertion passes.
Type guards the value when possible.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isFalsy(true); // throws an error
assertWrap.isFalsy(false); // returns `false`
assertWrap.isFalsy(1); // throws an error
assertWrap.isFalsy(0); // returns `0`
AssertionError If the value is not falsy.
Asserts that a number is finite: meaning, not NaN
and not Infinity
or -Infinity
.
Returns the number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isFinite(10); // returns `10`
assertWrap.isFinite(parseInt('invalid')); // throws an error
assertWrap.isFinite(Infinity); // throws an error
assertWrap.isFinite(-Infinity); // throws an error
AssertionError If the assertion fails.
Asserts that a value is a function. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isFunction(() => {}); // returns `() => {}`
assertWrap.isFunction({}); // throws an error
AssertionError If the assertion failed.
Asserts that a value is an HttpStatus. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isHttpStatus(400); // returns `400`
assertWrap.isHttpStatus(500); // returns `500`
assertWrap.isHttpStatus(99); // throws an error
AssertionError If the value is not an HttpStatus.
Checks that a value is an HttpStatus within a specific HttpStatusCategory. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isHttpStatusCategory(400, HttpStatusCategory.ClientError); // returns `400`
assertWrap.isHttpStatusCategory(500, HttpStatusCategory.Success); // throws an error
assertWrap.isHttpStatusCategory(99, HttpStatusCategory.Information); // throws an error
Asserts that child value is contained within a parent object, array, or string through reference equality. Returns the child value if the assertion passes.
Type guards the child when possible.
The value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
assertWrap.isIn(child, {child}); // returns `child`;
assertWrap.isIn('a', 'ab'); // returns `'a'`;
assertWrap.isIn(child, [child]); // returns `child`;
assertWrap.isIn(child, {child: {a: 'a'}}); // throws an error
assertWrap.isIn('a', 'bc'); // throws an error
AssertionError If the assertion fails.
Asserts that a number is inside the provided min and max bounds, inclusive. Returns the number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isInBounds(5, {min: 1, max: 10}); // returns `5`
assertWrap.isInBounds(10, {min: 1, max: 10}); // returns `10`
assertWrap.isInBounds(11, {min: 1, max: 10}); // fails
assertWrap.isInBounds(0, {min: 1, max: 10}); // fails
AssertionError If the assertion fails.
Asserts that a number is either Infinity
or -Infinity
. Returns the number if the
assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isInfinite(10); // throws an error
assertWrap.isInfinite(parseInt('invalid')); // throws an error
assertWrap.isInfinite(Infinity); // returns `Infinity`
assertWrap.isInfinite(-Infinity); // returns `-Infinity`
AssertionError If the assertion fails.
Asserts that a number is an integer. Returns the number if the assertion passes. This has the same limitations as https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isInteger(5); // returns `5`
assertWrap.isInteger(5.0000000000000001); // returns `5`
assertWrap.isInteger(5.1); // fails
assertWrap.isInteger(NaN); // fails
AssertionError If the assertion fails.
Asserts that a key is contained within a parent value. Returns the key if the assertion passes.
Type guards the key.
The key if it is in the parent.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isKeyof('a', {a: 0, b: 1}); // returns `'a'`
assertWrap.isKeyof('c', {a: 0, b: 1}); // throws an error
AssertionError If the key is not in the parent.
Asserts that an array or string has at least the given length. Returns the value if the assertion passes.
Type guards an array into an AtLeastTuple. Performs no type guarding on a string.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isLengthAtLeast(
[
'a',
'b',
'c',
],
2,
); // returns `['a', 'b', 'c']`
assertWrap.isLengthAtLeast(
[
'a',
'b',
'c',
],
3,
); // returns `['a', 'b', 'c']`
assertWrap.isLengthAtLeast(
[
'a',
'b',
],
3,
); // throws an error
AssertionError If the value is less than the given length.
Asserts that an array or string has exactly the given length. Returns the value if the assertion passes.
Type guards an array into a Tuple. Performs no type guarding on a string.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isLengthExactly(
[
'a',
'b',
'c',
],
2,
); // throws an error
assertWrap.isLengthExactly(
[
'a',
'b',
'c',
],
3,
); // returns `['a', 'b', 'c']`
assertWrap.isLengthExactly(
[
'a',
'b',
],
3,
); // throws an error
AssertionError If the value is not exactly the given length.
Asserts that a number is
NaN
.
Returns the number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNaN(10); // throws an error
assertWrap.isNaN(parseInt('invalid')); // returns `NaN`
assertWrap.isNaN(Infinity); // throws an error
AssertionError If the assertion fails.
Asserts that a number is outside ±delta
of the expectation. Returns the number if the
assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotApproximately(10, 8, 4); // throws an error
assertWrap.isNotApproximately(10, 12, 4); // throws an error
assertWrap.isNotApproximately(10, 8, 1); // returns `10`
assertWrap.isNotApproximately(10, 12, 1); // returns `10`
AssertionError If the assertion fails.
Asserts that a value is not an array. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotArray([]); // throws an error
assertWrap.isNotArray({length: 4}); // returns `{length: 4}`
AssertionError If the assertion failed.
Asserts that a value is not a BigInt. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotBigInt(123n); // throws an error
assertWrap.isNotBigInt(123); // returns `123`
AssertionError If the assertion failed.
Asserts that a value is not a boolean. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotBoolean(true); // throws an error
assertWrap.isNotBoolean('true'); // returns `'true'`
AssertionError If the assertion failed.
Asserts that a value is not empty. Supports strings, Maps, Sets, objects, and arrays. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotEmpty({}); // throws an error
assertWrap.isNotEmpty(''); // throws an error
assertWrap.isNotEmpty([]); // throws an error
assertWrap.isNotEmpty('a'); // returns `'a'`;
assertWrap.isNotEmpty({a: 'a'}); // returns `{a: 'a'}`;
AssertionError If the assertion fails.
Asserts that a child value is not an enum member. Returns the child value if the assertion passes.
Type guards the child value.
The child value if it is not an enum member.
import {assertWrap} from '@augment-vir/assert';
enum MyEnum {
A = 'a',
B = 'b',
}
assertWrap.isNotEnumValue('a', MyEnum); // throws an error
assertWrap.isNotEnumValue('A', MyEnum); // returns `'A'`
AssertionError If the child is an enum member.
Asserts that a value is not a function. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotFunction(() => {}); // throws an error
assertWrap.isNotFunction({}); // returns `{}`
AssertionError If the assertion failed.
Asserts that child value is not contained within a parent object, array, or string through reference equality. Returns the child value if the assertion passes.
Type guards the child when possible.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
assertWrap.isNotIn(child, {child}); // throws an error
assertWrap.isNotIn('a', 'ab'); // throws an error
assertWrap.isNotIn(child, [child]); // throws an error
assertWrap.isNotIn(child, {child: {a: 'a'}}); // returns `child`;
assertWrap.isNotIn('a', 'bc'); // returns `'a'`;
AssertionError If the assertion fails.
Asserts that a number is not an integer. Returns the number if the assertion passes. This has the same limitations, as https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotInteger(5); // fails
assertWrap.isNotInteger(5.0000000000000001); // fails
assertWrap.isNotInteger(5.1); // returns `5.1`
assertWrap.isNotInteger(NaN); // returns `NaN`
AssertionError If the assertion fails.
Asserts that a key is not contained within a parent value. Returns the key if the assertion passes.
Type guards the key.
The key if it is not in the parent.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotKeyOf('a', {a: 0, b: 1}); // throws an error
assertWrap.isNotKeyOf('c', {a: 0, b: 1}); // returns `'c'`
AssertionError If the key is in the parent.
Asserts that a value is not exactly `null. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotNull(null); // throws an error
assertWrap.isNotNull(undefined); // returns `undefined`
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see @see
Asserts that a value is not a number. This includes `NaN. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotNumber(123); // throws an error
assertWrap.isNotNumber(123n); // returns `123n`
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see
Asserts that a value is not an object. This includes arrays. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotObject({}); // throws an error
assertWrap.isNotObject([]); // returns `[]`
AssertionError If the assertion failed.
Asserts that a value is a valid PropertyKey
. PropertyKey
is a built-in TypeScript
type which refers to all possible key types for a JavaScript object. Returns the value if
the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isPropertyKey('key'); // returns `'key'`
assertWrap.isPropertyKey(true); // throws an error
assertWrap.isPropertyKey({}); // throws an error
AssertionError If the assertion fails.
Asserts that a value is a not Promise
instance. Returns the value if the assertion
passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(
onFulfilled ? onFulfilled(this.value) : this.value,
);
}
}
assertWrap.isNotPromise(Promise.resolve(5)); // throws an error
assertWrap.isNotPromise(new CustomThenable(5)); // returns the `CustomThenable` promise
assertWrap.isNotPromise(5); // returns `5`
AssertionError If the assertion fails.
Asserts that a value is not a PromiseLike
. Returns the value if the assertion passes.
PromiseLike
is TypeScript built-in type that has a .then
method and thus behaves like
a promise. This is also referred to as a "thenable". This enables the use of third-party
promise implementations that aren't instances of the built-in Promise
class.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(
onFulfilled ? onFulfilled(this.value) : this.value,
);
}
}
assertWrap.isNotPromiseLike(Promise.resolve(5)); // throws an error
assertWrap.isNotPromiseLike(new CustomThenable(5)); // throws an error
assertWrap.isNotPromiseLike(5); // returns `5`
AssertionError If the assertion fails.
Asserts that a value is not a valid PropertyKey
. PropertyKey
is a built-in
TypeScript type which refers to all possible key types for a JavaScript object. Returns
the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotPropertyKey('key'); // throws an error
assertWrap.isNotPropertyKey(true); // returns `true`
assertWrap.isNotPropertyKey({}); // returns `{}`
AssertionError If the assertion fails.
Asserts that a value is not a string. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotString(''); // throws an error
assertWrap.isNotString(5); // returns `5`
AssertionError If the assertion failed.
Asserts that a value is not a symbol. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotSymbol(Symbol('my-symbol')); // throws an error
assertWrap.isNotSymbol('my-symbol'); // returns `'my-symbol'`
AssertionError If the assertion failed.
Asserts that a value is not exactly `undefined. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNotUndefined(undefined); // throws an error
assertWrap.isNotUndefined(null); // returns `null`
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see
Asserts that a value is not a valid UUID. The nil or max UUIDs are included as not valid. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';
assertWrap.isNotUuid(createUuidV4()); // throws an error
assertWrap.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // throws an error
assertWrap.isNotUuid('00000000-0000-0000-0000-000000000000'); // returns `'00000000-0000-0000-0000-000000000000'`
assertWrap.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // returns `'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'`
assertWrap.isNotUuid('not-a-uuid'); // returns `'not-a-uuid'`
AssertionError If the assertion fails.
Asserts that a value is exactly `null. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNull(null); // returns `null`
assertWrap.isNull(undefined); // throws an error
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see
Asserts that a value is nullish (null
or undefined
). Returns the value if the
assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNullish(0); // fails
assertWrap.isNullish(''); // fails
assertWrap.isNullish(null); // returns `null`
assertWrap.isNullish(undefined); // returns `undefined`
AssertionError If the assertion fails.
Asserts that a value is a number. This excludes `NaN. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isNumber(123); // returns `123`
assertWrap.isNumber(123n); // throws an error
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see
Asserts that a value is an object. This excludes arrays. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isObject({}); // returns `{}`
assertWrap.isObject([]); // throws an error
AssertionError If the assertion failed.
Asserts that a number is outside the provided min and max bounds, exclusive. Returns the number if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isOutBounds(5, {min: 1, max: 10}); // fails
assertWrap.isOutBounds(10, {min: 1, max: 10}); // fails
assertWrap.isOutBounds(11, {min: 1, max: 10}); // returns `11`
assertWrap.isOutBounds(0, {min: 1, max: 10}); // returns `0`
AssertionError If the assertion fails.
Asserts that a value is a JavaScript primitive. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isPrimitive('key'); // returns `'key'`
assertWrap.isPrimitive(true); // returns `true`
assertWrap.isPrimitive({}); // throws an error
AssertionError If the assertion fails.
Asserts that a value is a Promise
instance. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(
onFulfilled ? onFulfilled(this.value) : this.value,
);
}
}
assertWrap.isPromise(Promise.resolve(5)); // returns the `Promise` instance
assertWrap.isPromise(new CustomThenable(5)); // throws an error
assertWrap.isPromise(5); // throws an error
AssertionError If the assertion fails.
Asserts that a value is a PromiseLike
. Returns the value if the assertion passes.
PromiseLike
is TypeScript built-in type that has a .then
method and thus behaves like
a promise. This is also referred to as a "thenable". This enables the use of third-party
promise implementations that aren't instances of the built-in Promise
class.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(
onFulfilled ? onFulfilled(this.value) : this.value,
);
}
}
assertWrap.isPromiseLike(Promise.resolve(5)); // returns the `Promise` instance
assertWrap.isPromiseLike(new CustomThenable(5)); // returns the `CustomThenable` instance
assertWrap.isPromiseLike(5); // throws an error
AssertionError If the assertion fails.
Asserts that a value is not a JavaScript primitive. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isPrimitive('key'); // throws an error
assertWrap.isPrimitive(true); // throws an error
assertWrap.isPrimitive({}); // returns `{}`
AssertionError If the assertion fails.
Asserts that a value is a string. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isString(''); // returns `''`
assertWrap.isString(5); // throws an error
AssertionError If the assertion failed.
Asserts that a value is a symbol. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isSymbol(Symbol('my-symbol')); // returns the created symbol
assertWrap.isSymbol('my-symbol'); // throws an error
AssertionError If the assertion failed.
Asserts that a value is exactly true
. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isTrue(true); // returns `true`
assertWrap.isTrue(false); // throws an error
assertWrap.isTrue(1); // throws an error
assertWrap.isTrue(0); // throws an error
AssertionError If the value is not true
.
Asserts that a value is truthy. Returns the value if the assertion passes.
Type guards the value.
Optional
failureMessage: stringThe value if the assertion passes.
import {assertWrap} from '@augment-vir/assert';
assertWrap.isTruthy(true); // returns `true`
assertWrap.isTruthy(false); // throws an error
assertWrap.isTruthy(1); // returns `1`
assertWrap.isTruthy(0); // throws an error
AssertionError If the value is not truthy.
Asserts that a value is exactly `undefined. Returns the value if the assertion passes.
Type guards the value.
@example
import {assertWrap} from '@augment-vir/assert';
assertWrap.isUndefined(undefined); // returns `undefined`
assertWrap.isUndefined(null); // throws an error
@returns The value if the assertion passes. @throws {@link AssertionError} If the assertion failed. @see
Asserts that a value is a valid UUID. Does not accept the nil or max UUIDs. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';
assertWrap.isUuid(createUuidV4()); // returns the generated UUID
assertWrap.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // returns `'29e0f18e-6115-4982-8342-0afcadf5d611'`
assertWrap.isUuid('00000000-0000-0000-0000-000000000000'); // throws an error
assertWrap.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // throws an error
assertWrap.isUuid('not-a-uuid'); // throws an error
AssertionError If the assertion fails.
Asserts that two values are deeply equal when stringified into JSON. This will fail or may not make any sense if the values are not valid JSON. This internally sorts all given object keys so it is insensitive to object key order. Returns the first value if the assertion passes.
Type guards the first value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.jsonEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
assertWrap.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // throws an error
AssertionError If both inputs are not equal.
Asserts that a parent value does not have the key. Returns the parent if the assertion passes.
Type guards the parent value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.lacksKey({a: 0, b: 1}, 'a'); // throws an error
assertWrap.lacksKey({a: 0, b: 1}, 'c'); // returns `{a: 0, b: 1}`
AssertionError If the parent has the key.
Asserts that a parent value none of the keys. Returns the parent if the assertion passes.
Type guards the parent value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.lacksKeys({a: 0, b: 1}, [
'b',
'c',
]); // throws an error
assertWrap.lacksKeys({a: 0, b: 1}, [
'c',
'd',
]); // returns `{a: 0, b: 1}`
AssertionError If the parent has any of the keys.
Asserts that an object/array parent does not include a child value through reference equality. Returns the parent value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
assertWrap.lacksValue({child}, child); // throws an error
assertWrap.lacksValue({child: {a: 'a'}}, child); // returns `{child: {a: 'a'}}`;
assertWrap.lacksValue([child], child); // throws an error
AssertionError If the assertion fails.
Asserts that an object/array parent includes none of the provided child values through reference equality. Returns the parent value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
const child = {a: 'a'};
const child2 = {b: 'b'};
assertWrap.lacksValues({}, [
child,
child2,
]); // returns `{}`;
assertWrap.lacksValues({child, child2}, [
child,
child2,
]); // throws an error
assertWrap.lacksValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // throws an error
AssertionError If the assertion fails.
Asserts that two values are loosely equal (using
==
).
Returns the first value if the assertion passes.
Type guards the first value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.looseEquals('a', 'a'); // returns `'a'`
assertWrap.looseEquals('1', 1); // returns `'1'`
assertWrap.looseEquals({a: 'a'}, {a: 'a'}); // throws an error
const objectExample = {a: 'a'};
assertWrap.looseEquals(objectExample, objectExample); // returns `{a: 'a'}`
AssertionError If both inputs are not loosely equal.
Asserts that a string (first input, actual
) matches a RegExp (second input,
expected
). Returns the string if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.matches('hi', /^h/); // returns `'hi'`
assertWrap.matches('hi', /^g/); // throws an error
AssertionError If the assertion fails.
Asserts that a string (first input, actual
) does not match a RegExp (second input,
expected
). Returns the string if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.mismatches('hi', /^h/); // throws an error
assertWrap.mismatches('hi', /^g/); // returns `'hi'`
AssertionError If the assertion fails.
Asserts that two values are not deeply equal using the deep-eql package. Returns the first value if the assertion passes.
Note that this check may be expensive, depending on what values it is passed. Whenever possible, use simpler equality checks instead (see the see section below).
Type guards the first value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.deepEquals('a', 'a'); // returns `'a'`
assertWrap.deepEquals('1', 1); // throws an error
assertWrap.deepEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
const objectExample = {a: 'a'};
assertWrap.deepEquals(objectExample, objectExample); // returns `{a: 'a'}`
AssertionError If both inputs are deeply equal.
Asserts that two objects are not deeply equal by checking only their top-level values
for strict (non-deep, reference, using
===
)
equality and, if so, returns the first object.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.notEntriesEqual({a: 'a'}, {a: 'a'}); // throws an error
assertWrap.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // returns `{a: {b: 'b'}}`
const bExample = {b: 'b'};
assertWrap.notEntriesEqual({a: bExample}, {a: bExample}); // throws an error
AssertionError If both inputs are equal.
Asserts that a value is not an instance of the given class constructor. Returns the value if the assertion passes.
Type guards the value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.notInstanceOf(/abc/, RegExp); // throws an error
assertWrap.notInstanceOf('abc', RegExp); // returns `'abc'`
AssertionError If the value is an instance of the given class constructor.
Asserts that two values are not deeply equal when stringified into JSON. This may not make any sense if the values are not valid JSON. This internally sorts all given object keys so it is insensitive to object key order. Returns the first value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.notJsonEquals({a: 'a'}, {a: 'a'}); // throws an error
assertWrap.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // returns `{a: {b: 'b'}}`
AssertionError If both inputs are not equal.
Asserts that two values are not loosely equal (using
==
).
Returns the first value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.notLooseEquals('a', 'a'); // throws an error
assertWrap.notLooseEquals('1', 1); // throws an error
assertWrap.notLooseEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
const objectExample = {a: 'a'};
assertWrap.notLooseEquals(objectExample, objectExample); // throws an error
AssertionError If both inputs are loosely equal.
Asserts that two values are not strictly equal (using
===
).
Returns the first value if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.notStrictEquals('a', 'a'); // throws an error
assertWrap.notStrictEquals('1', 1); // returns `'1'`
assertWrap.notStrictEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`
const objectExample = {a: 'a'};
assertWrap.notStrictEquals(objectExample, objectExample); // throws an error
AssertionError If both inputs are strictly equal.
Asserts that the output of the given function deeply equals expectations. A custom asserter can optionally be provided as the first argument to change the expectation checking from the default "deeply equals" to whatever you want. Returns the output if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.output((input: number) => String(input), [5], '5'); // returns `'5'`
assertWrap.output((input: number) => String(input), [10], '5'); // throws an error
assertWrap.output(assert.isLengthAtLeast, (input: number) => String(input), [5], 2); // throws an error
assertWrap.output(assert.isLengthAtLeast, (input: number) => String(input), [10], 2); // returns `10`
AssertionError If the assertion fails.
Checks that a parent string or array starts with a specific child. This uses reference equality when the parent is an array. Returns the parent if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.startsWith('ab', 'b'); // throws an error
assertWrap.startsWith('ab', 'a'); // returns `'ab'`
assertWrap.startsWith(
[
'a',
'b',
],
'b',
); // throws an error
assertWrap.startsWith(
[
'a',
'b',
],
'a',
); // returns `['a', 'b']`
AssertionError If the parent does not start with the child.
Asserts that a parent string or array starts with a specific child. This uses reference equality when the parent is an array. Returns the parent if the assertion passes.
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.startsWith('ab', 'b'); // returns `'ab'`
assertWrap.startsWith('ab', 'a'); // throws an error
assertWrap.startsWith(
[
'a',
'b',
],
'b',
); // returns `['a', 'b']`
assertWrap.startsWith(
[
'a',
'b',
],
'a',
); // throws an error
AssertionError If the parent does start with the child.
Asserts that two values are strictly equal (using
===
).
Returns the first value if the assertion passes.
Type guards the first value.
import {assertWrap} from '@augment-vir/assert';
assertWrap.strictEquals('a', 'a'); // returns `'a'`
assertWrap.strictEquals('1', 1); // throws an error
assertWrap.strictEquals({a: 'a'}, {a: 'a'}); // throws an error
const objectExample = {a: 'a'};
assertWrap.strictEquals(objectExample, objectExample); // returns `{a: 'a'}`
AssertionError If both inputs are not strictly equal.
If a function input is provided:
Calls that function and asserts that the function throw an error, comparing the error with the given ErrorMatchOptions, if provided. Returns the Error if the assertion passes.
If a promise is provided:
Awaits the promise and asserts that the promise rejected with an error, comparing the error with the given ErrorMatchOptions, if provided. Returns the Error if the assertion passes.
This assertion will automatically type itself as async vs async based on the input. (A promise or async function inputs results in async. Otherwise, sync.)
Performs no type guarding.
import {assertWrap} from '@augment-vir/assert';
assertWrap.throws(() => {
throw new Error();
}); // returns the thrown error
assertWrap.throws(
() => {
throw new Error();
},
{matchMessage: 'hi'},
); // throws an error
await assertWrap.throws(Promise.reject()); // returns the rejection
assertWrap.throws(() => {}); // throws an error
AssertionError If the assertion fails.
A group of guard methods that do the following:
This can also be called as a standalone assertion function which asserts that its input is truthy and returns it if so.
Example
Throws
AssertionError When the assertion fails.
Package
@augment-vir/assert