Function checkWrap

A group of guard methods that do the following:

  1. Run the method's assertion on the given inputs.
  2. If the assertion fails, return undefined.
  3. If the assertion succeeds, the first input is returned and (when possible) type guarded.

This can also be called as a standalone check function which checks that its input is truthy and returns it if so, else undefined.

import {checkWrap} from '@augment-vir/assert';

// `result1` will be `['a']`
const result1 = checkWrap.deepEquals(['a'], ['a']);

const value: unknown = 'some value' as unknown;
// `result2` will be `'some value'` and it will have the type of `string`
const result2 = checkWrap.isString(value);

const value2: unknown = 'some value' as unknown;
// `result` will be `undefined`
const result3 = checkWrap.isNumber(value2);

Properties

endsWith: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string,
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
} = ...

Checks that a parent string or array ends with a specific child. This uses reference equality when the parent is an array. Returns the value if the check passes, otherwise undefined.

Performs no type guarding.

import {checkWrap} from '@augment-vir/assert';

checkWrap.endsWith('ab', 'b'); // returns `'ab'`
checkWrap.endsWith('ab', 'a'); // returns `undefined`
checkWrap.endsWith(['a', 'b'], 'b'); // returns `['a', 'b']`
checkWrap.endsWith(['a', 'b'], 'a'); // returns `undefined`

The first value if the check passes, otherwise undefined.

endsWithout: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string,
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
} = ...

Checks 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 value if the check passes, otherwise undefined.

Performs no type guarding.

import {checkWrap} from '@augment-vir/assert';

checkWrap.endsWithout('ab', 'b'); // returns `undefined`
checkWrap.endsWithout('ab', 'a'); // returns `'ab'`
checkWrap.endsWithout(['a', 'b'], 'b'); // returns `undefined`
checkWrap.endsWithout(['a', 'b'], 'a'); // returns `['a', 'b']`

The first value if the check passes, otherwise undefined.

isDefined: undefined = undefined
isLengthAtLeast: {
    <Element, Length extends number>(
        actual: readonly (undefined | Element)[],
        length: Length,
    ): AtLeastTuple<Element, Length> | undefined;
    <Actual extends string | AnyObject>(
        actual: Actual,
        length: number,
    ): Actual | undefined;
} = ...

Checks that an array or string has at least the given length. Returns the value if the check passes, otherwise undefined.

Type guards an array into an AtLeastTuple. Performs no type guarding on a string.

import {checkWrap} from '@augment-vir/assert';

checkWrap.isLengthAtLeast(['a', 'b', 'c'], 2); // returns `['a', 'b', 'c']`
checkWrap.isLengthAtLeast(['a', 'b', 'c'], 3); // returns `['a', 'b', 'c']`
checkWrap.isLengthAtLeast(['a', 'b'], 3); // returns `undefined`

The value if the check passes, otherwise undefined.

isLengthExactly: {
    <Element, Length extends number>(
        actual: readonly (undefined | Element)[],
        length: Length,
    ): Tuple<Element, Length> | undefined;
    <Actual extends string | AnyObject>(
        actual: Actual,
        length: number,
    ): Actual | undefined;
} = ...

Checks that an array or string has exactly the given length. Returns the value if the check passes, otherwise undefined.

Type guards an array into a Tuple. Performs no type guarding on a string.

import {checkWrap} from '@augment-vir/assert';

checkWrap.isLengthExactly(['a', 'b', 'c'], 2); // returns `undefined`
checkWrap.isLengthExactly(['a', 'b', 'c'], 3); // returns `['a', 'b', 'c']`
checkWrap.isLengthExactly(['a', 'b'], 3); // returns `undefined`

The value if the check passes, otherwise undefined.

