2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
|
|
|
|
2023-12-06 17:13:38 +00:00
|
|
|
/**
|
|
|
|
* Make an assertion, error will be thrown if `expr` does not have truthy value.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { assert } from "https://deno.land/std@$STD_VERSION/assert/assert.ts";
|
|
|
|
*
|
|
|
|
* assert("hello".includes("ello")); // Doesn't throw
|
|
|
|
* assert("hello".includes("world")); // Throws
|
|
|
|
* ```
|
|
|
|
*/
|
2023-07-13 07:04:30 +00:00
|
|
|
export function assert(expr: unknown, msg = ""): asserts expr {
|
|
|
|
if (!expr) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|