filestash/public/assets/lib/assert.js
2024-12-04 16:05:28 +11:00

33 lines
885 B
JavaScript

export default class assert {
/**
* @param {*} object
* @param {Function} type
* @param {string} [msg]
* @return {*}
* @throws {TypeError}
*/
static type(object, type, msg) {
if (object === undefined) throw new TypeError(msg || "assertion failed - undefined object");
if (!(object instanceof type)) throw new TypeError(msg || `assertion failed - unexpected type for ${JSON.stringify(object)}`);
return object;
}
/**
* @param {*} object
* @param {string} [msg]
* @return {*}
* @throws {TypeError}
*/
static truthy(object, msg) {
if (!object) throw new TypeError(msg || `assertion failed - object is not truthy`);
return object;
}
/**
* @param {string} msg
* @throws {TypeError}
*/
static fail(msg) {
throw new TypeError(msg);
}
}