mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
47c38498eb
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
import { AssertionError } from "./assertion_error.ts";
|
|
|
|
/** Any constructor */
|
|
// deno-lint-ignore no-explicit-any
|
|
export type AnyConstructor = new (...args: any[]) => any;
|
|
/** Gets constructor type */
|
|
export type GetConstructorType<T extends AnyConstructor> = T extends // deno-lint-ignore no-explicit-any
|
|
new (...args: any) => infer C ? C
|
|
: never;
|
|
|
|
/**
|
|
* Make an assertion that `obj` is an instance of `type`.
|
|
* If not then throw.
|
|
*
|
|
* @example Usage
|
|
* ```ts no-eval
|
|
* import { assertInstanceOf } from "@std/assert/assert-instance-of";
|
|
*
|
|
* assertInstanceOf(new Date(), Date); // Doesn't throw
|
|
* assertInstanceOf(new Date(), Number); // Throws
|
|
* ```
|
|
*
|
|
* @typeParam T The expected type of the object.
|
|
* @param actual The object to check.
|
|
* @param expectedType The expected class constructor.
|
|
* @param msg The optional message to display if the assertion fails.
|
|
*/
|
|
export function assertInstanceOf<T extends AnyConstructor>(
|
|
actual: unknown,
|
|
expectedType: T,
|
|
msg = "",
|
|
): asserts actual is GetConstructorType<T> {
|
|
if (actual instanceof expectedType) return;
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
const expectedTypeStr = expectedType.name;
|
|
|
|
let actualTypeStr = "";
|
|
if (actual === null) {
|
|
actualTypeStr = "null";
|
|
} else if (actual === undefined) {
|
|
actualTypeStr = "undefined";
|
|
} else if (typeof actual === "object") {
|
|
actualTypeStr = actual.constructor?.name ?? "Object";
|
|
} else {
|
|
actualTypeStr = typeof actual;
|
|
}
|
|
|
|
if (expectedTypeStr === actualTypeStr) {
|
|
msg =
|
|
`Expected object to be an instance of "${expectedTypeStr}"${msgSuffix}`;
|
|
} else if (actualTypeStr === "function") {
|
|
msg =
|
|
`Expected object to be an instance of "${expectedTypeStr}" but was not an instanced object${msgSuffix}`;
|
|
} else {
|
|
msg =
|
|
`Expected object to be an instance of "${expectedTypeStr}" but was "${actualTypeStr}"${msgSuffix}`;
|
|
}
|
|
|
|
throw new AssertionError(msg);
|
|
}
|