isNullish: undefined = undefined
output: {
    <const FunctionToCall extends AnyFunction>(
        functionToCall: FunctionToCall,
        inputs: Parameters<NoInfer<FunctionToCall>>,
        expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>,
        failureMessage?: string,
    ): OutputReturn<
        FunctionToCall,
        Awaited<ReturnType<NoInfer<FunctionToCall>>>
        | undefined,
    >;
    <const FunctionToCall extends AnyFunction>(
        asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>,
        functionToCall: FunctionToCall,
        inputs: Parameters<NoInfer<FunctionToCall>>,
        expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>,
        failureMessage?: string,
    ): OutputReturn<
        FunctionToCall,
        Awaited<ReturnType<NoInfer<FunctionToCall>>>
        | undefined,
    >;
} = checkWrapOutput

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 check passes, otherwise undefined.

Performs no type guarding.

import {checkWrap} from '@augment-vir/assert';

checkWrap.output((input: number) => String(input), [5], '5'); // returns `'5'`
checkWrap.output((input: number) => String(input), [10], '5'); // returns `undefined`

checkWrap.output(assert.isLengthAtLeast, (input: number) => String(input), [5], 2); // returns `undefined`
checkWrap.output(assert.isLengthAtLeast, (input: number) => String(input), [10], 2); // returns `10`

The output if the assertion passes, otherwise undefined.

startsWith: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string,
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
} = ...

Checks that a parent string or array starts with a specific child. This uses reference equality when the parent is an array. Returns the value if the check passes, otherwise undefined.

Performs no type guarding.

import {checkWrap} from '@augment-vir/assert';

checkWrap.startsWith('ab', 'b'); // returns `undefined`
checkWrap.startsWith('ab', 'a'); // returns `'ab'`
checkWrap.startsWith(['a', 'b'], 'b'); // returns `undefined`
checkWrap.startsWith(['a', 'b'], 'a'); // returns `['a', 'b']`

The first value if the check passes, otherwise undefined.

startsWithout: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string,
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): typeof parent | undefined;
} = ...

Checks that a parent string or array starts with a specific child. This uses reference equality when the parent is an array. Returns the value if the check passes, otherwise undefined.

Performs no type guarding.

import {checkWrap} from '@augment-vir/assert';

checkWrap.startsWith('ab', 'b'); // returns `undefined`
checkWrap.startsWith('ab', 'a'); // returns `'ab'`
checkWrap.startsWith(['a', 'b'], 'b'); // returns `undefined`
checkWrap.startsWith(['a', 'b'], 'a'); // returns `['a', 'b']`

The first value if the check passes, otherwise undefined.

throws: {
    (
        this: void,
        callbackOrPromise: () => never,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
        failureMessage?: string,
    ): Error | undefined;
    (
        this: void,
        callbackOrPromise: Promise<any> | () => Promise<any>,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
        failureMessage?: string,
    ): Promise<Error | undefined>;
    (
        this: void,
        callback: () => any,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
        failureMessage?: string,
    ): Error | undefined;
    (
        this: void,
        callback: Promise<any> | () => any,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
        failureMessage?: string,
    ): MaybePromise<Error | undefined>;
} = throwsCheckWrap

If a function input is provided:

Calls that function and checks that the function throw an error, comparing the error with the given ErrorMatchOptions, if provided. Returns the error if the check passes, otherwise undefined.

If a promise is provided:

Awaits the promise and checks that the promise rejected with an error, comparing the error with the given ErrorMatchOptions, if provided. Returns the error if the check passes, otherwise undefined.

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 {checkWrap} from '@augment-vir/assert';

checkWrap.throws(() => {
throw new Error();
}); // returns the thrown error
await checkWrap.throws(Promise.reject()); // returns the rejection
checkWrap.throws(() => {}); // returns `undefined`

The Error if the check passes, otherwise undefined.

