Asserts that two values are deeply equal using the deep-eql package.
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 {assert} from '@augment-vir/assert';
assert.deepEquals('a', 'a'); // passes
assert.deepEquals('1', 1); // fails
assert.deepEquals({a: 'a'}, {a: 'a'}); // passes
const objectExample = {a: 'a'};
assert.deepEquals(objectExample, objectExample); // passes
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.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.endsWith('ab', 'b'); // passes
assert.endsWith('ab', 'a'); // fails
assert.endsWith(
[
'a',
'b',
],
'b',
); // passes
assert.endsWith(
[
'a',
'b',
],
'a',
); // fails
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.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.endsWithout('ab', 'b'); // fails
assert.endsWithout('ab', 'a'); // passes
assert.endsWithout(
[
'a',
'b',
],
'b',
); // fails
assert.endsWithout(
[
'a',
'b',
],
'a',
); // passes
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.
Type guards the first value.
import {assert} from '@augment-vir/assert';
assert.entriesEqual({a: 'a'}, {a: 'a'}); // passes
assert.entriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // fails
const bExample = {b: 'b'};
assert.entriesEqual({a: bExample}, {a: bExample}); // passes
AssertionError If both inputs are not equal.
Immediately throw an assertion error.
Asserts that a parent value has the key.
Type guards the parent value.
import {assert} from '@augment-vir/assert';
assert.hasKey({a: 0, b: 1}, 'a'); // passes
assert.hasKey({a: 0, b: 1}, 'c'); // fails
AssertionError If the parent does not have the key.
Asserts that a parent value has all the keys.
Type guards the parent value.
import {assert} from '@augment-vir/assert';
assert.hasKeys({a: 0, b: 1}, [
'a',
'b',
]); // passes
assert.hasKeys({a: 0, b: 1}, [
'b',
'c',
]); // fails
AssertionError If the parent does not have all the keys.
Asserts that an object/array parent includes a child value through reference equality.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
assert.hasValue({child}, child); // passes
assert.hasValue({child: {a: 'a'}}, child); // fails
assert.hasValue([child], child); // passes
AssertionError If the assertion fails.
Asserts that an object/array parent includes all child values through reference equality.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
const child2 = {b: 'b'};
assert.hasValues({child, child2}, [
child,
child2,
]); // passes
assert.hasValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // fails
assert.hasValues(
[child],
[
child,
child2,
],
); // passes
AssertionError If the assertion fails.
Asserts that a value is an instance of the given class constructor.
Type guards the value.
Wraps the JavaScript built-in "instanceof" in a type guard assertion.
The constructor that the "instance" input will be checked against.
Optional
failureMessage: stringMessage to include in error message if this assertion fails.
import {assert} from '@augment-vir/assert';
assert.instanceOf(/abc/, RegExp); // passes
assert.instanceOf('abc', RegExp); // fails
AssertionError If the value is not an instance of the given class constructor.
Asserts that a number is above the expectation (actual > expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isAbove(10, 5); // passes
assert.isAbove(5, 5); // fails
assert.isAbove(5, 10); // fails
AssertionError If the assertion fails.
Asserts that a number is within ±delta
of the expectation.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isApproximately(10, 8, 4); // passes
assert.isApproximately(10, 12, 4); // passes
assert.isApproximately(10, 8, 1); // fails
assert.isApproximately(10, 12, 1); // fails
AssertionError If the assertion fails.
Asserts that a value is an array.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isArray([]); // passes
assert.isArray({length: 4}); // fails
AssertionError If the assertion failed.
Asserts that a number is at least the expectation (actual >= expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isAtLeast(10, 5); // passes
assert.isAtLeast(5, 5); // passes
assert.isAtLeast(5, 10); // fails
AssertionError If the assertion fails.
Asserts that a number is at most the expectation (actual <= expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isAtMost(10, 5); // fails
assert.isAtMost(5, 5); // passes
assert.isAtMost(5, 10); // passes
AssertionError If the assertion fails.
Asserts that a number is below the expectation (actual < expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isBelow(10, 5); // fails
assert.isBelow(5, 5); // fails
assert.isBelow(5, 10); // passes
AssertionError If the assertion fails.
Asserts that a value is a BigInt.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isBigInt(123n); // passes
assert.isBigInt(123); // fails
AssertionError If the assertion failed.
Asserts that a value is a boolean.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isBoolean(true); // passes
assert.isBoolean('true'); // fails
AssertionError If the assertion failed.
Asserts that a value is defined (not null
and not undefined
).
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isDefined(0); // passes
assert.isDefined(''); // passes
assert.isDefined(null); // fails
assert.isDefined(undefined); // fails
AssertionError If the assertion fails.
Asserts that a value is empty. Supports strings, Maps, Sets, objects, and arrays.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isEmpty({}); // passes
assert.isEmpty(''); // passes
assert.isEmpty([]); // passes
assert.isEmpty('a'); // fails
assert.isEmpty({a: 'a'}); // fails
AssertionError If the assertion fails.
Asserts that a child value is an enum member.
Type guards the child value.
import {assert} from '@augment-vir/assert';
enum MyEnum {
A = 'a',
B = 'b',
}
assert.isEnumValue('a', MyEnum); // passes
assert.isEnumValue('A', MyEnum); // fails
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 {assert} from '@augment-vir/assert';
assert.isError(new Error()); // passes
assert.isError(new Error(), {matchMessage: 'hi'}); // fails
assert.isError({message: 'not an error'}); // fails
AssertionError If the assertion fails.
Asserts that a value is exactly false
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isFalse(true); // fails
assert.isFalse(false); // passes
assert.isFalse(1); // fails
assert.isFalse(0); // fails
AssertionError If the value is not false
.
Asserts that a value is falsy.
Type guards the value when possible.
import {assert} from '@augment-vir/assert';
assert.isFalsy(true); // fails
assert.isFalsy(false); // passes
assert.isFalsy(1); // fails
assert.isFalsy(0); // passes
AssertionError If the value is not falsy.
Asserts that a number is finite: meaning, not NaN
and not Infinity
or -Infinity
.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isFinite(10); // passes
assert.isFinite(parseInt('invalid')); // fails
assert.isFinite(Infinity); // fails
assert.isFinite(-Infinity); // fails
AssertionError If the assertion fails.
Asserts that a value is a function.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isFunction(() => {}); // passes
assert.isFunction({}); // fails
AssertionError If the assertion failed.
Asserts that a value is an HttpStatus.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isHttpStatus(400); // passes
assert.isHttpStatus(500); // passes
assert.isHttpStatus(99); // fails
AssertionError If the value is not an HttpStatus.
Asserts that a value is an HttpStatus within a specific HttpStatusCategory.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isHttpStatusCategory(400, HttpStatusCategory.ClientError); // passes
assert.isHttpStatusCategory(500, HttpStatusCategory.Success); // fails
assert.isHttpStatusCategory(99, HttpStatusCategory.Information); // fails
AssertionError If the value is not an HttpStatus within the HttpStatusCategory.
Asserts that child value is contained within a parent object, array, or string through reference equality.
Type guards the child when possible.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
assert.isIn(child, {child}); // passes
assert.isIn('a', 'ab'); // passes
assert.isIn(child, [child]); // passes
assert.isIn(child, {child: {a: 'a'}}); // fails
assert.isIn('a', 'bc'); // fails
AssertionError If the assertion fails.
Asserts that a number is inside the provided min and max bounds, inclusive.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isInBounds(5, {min: 1, max: 10}); // passes
assert.isInBounds(10, {min: 1, max: 10}); // passes
assert.isInBounds(11, {min: 1, max: 10}); // fails
assert.isInBounds(0, {min: 1, max: 10}); // fails
AssertionError If the assertion fails.
Asserts that a number is either Infinity
or -Infinity
.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isInfinite(10); // fails
assert.isInfinite(parseInt('invalid')); // fails
assert.isInfinite(Infinity); // passes
assert.isInfinite(-Infinity); // passes
AssertionError If the assertion fails.
Asserts that a number is an integer. This has the same limitations as https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isInteger(5); // passes
assert.isInteger(5.0000000000000001); // passes
assert.isInteger(5.1); // fails
assert.isInteger(NaN); // fails
AssertionError If the assertion fails.
Asserts that a key is contained within a parent value.
Type guards the key.
import {assert} from '@augment-vir/assert';
assert.isKeyof('a', {a: 0, b: 1}); // passes
assert.isKeyof('c', {a: 0, b: 1}); // fails
AssertionError If the key is not in the parent.
Asserts that an array or string has at least the given length.
Type guards an array into an AtLeastTuple. Performs no type guarding on a string.
import {assert} from '@augment-vir/assert';
assert.isLengthAtLeast(
[
'a',
'b',
'c',
],
2,
); // passes
assert.isLengthAtLeast(
[
'a',
'b',
'c',
],
3,
); // passes
assert.isLengthAtLeast(
[
'a',
'b',
],
3,
); // fails
AssertionError If the value is less than the given length.
Asserts that an array or string has exactly the given length.
Type guards an array into a Tuple. Performs no type guarding on a string.
import {assert} from '@augment-vir/assert';
assert.isLengthExactly(
[
'a',
'b',
'c',
],
2,
); // fails
assert.isLengthExactly(
[
'a',
'b',
'c',
],
3,
); // passes
assert.isLengthExactly(
[
'a',
'b',
],
3,
); // fails
AssertionError If the value is not exactly the given length.
Asserts that a number is
NaN
.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isNaN(10); // fails
assert.isNaN(parseInt('invalid')); // passes
assert.isNaN(Infinity); // fails
AssertionError If the assertion fails.
Asserts that a number is outside ±delta
of the expectation.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isNotApproximately(10, 8, 4); // fails
assert.isNotApproximately(10, 12, 4); // fails
assert.isNotApproximately(10, 8, 1); // passes
assert.isNotApproximately(10, 12, 1); // passes
AssertionError If the assertion fails.
Asserts that a value is not an array.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotArray([]); // fails
assert.isNotArray({length: 4}); // passes
AssertionError If the assertion failed.
Asserts that a value is not a BigInt.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotBigInt(123n); // fails
assert.isNotBigInt(123); // passes
AssertionError If the assertion failed.
Asserts that a value is not a boolean.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotBoolean(true); // fails
assert.isNotBoolean('true'); // passes
AssertionError If the assertion failed.
Asserts that a value is not empty. Supports strings, Maps, Sets, objects, and arrays.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotEmpty({}); // fails
assert.isNotEmpty(''); // fails
assert.isNotEmpty([]); // fails
assert.isNotEmpty('a'); // passes
assert.isNotEmpty({a: 'a'}); // passes
AssertionError If the assertion fails.
Asserts that a child value is not an enum member.
Type guards the child value.
import {assert} from '@augment-vir/assert';
enum MyEnum {
A = 'a',
B = 'b',
}
assert.isNotEnumValue('a', MyEnum); // fails
assert.isNotEnumValue('A', MyEnum); // passes
AssertionError If the child is an enum member.
Asserts that a value is not a function.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotFunction(() => {}); // fails
assert.isNotFunction({}); // passes
AssertionError If the assertion failed.
Asserts that child value is not contained within a parent object, array, or string through reference equality.
Type guards the child when possible.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
assert.isNotIn(child, {child}); // fails
assert.isNotIn('a', 'ab'); // fails
assert.isNotIn(child, [child]); // fails
assert.isNotIn(child, {child: {a: 'a'}}); // passes
assert.isNotIn('a', 'bc'); // passes
AssertionError If the assertion fails.
Asserts that a number is not an integer. This has the same limitations, as https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isNotInteger(5); // fails
assert.isNotInteger(5.0000000000000001); // fails
assert.isNotInteger(5.1); // passes
assert.isNotInteger(NaN); // passes
AssertionError If the assertion fails.
Asserts that a key is not contained within a parent value.
Type guards the key.
import {assert} from '@augment-vir/assert';
assert.isNotKeyOf('a', {a: 0, b: 1}); // fails
assert.isNotKeyOf('c', {a: 0, b: 1}); // passes
AssertionError If the key is in the parent.
Asserts that a value is not exactly null
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotNull(null); // fails
assert.isNotNull(undefined); // passes
AssertionError If the assertion failed.
Asserts that a value is not a number. This includes NaN
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotNumber(123); // fails
assert.isNotNumber(123n); // passes
AssertionError If the assertion failed.
Asserts that a value is not an object. This includes arrays.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotObject({}); // fails
assert.isNotObject([]); // passes
AssertionError If the assertion failed.
Asserts that a value is not a JavaScript primitive.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isPrimitive('key'); // fails
assert.isPrimitive(true); // fails
assert.isPrimitive({}); // passes
AssertionError If the assertion fails.
Asserts that a value is a not Promise
instance.
Type guards the value.
import {assert} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}
assert.isNotPromise(Promise.resolve(5)); // fails
assert.isNotPromise(new CustomThenable(5)); // passes
assert.isNotPromise(5); // passes
AssertionError If the assertion fails.
Asserts that a value is not a PromiseLike
.
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 {assert} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}
assert.isNotPromiseLike(Promise.resolve(5)); // fails
assert.isNotPromiseLike(new CustomThenable(5)); // fails
assert.isNotPromiseLike(5); // passes
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.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotPropertyKey('key'); // fails
assert.isNotPropertyKey(true); // passes
assert.isNotPropertyKey({}); // passes
AssertionError If the assertion fails.
Asserts that a value is not a string.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotString(''); // fails
assert.isNotString(5); // passes
AssertionError If the assertion failed.
Asserts that a value is not a symbol.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotSymbol(Symbol('my-symbol')); // fails
assert.isNotSymbol('my-symbol'); // passes
AssertionError If the assertion failed.
Asserts that a value is not exactly undefined
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNotUndefined(undefined); // fails
assert.isNotUndefined(null); // passes
AssertionError If the assertion failed.
Asserts that a value is not a valid UUID. The nil or max UUIDs are included as not valid.
Type guards the value.
import {assert} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';
assert.isNotUuid(createUuidV4()); // fails
assert.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // fails
assert.isNotUuid('00000000-0000-0000-0000-000000000000'); // passes
assert.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // passes
assert.isNotUuid('not-a-uuid'); // passes
AssertionError If the assertion fails.
Asserts that a value is exactly null
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNull(null); // passes
assert.isNull(undefined); // fails
AssertionError If the assertion failed.
Asserts that a value is nullish (null
or undefined
).
Type guards the value.
The value to check.
Optional
failureMessage: stringMessage to include in error message if this assertion fails.
import {assert} from '@augment-vir/assert';
assert.isNullish(0); // fails
assert.isNullish(''); // fails
assert.isNullish(null); // passes
assert.isNullish(undefined); // passes
AssertionError If the assertion fails.
Asserts that a value is a number. This excludes NaN
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isNumber(123); // passes
assert.isNumber(123n); // fails
AssertionError If the assertion failed.
Asserts that a value is an object. This excludes arrays.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isObject({}); // passes
assert.isObject([]); // fails
AssertionError If the assertion failed.
Asserts that a number is outside the provided min and max bounds, exclusive.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.isOutBounds(5, {min: 1, max: 10}); // fails
assert.isOutBounds(10, {min: 1, max: 10}); // fails
assert.isOutBounds(11, {min: 1, max: 10}); // passes
assert.isOutBounds(0, {min: 1, max: 10}); // passes
AssertionError If the assertion fails.
Asserts that a value is a JavaScript primitive.
Type guards the value.
Asserts that the given value is a primitive.
Optional
failureMessage: stringimport {assert} from '@augment-vir/assert';
assert.isPrimitive('key'); // passes
assert.isPrimitive(true); // passes
assert.isPrimitive({}); // fails
AssertionError If the assertion fails.
Asserts that a value is a Promise
instance.
Type guards the value.
Checks if a value is an actual Promise
object. In reality this is just a simple wrapper for
instanceof Promise
, but it makes checking a bit more ergonomic.
Optional
failureMessage: stringimport {assert} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}
assert.isPromise(Promise.resolve(5)); // passes
assert.isPromise(new CustomThenable(5)); // fails
assert.isPromise(5); // fails
AssertionError If the assertion fails.
Asserts that a value is a PromiseLike
.
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 {assert} from '@augment-vir/assert';
class CustomThenable {
constructor(public value: any) {}
then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}
assert.isPromiseLike(Promise.resolve(5)); // passes
assert.isPromiseLike(new CustomThenable(5)); // passes
assert.isPromiseLike(5); // fails
AssertionError If the assertion fails.
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.
Type guards the value.
Asserts that the given value is a PropertyKey ( string | number | symbol).
Optional
failureMessage: stringimport {assert} from '@augment-vir/assert';
assert.isPropertyKey('key'); // passes
assert.isPropertyKey(true); // fails
assert.isPropertyKey({}); // fails
AssertionError If the assertion fails.
Asserts that a value is a string.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isString(''); // passes
assert.isString(5); // fails
AssertionError If the assertion failed.
Asserts that a value is a symbol.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isSymbol(Symbol('my-symbol')); // passes
assert.isSymbol('my-symbol'); // fails
AssertionError If the assertion failed.
Asserts that a value is exactly true
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isTrue(true); // passes
assert.isTrue(false); // fails
assert.isTrue(1); // fails
assert.isTrue(0); // fails
AssertionError If the value is not true
.
Asserts that a value is truthy.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isTruthy(true); // passes
assert.isTruthy(false); // fails
assert.isTruthy(1); // passes
assert.isTruthy(0); // fails
AssertionError If the value is not truthy.
Asserts that a value is exactly undefined
.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.isUndefined(undefined); // passes
assert.isUndefined(null); // fails
AssertionError If the assertion failed.
Asserts that a value is a valid UUID. Does not accept the nil or max UUIDs.
Type guards the value.
Checks if the input string is a valid v4 UUID.
Optional
failureMessage: stringimport {assert} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';
assert.isUuid(createUuidV4()); // passes
assert.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // passes
assert.isUuid('00000000-0000-0000-0000-000000000000'); // fails
assert.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // fails
assert.isUuid('not-a-uuid'); // fails
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.
Type guards the first value.
import {assert} from '@augment-vir/assert';
assert.jsonEquals({a: 'a'}, {a: 'a'}); // passes
assert.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // fails
AssertionError If both inputs are not equal.
Asserts that a parent value does not have the key.
Type guards the parent value.
import {assert} from '@augment-vir/assert';
assert.lacksKey({a: 0, b: 1}, 'a'); // fails
assert.lacksKey({a: 0, b: 1}, 'c'); // passes
AssertionError If the parent has the key.
Asserts that a parent value none of the keys.
Type guards the parent value.
import {assert} from '@augment-vir/assert';
assert.lacksKeys({a: 0, b: 1}, [
'b',
'c',
]); // fails
assert.lacksKeys({a: 0, b: 1}, [
'c',
'd',
]); // passes
AssertionError If the parent has any of the keys.
Asserts that an object/array parent does not include a child value through reference equality.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
assert.lacksValue({child}, child); // fails
assert.lacksValue({child: {a: 'a'}}, child); // passes
assert.lacksValue([child], child); // fails
AssertionError If the assertion fails.
Asserts that an object/array parent includes none of the provided child values through reference equality.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
const child = {a: 'a'};
const child2 = {b: 'b'};
assert.lacksValues({}, [
child,
child2,
]); // passes
assert.lacksValues({child, child2}, [
child,
child2,
]); // fails
assert.lacksValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // fails
AssertionError If the assertion fails.
Asserts that two values are loosely equal (using
==
).
Type guards the first value.
import {assert} from '@augment-vir/assert';
assert.looseEquals('a', 'a'); // passes
assert.looseEquals('1', 1); // passes
assert.looseEquals({a: 'a'}, {a: 'a'}); // fails
const objectExample = {a: 'a'};
assert.looseEquals(objectExample, objectExample); // passes
AssertionError If both inputs are not loosely equal.
Asserts that a string (first input, actual
) matches a RegExp (second input, expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.matches('hi', /^h/); // passes
assert.matches('hi', /^g/); // fails
AssertionError If the assertion fails.
Asserts that a string (first input, actual
) does not match a RegExp (second input,
expected
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.mismatches('hi', /^h/); // fails
assert.mismatches('hi', /^g/); // passes
AssertionError If the assertion fails.
Asserts that two values are not deeply equal using the deep-eql package.
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 {assert} from '@augment-vir/assert';
assert.deepEquals('a', 'a'); // false
assert.deepEquals('1', 1); // passes
assert.deepEquals({a: 'a'}, {a: 'a'}); // fails
const objectExample = {a: 'a'};
assert.deepEquals(objectExample, objectExample); // fails
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.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.notEntriesEqual({a: 'a'}, {a: 'a'}); // fails
assert.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // passes
const bExample = {b: 'b'};
assert.notEntriesEqual({a: bExample}, {a: bExample}); // fails
AssertionError If both inputs are equal.
Asserts that a value is not an instance of the given class constructor.
Type guards the value.
import {assert} from '@augment-vir/assert';
assert.notInstanceOf(/abc/, RegExp); // fails
assert.notInstanceOf('abc', RegExp); // passes
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.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.notJsonEquals({a: 'a'}, {a: 'a'}); // fails
assert.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // passes
AssertionError If both inputs are not equal.
Asserts that two values are not loosely equal (using
==
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.notLooseEquals('a', 'a'); // fails
assert.notLooseEquals('1', 1); // fails
assert.notLooseEquals({a: 'a'}, {a: 'a'}); // passes
const objectExample = {a: 'a'};
assert.notLooseEquals(objectExample, objectExample); // fails
AssertionError If both inputs are loosely equal.
Asserts that two values are not strictly equal (using
===
).
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.notStrictEquals('a', 'a'); // fails
assert.notStrictEquals('1', 1); // passes
assert.notStrictEquals({a: 'a'}, {a: 'a'}); // passes
const objectExample = {a: 'a'};
assert.notStrictEquals(objectExample, objectExample); // fails
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 "deeply equals" to whatever you want.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.output((input: number) => String(input), [5], '5'); // passes
assert.output((input: number) => String(input), [10], '5'); // fails
assert.output(assert.isLengthAtLeast, (input: number) => String(input), [5], 2); // fails
assert.output(assert.isLengthAtLeast, (input: number) => String(input), [10], 2); // passes
AssertionError If the assertion fails.
Asserts that a parent string or array starts with a specific child. This uses reference equality when the parent is an array.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.startsWith('ab', 'b'); // fails
assert.startsWith('ab', 'a'); // passes
assert.startsWith(
[
'a',
'b',
],
'b',
); // fails
assert.startsWith(
[
'a',
'b',
],
'a',
); // passes
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.
Performs no type guarding.
import {assert} from '@augment-vir/assert';
assert.startsWith('ab', 'b'); // passes
assert.startsWith('ab', 'a'); // fails
assert.startsWith(
[
'a',
'b',
],
'b',
); // passes
assert.startsWith(
[
'a',
'b',
],
'a',
); // fails
AssertionError If the parent does start with the child.
Asserts that two values are strictly equal (using
===
).
Type guards the first value.
import {assert} from '@augment-vir/assert';
assert.strictEquals('a', 'a'); // passes
assert.strictEquals('1', 1); // fails
assert.strictEquals({a: 'a'}, {a: 'a'}); // fails
const objectExample = {a: 'a'};
assert.strictEquals(objectExample, objectExample); // passes
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.
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.
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 {assert} from '@augment-vir/assert';
assert.throws(() => {
throw new Error();
}); // passes
assert.throws(
() => {
throw new Error();
},
{matchMessage: 'hi'},
); // fails
await assert.throws(Promise.reject()); // passes
assert.throws(() => {}); // fails
AssertionError If the assertion fails.
Asserts within the TypeScript type system that a given type or value matches type
expectations (using the expect-type
package.
Make sure to call a method on the first call to actually assert anything.
Use this to write type tests. Don't use this in production code. It won't cause issues, but it's a useless no-op at run-time (it's not even a type guard).
Performs no type guarding.
A group of guard methods that assert their conditions and do nothing else.
This can also be called as a standalone assertion function which asserts that its input is truthy.
Example
Throws
AssertionError When the assertion fails.
Package
@augment-vir/assert