UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

27 lines (26 loc) 1.36 kB
import { RequiredError } from "../error/RequiredError.js"; /** Is a given value a class constructor? */ export function isConstructor(value) { return typeof value === "function" && value.toString().startsWith("class"); } /** Is a value an instance of a class? */ export function isInstance(value, type) { return value instanceof type; } /** Assert that a value is an instance of something. */ export function assertInstance(value, type) { if (!(value instanceof type)) throw new RequiredError(`Must be instance of class "${type.name}"`, { received: value, expected: type, caller: assertInstance }); } /** Get the 'getter' function for a given property, or `undefined` if it doesn't exist. */ // biome-ignore lint/complexity/noBannedTypes: This is correct here. export function getGetter(obj, prop) { const descriptor = Object.getOwnPropertyDescriptor(obj, prop); return descriptor && typeof descriptor.get === "function" ? descriptor.get : undefined; } /** Get the 'setter' function for a given property, or `undefined` if it doesn't exist. */ // biome-ignore lint/complexity/noBannedTypes: This is correct here. export function getSetter(obj, prop) { const descriptor = Object.getOwnPropertyDescriptor(obj.constructor.prototype, prop); return descriptor && typeof descriptor.set === "function" ? descriptor.set : undefined; }