Methods

  • Checks that two values are deeply equal using the deep-eql package. Returns the first value if the check 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.

    Type Parameters

    • Actual
    • Expected

    Parameters

    Returns undefined | NarrowToExpected<Actual, Expected>

    The first value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.deepEquals('a', 'a'); // true

    checkWrap.deepEquals('1', 1); // false

    checkWrap.deepEquals({a: 'a'}, {a: 'a'}); // true

    const objectExample = {a: 'a'};
    checkWrap.deepEquals(objectExample, objectExample); // true
  • Checks that two objects are deeply equal by checking only their top-level values for strict (non-deep, reference, using ===) equality. If the check passes the first object is returned. If not, undefined is returned.

    Type guards the first value.

    Type Parameters

    • Actual extends object
    • Expected extends object

    Parameters

    Returns undefined | NarrowToExpected<Actual, Expected>

    The first input if the assertion passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.entriesEqual({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`

    checkWrap.entriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // returns `undefined`

    const bExample = {b: 'b'};
    checkWrap.entriesEqual({a: bExample}, {a: bExample}); // returns `{a: {b: 'b'}}`
  • Checks that a parent value has the key. Returns the parent value if the check passes, otherwise undefined.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

    Returns undefined | CombineTypeWithKey<Key, Parent>

    The parent value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.hasKey({a: 0, b: 1}, 'a'); // returns `{a: 0, b: 1}`
    checkWrap.hasKey({a: 0, b: 1}, 'c'); // returns `undefined`
  • Checks that a parent value has all the keys. Returns the parent value if the check passes, otherwise undefined.

    Type guards the parent value.

    Type Parameters

    • const Keys extends PropertyKey
    • const Parent

    Parameters

    Returns undefined | CombineTypeWithKey<Keys, Parent>

    The parent value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.hasKeys({a: 0, b: 1}, ['a', 'b']); // returns `{a: 0, b: 1}`
    checkWrap.hasKeys({a: 0, b: 1}, ['b', 'c']); // returns `undefined`
  • Checks that an object/array parent includes a child value through reference equality.

    Performs no type guarding.

    Type Parameters

    • Parent extends string | object

    Parameters

    • this: void
    • parent: Parent
    • value: unknown

    Returns undefined | Parent

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};

    checkWrap.hasValue({child}, child); // returns `{child}`
    checkWrap.hasValue({child: {a: 'a'}}, child); // returns `undefined`
    checkWrap.hasValue([child], child); // returns `[child]`
  • Checks that an object/array parent includes all child values through reference equality.

    Performs no type guarding.

    Type Parameters

    • Parent extends string | object

    Parameters

    • this: void
    • parent: Parent
    • values: unknown[]

    Returns undefined | Parent

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};
    const child2 = {b: 'b'};

    checkWrap.hasValues({child, child2}, [child, child2]); // returns `{child, child2}`
    checkWrap.hasValues({child: {a: 'a'}, child2}, [child, child2]); // returns `undefined`
    checkWrap.hasValues([child], [child, child2]); // returns `[child]`
  • Checks that a value is an instance of the given class constructor. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • const Instance

    Parameters

    • this: void
    • instance: unknown
    • constructor: Constructor<Instance>

    Returns undefined | Instance

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.instanceOf(/abc/, RegExp); // returns `/abc/`
    checkWrap.instanceOf('abc', RegExp); // returns `undefined`
  • Checks that a number is above the expectation (actual > expected). Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isAbove(10, 5); // returns `10`
    checkWrap.isAbove(5, 5); // returns `undefined`
    checkWrap.isAbove(5, 10); // returns `undefined`
  • Checks that a number is within ±delta of the expectation. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number
    • delta: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isApproximately(10, 8, 4); // returns `10`
    checkWrap.isApproximately(10, 12, 4); // returns `10`
    checkWrap.isApproximately(10, 8, 1); // returns `undefined`
    checkWrap.isApproximately(10, 12, 1); // returns `undefined`
  • Checks that a value is an array. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | ArrayNarrow<Actual>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isArray([]); // returns `[]`
    checkWrap.isArray({length: 4}); // returns `undefined`
  • Checks that a number is at least the expectation (actual >= expected). Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isAtLeast(10, 5); // returns `10`
    checkWrap.isAtLeast(5, 5); // returns `5`
    checkWrap.isAtLeast(5, 10); // returns `undefined`
  • Checks that a number is at most the expectation (actual <= expected). Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isAtMost(10, 5); // returns `undefined`
    checkWrap.isAtMost(5, 5); // returns `5`
    checkWrap.isAtMost(5, 10); // returns `5`
  • Checks that a number is below the expectation (actual < expected). Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isBelow(10, 5); // returns `undefined`
    checkWrap.isBelow(5, 5); // returns `undefined`
    checkWrap.isBelow(5, 10); // returns `5`
  • Checks that a value is an instance of the built-in Error class and compares it to the given ErrorMatchOptions, if provided. Returns the error if the check passes, otherwise undefined.

    Type guards the input.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalmatchOptions: PartialWithNullable<
          {
              matchConstructor: ErrorConstructor
              | new (...args: any[]) => Error;
              matchMessage: string | RegExp;
          },
      >

    Returns undefined | NarrowToExpected<Actual, Error>

    The Error if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isError(new Error()); // returns the Error
    checkWrap.isError(new Error(), {matchMessage: 'hi'}); // returns `undefined`
    checkWrap.isError({message: 'not an error'}); // returns `undefined`
  • Checks that a value is exactly false. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown

    Returns undefined | false

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isFalse(true); // returns `false`
    checkWrap.isFalse(false); // returns `true`
    checkWrap.isFalse(1); // returns `false`
    checkWrap.isFalse(0); // returns `false`
  • Checks that a value is falsy. Returns the value if the check passes, otherwise undefined.

    Type guards the value when possible.

    Type Parameters

    • T

    Parameters

    • this: void
    • input: T

    Returns undefined | NarrowToExpected<T, FalsyValue>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isFalsy(true); // returns `false`
    checkWrap.isFalsy(false); // returns `true`
    checkWrap.isFalsy(1); // returns `false`
    checkWrap.isFalsy(0); // returns `true`
  • Checks that a number is finite: meaning, not NaN and not Infinity or -Infinity. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isFinite(10); // returns `10`
    checkWrap.isFinite(parseInt('invalid')); // returns `undefined`
    checkWrap.isFinite(Infinity); // returns `undefined`
    checkWrap.isFinite(-Infinity); // returns `undefined`
  • Checks that child value is contained within a parent object, array, or string through reference equality.

    Type guards the child when possible.

    Type Parameters

    • Parent extends string | object
    • Child

    Parameters

    Returns undefined | Extract<Child, Values<Parent>>

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};

    checkWrap.isIn(child, {child}); // returns `child`
    checkWrap.isIn('a', 'ab'); // returns `'a'`
    checkWrap.isIn(child, [child]); // returns `child`

    checkWrap.isIn(child, {child: {a: 'a'}}); // returns `undefined`
    checkWrap.isIn('a', 'bc'); // returns `undefined`
  • Checks that a number is inside the provided min and max bounds, inclusive. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    Returns undefined | Actual

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isInBounds(5, {min: 1, max: 10}); // returns `5`
    checkWrap.isInBounds(10, {min: 1, max: 10}); // returns `10`
    checkWrap.isInBounds(11, {min: 1, max: 10}); // returns `undefined`
    checkWrap.isInBounds(0, {min: 1, max: 10}); // returns `undefined`
  • Checks that a number is either Infinity or -Infinity. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isInfinite(10); // returns `undefined`
    checkWrap.isInfinite(parseInt('invalid')); // returns `undefined`
    checkWrap.isInfinite(Infinity); // returns `Infinity`
    checkWrap.isInfinite(-Infinity); // returns `-Infinity`
  • Checks that a key is contained within a parent value. Returns the key if the check passes, otherwise undefined.

    Type guards the key.

    Type Parameters

    • const Key extends PropertyKey
    • const Parent

    Parameters

    Returns undefined | NarrowToExpected<Key, keyof Parent>

    The key if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isKeyof('a', {a: 0, b: 1}); // returns `'a'`
    checkWrap.isKeyof('c', {a: 0, b: 1}); // returns `undefined`
  • Checks that a number is NaN. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNaN(10); // returns `undefined`
    checkWrap.isNaN(parseInt('invalid')); // returns `NaN`
    checkWrap.isNaN(Infinity); // returns `undefined`
  • Checks that a number is outside ±delta of the expectation. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    • this: void
    • actual: Actual
    • expected: number
    • delta: number

    Returns undefined | Actual

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotApproximately(10, 8, 4); // returns `undefined`
    checkWrap.isNotApproximately(10, 12, 4); // returns `undefined`
    checkWrap.isNotApproximately(10, 8, 1); // returns `10`
    checkWrap.isNotApproximately(10, 12, 1); // returns `10`
  • Checks that a value is not an array. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, readonly unknown[]>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotArray([]); // returns `undefined`
    checkWrap.isNotArray({length: 4}); // returns `{length: 4}`
  • Checks that a value is not a BigInt. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, bigint>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotBigInt(123n); // returns `undefined`
    checkWrap.isNotBigInt(123); // returns `123`
  • Checks that a value is not a boolean. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, boolean>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotBoolean(true); // returns `undefined`
    checkWrap.isNotBoolean('true'); // returns `'true'`
  • Checks that a value is not empty. Supports strings, Maps, Sets, objects, and arrays.

    Type guards the value.

    Type Parameters

    Parameters

    Returns undefined | Exclude<Actual, Empty>

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotEmpty({}); // returns `undefined`
    checkWrap.isNotEmpty(''); // returns `undefined`
    checkWrap.isNotEmpty([]); // returns `undefined`

    checkWrap.isNotEmpty('a'); // returns `'a'`
    checkWrap.isNotEmpty({a: 'a'}); // returns `{a: 'a'}`
  • Checks that a value is not a function. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, AnyFunction>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotFunction(() => {}); // returns `undefined`
    checkWrap.isNotFunction({}); // returns `{}`
  • Checks that child value is not contained within a parent object, array, or string through reference equality.

    Type guards the child when possible.

    Type Parameters

    • Parent extends string | object
    • Child

    Parameters

    Returns undefined | Exclude<Child, Values<Parent>>

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};

    checkWrap.isNotIn(child, {child}); // returns `undefined`
    checkWrap.isNotIn('a', 'ab'); // returns `undefined`
    checkWrap.isNotIn(child, [child]); // returns `undefined`

    checkWrap.isNotIn(child, {child: {a: 'a'}}); // returns `child`
    checkWrap.isNotIn('a', 'bc'); // returns `'a'`
  • Checks that a key is not contained within a parent value. Returns the key if the check passes, otherwise undefined.

    Type guards the key.

    Type Parameters

    • const Key extends PropertyKey
    • const Parent

    Parameters

    Returns undefined | Exclude<Key, RequiredKeysOf<Parent>>

    The key if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotKeyOf('a', {a: 0, b: 1}); // returns `undefined`
    checkWrap.isNotKeyOf('c', {a: 0, b: 1}); // returns `'c'`
  • Checks that a value is not exactly null. Returns the value if the check passes, otherwise undefined`.

    Type guards the value.

    @example

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotNull(null); // returns `undefined`
    checkWrap.isNotNull(undefined); // returns `undefined`

    @returns The value if the check passes. Otherwise, undefined.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, null>

  • Checks that a value is not a number. This includes NaN. Returns the value if the check passes, otherwise undefined`.

    Type guards the value.

    @example

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotNumber(123); // returns `undefined`
    checkWrap.isNotNumber(123n); // returns `123n`

    @returns The value if the check passes. Otherwise, undefined.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, number>

  • Checks that a value is not an object. This includes arrays. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, UnknownObject>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotObject({}); // returns `undefined`
    checkWrap.isNotObject([]); // returns `[]`
  • Checks 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 check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, Primitive>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotPrimitive('key'); // returns `undefined`
    checkWrap.isNotPrimitive(true); // returns `undefined`
    checkWrap.isNotPrimitive({}); // returns `{}`
  • Checks that a value is a not Promise instance. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, Promise<any>>

    import {checkWrap} from '@augment-vir/assert';

    class CustomThenable {
    constructor(public value: any) {}

    then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
    return new CustomThenable(
    onFulfilled ? onFulfilled(this.value) : this.value,
    );
    }
    }

    checkWrap.isNotPromise(Promise.resolve(5)); // returns `false`
    checkWrap.isNotPromise(new CustomThenable(5)); // returns `true`
    checkWrap.isNotPromise(5); // returns `true`
  • Checks that a value is not a PromiseLike. Returns the value if the check passes, otherwise undefined.

    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.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, PromiseLike<any>>

    import {checkWrap} from '@augment-vir/assert';

    class CustomThenable {
    constructor(public value: any) {}

    then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
    return new CustomThenable(
    onFulfilled ? onFulfilled(this.value) : this.value,
    );
    }
    }

    checkWrap.isNotPromiseLike(Promise.resolve(5)); // returns `false`
    checkWrap.isNotPromiseLike(new CustomThenable(5)); // returns `false`
    checkWrap.isNotPromiseLike(5); // returns `true`
  • Checks 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 check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, PropertyKey>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotPropertyKey('key'); // returns `undefined`
    checkWrap.isNotPropertyKey(true); // returns `true`
    checkWrap.isNotPropertyKey({}); // returns `{}`
  • Checks that a value is not a string. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, string>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotString(''); // returns `undefined`
    checkWrap.isNotString(5); // returns `5`
  • Checks that a value is not a symbol. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, symbol>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNotSymbol(Symbol('my-symbol')); // returns `undefined`
    checkWrap.checkWrap('my-symbol'); // returns `'my-symbol'`
  • Checks that a value is not a valid UUID. The nil or max UUIDs are included as not valid. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Exclude<Actual, `${string}-${string}-${string}-${string}-${string}`>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';
    import {createUuidV4} from '@augment-vir/common';

    checkWrap.isNotUuid(createUuidV4()); // returns `undefined`
    checkWrap.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // returns `undefined`
    checkWrap.isNotUuid('00000000-0000-0000-0000-000000000000'); // returns `'00000000-0000-0000-0000-000000000000'`
    checkWrap.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // returns `'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'`
    checkWrap.isNotUuid('not-a-uuid'); // returns `'not-a-uuid'`
  • Checks that a value is a number. This excludes NaN. Returns the value if the check passes, otherwise undefined`.

    Type guards the value.

    @example

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isNumber(123); // returns `123`
    checkWrap.isNumber(123n); // returns `undefined`

    @returns The value if the check passes. Otherwise, undefined.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | NarrowToActual<Actual, number>

  • Checks that a number is outside the provided min and max bounds, exclusive. Returns the number if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual extends number

    Parameters

    Returns undefined | Actual

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isOutBounds(5, {min: 1, max: 10}); // returns `undefined`
    checkWrap.isOutBounds(10, {min: 1, max: 10}); // returns `undefined`
    checkWrap.isOutBounds(11, {min: 1, max: 10}); // returns `11`
    checkWrap.isOutBounds(0, {min: 1, max: 10}); // returns `0`
  • Checks that a value is a JavaScript primitive. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Extract<Actual, Primitive>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isPrimitive('key'); // returns `'key'`
    checkWrap.isPrimitive(true); // returns `true`
    checkWrap.isPrimitive({}); // returns `undefined`
  • Checks that a value is a Promise instance. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Extract<Actual, Promise<any>>

    import {checkWrap} from '@augment-vir/assert';

    class CustomThenable {
    constructor(public value: any) {}

    then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
    return new CustomThenable(
    onFulfilled ? onFulfilled(this.value) : this.value,
    );
    }
    }

    checkWrap.isPromise(Promise.resolve(5)); // returns `true`
    checkWrap.isPromise(new CustomThenable(5)); // returns `false`
    checkWrap.isPromise(5); // returns `false`
  • Checks that a value is a PromiseLike. Returns the value if the check passes, otherwise undefined.

    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.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | Extract<Actual, PromiseLike<any>>

    import {checkWrap} from '@augment-vir/assert';

    class CustomThenable {
    constructor(public value: any) {}

    then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
    return new CustomThenable(
    onFulfilled ? onFulfilled(this.value) : this.value,
    );
    }
    }

    checkWrap.isPromiseLike(Promise.resolve(5)); // returns `true`
    checkWrap.isPromiseLike(new CustomThenable(5)); // returns `true`
    checkWrap.isPromiseLike(5); // returns `false`
  • Checks that a value is not a JavaScript primitive. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns
        | undefined
        | Extract<string, Actual>
        | Extract<number, Actual>
        | Extract<symbol, Actual>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isPrimitive('key'); // returns `undefined`
    checkWrap.isPrimitive(true); // returns `undefined`
    checkWrap.isPrimitive({}); // returns `{}`
  • Checks that a value is a symbol. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns undefined | NarrowToActual<Actual, symbol>

    The value if the check passes. Otherwise, undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isSymbol(Symbol('my-symbol')); // returns the created symbol
    checkWrap.isSymbol('my-symbol'); // returns `undefined`
  • Checks that a value is exactly true. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown

    Returns undefined | true

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isTrue(true); // returns `true`
    checkWrap.isTrue(false); // returns `false`
    checkWrap.isTrue(1); // returns `false`
    checkWrap.isTrue(0); // returns `false`
  • Checks that a value is truthy. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • T

    Parameters

    • this: void
    • input: T

    Returns undefined | Exclude<T, FalsyValue>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.isTruthy(true); // passes
    checkWrap.isTruthy(false); // fails
    checkWrap.isTruthy(1); // passes
    checkWrap.isTruthy(0); // fails
  • Checks that a value is a valid UUID. Does not accept the nil or max UUIDs. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns
        | undefined
        | NarrowToExpected<
            Actual,
            `${string}-${string}-${string}-${string}-${string}`,
        >

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';
    import {createUuidV4} from '@augment-vir/common';

    checkWrap.isUuid(createUuidV4()); // returns the generated UUID
    checkWrap.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // returns `'29e0f18e-6115-4982-8342-0afcadf5d611'`
    checkWrap.isUuid('00000000-0000-0000-0000-000000000000'); // returns `undefined`
    checkWrap.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // returns `undefined`
    checkWrap.isUuid('not-a-uuid'); // returns `undefined`
  • Checks that a parent value does not have the key. Returns the parent value if the check passes, otherwise undefined.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

    Returns undefined | Exclude<Parent, Record<Key, any>>

    The parent value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.lacksKey({a: 0, b: 1}, 'a'); // returns `undefined`
    checkWrap.lacksKey({a: 0, b: 1}, 'c'); // returns `{a: 0, b: 1}`
  • Checks that a parent value none of the keys. Returns the parent value if the check passes, otherwise undefined.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

    Returns undefined | Exclude<Parent, Partial<Record<Key, any>>>

    The parent value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.lacksKeys({a: 0, b: 1}, ['b', 'c']); // returns `undefined`
    checkWrap.lacksKeys({a: 0, b: 1}, ['c', 'd']); // returns `{a: 0, b: 1}`
  • Checks that an object/array parent does not include a child value through reference equality.

    Performs no type guarding.

    Type Parameters

    • Parent extends string | object

    Parameters

    • this: void
    • parent: Parent
    • value: unknown

    Returns undefined | Parent

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};

    checkWrap.lacksValue({child}, child); // returns `undefined`
    checkWrap.lacksValue({child: {a: 'a'}}, child); // returns `{child: {a: 'a'}}`
    checkWrap.lacksValue([child], child); // returns `undefined`
  • Checks that an object/array parent includes none of the provided child values through reference equality.

    Performs no type guarding.

    Type Parameters

    • Parent extends string | object

    Parameters

    • this: void
    • parent: Parent
    • values: unknown[]

    Returns undefined | Parent

    import {checkWrap} from '@augment-vir/assert';

    const child = {a: 'a'};
    const child2 = {b: 'b'};

    checkWrap.lacksValues({}, [child, child2]); // returns `{}`
    checkWrap.lacksValues({child, child2}, [child, child2]); // returns `undefined`
    checkWrap.lacksValues({child: {a: 'a'}, child2}, [child, child2]); // returns `undefined`
  • Checks that two values are loosely equal (using ==). Returns the first value if the check passes, otherwise undefined.

    Type guards the first value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • expected: unknown

    Returns undefined | Actual

    The first value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.looseEquals('a', 'a'); // returns `'a'`

    checkWrap.looseEquals('1', 1); // returns `'1'`

    checkWrap.looseEquals({a: 'a'}, {a: 'a'}); // returns `undefined`

    const objectExample = {a: 'a'};
    checkWrap.looseEquals(objectExample, objectExample); // returns `{a: 'a'}`
  • Checks that a string (first input, actual) matches a RegExp (second input, expected). Returns the string if the check passes, otherwise undefined.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: string
    • expected: RegExp

    Returns undefined | string

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.matches('hi', /^h/); // returns `'hi'`
    checkWrap.matches('hi', /^g/); // returns `undefined`
  • Checks that a string (first input, actual) does not match a RegExp (second input, expected). Returns the string if the check passes, otherwise undefined.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: string
    • expected: RegExp

    Returns undefined | string

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.mismatches('hi', /^h/); // returns `undefined`
    checkWrap.mismatches('hi', /^g/); // returns `'hi'`
  • Checks that two values are not deeply equal using the deep-eql package. Returns the first value if the check 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.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • expected: unknown

    Returns undefined | Actual

    The first value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.deepEquals('a', 'a'); // false

    checkWrap.deepEquals('1', 1); // true

    checkWrap.deepEquals({a: 'a'}, {a: 'a'}); // false

    const objectExample = {a: 'a'};
    checkWrap.deepEquals(objectExample, objectExample); // false
  • Checks that two objects are not deeply equal by checking only their top-level values for strict (non-deep, reference, using ===) equality. If the check passes the first object is returned. If not, undefined is returned.

    Performs no type guarding.

    Parameters

    Returns undefined | AnyObject

    The first input if the assertion passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.notEntriesEqual({a: 'a'}, {a: 'a'}); // returns `undefined`

    checkWrap.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // returns `{a: {b: 'b'}}`

    const bExample = {b: 'b'};
    checkWrap.notEntriesEqual({a: bExample}, {a: bExample}); // returns `undefined`
  • Checks that a value is not an instance of the given class constructor. Returns the value if the check passes, otherwise undefined.

    Type guards the value.

    Type Parameters

    • const Actual
    • const Instance

    Parameters

    Returns undefined | Exclude<Actual, Instance>

    The value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.notInstanceOf(/abc/, RegExp); // returns `undefined`
    checkWrap.notInstanceOf('abc', RegExp); // returns `'abc'`
  • Checks 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 check passes.

    Performs no type guarding.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • expected: unknown

    Returns undefined | Actual

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.notJsonEquals({a: 'a'}, {a: 'a'}); // false

    checkWrap.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // true
  • Checks that two values are not loosely equal (using ==). Returns the first value if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • expected: unknown

    Returns undefined | Actual

    The first value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.notLooseEquals('a', 'a'); // returns `undefined`

    checkWrap.notLooseEquals('1', 1); // returns `undefined`

    checkWrap.notLooseEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`

    const objectExample = {a: 'a'};
    checkWrap.notLooseEquals(objectExample, objectExample); // returns `undefined`
  • Checks that two values are not strictly equal (using ===). Returns the first value if the check passes, otherwise undefined.

    Performs no type guarding.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • expected: unknown

    Returns undefined | Actual

    The first value if the check passes, otherwise undefined.

    import {checkWrap} from '@augment-vir/assert';

    checkWrap.notStrictEquals('a', 'a'); // returns `undefined`

    checkWrap.notStrictEquals('1', 1); // returns `'1'`

    checkWrap.notStrictEquals({a: 'a'}, {a: 'a'}); // returns `{a: 'a'}`

    const objectExample = {a: 'a'};
    checkWrap.notStrictEquals(objectExample, objectExample); // returns `undefined`