tickwerk/src/utils.ts

21 lines
822 B
TypeScript

// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
const defaultMessage =
'There has been an unexpected error in the Tickwerk module. For more details, please take a look at the console (F12).';
/**
* Tests if the given `value` is truthy.
*
* If it is not truthy, an {@link Error} is thrown, which depends on the given `message` parameter:
* - If `message` is a string`, it is used to construct a new {@link Error} which then is thrown.
* - If `message` is an instance of {@link Error}, it is thrown.
* - If `message` is `undefined`, an {@link Error} with a default message is thrown.
*/
export const enforce = (value: unknown, message: string | Error = defaultMessage): asserts value => {
if (!value) {
throw message instanceof Error ? message : new Error(message);
}
};