Assert.notEmpty()

Syntax

function notEmpty(iterable: Iterable<unknown>, name?: string): void

Asserts that an array-like or iterable isn’t empty and throws an assertion error if it is.

Parameters
iterable

An iterable which may or may not be empty.

name Optional

A variable, property, or parameter name which, if specified, will print the name as part of the assertion error to make it more clear where the error originated.

Example

import { Assert } from 'potence';

function getFirst(array: number[]): number {
    Assert.notEmpty(array);

    return array[0];
}

getFirst([1]);  // -> 1
getFirst([]);   // AssertionError: "Assertion failed: expected iterable not to be empty but had 0 elements"