@rr0/common
Version:
Common and utility classes
32 lines • 963 B
JavaScript
import { RR0AssertionError } from "./RR0AssertionError.js";
export class Assert {
/**
* Provide an Assert instance suited to the environment (browser or NodeJS)
*/
static async getInstance() {
if (!Assert.instance) {
Assert.instance = typeof process === "undefined" ? new BrowserAssert() : new NodeAssert(await import("node:assert"));
}
return Assert.instance;
}
}
export class BrowserAssert extends Assert {
ok(something, message = RR0AssertionError.defaultMessage) {
console.assert(something, message);
}
}
export class NodeAssert extends Assert {
constructor(assert) {
super();
this.assert = assert;
}
ok(something, message = RR0AssertionError.defaultMessage) {
try {
this.assert.ok(something, message);
}
catch (e) {
throw new RR0AssertionError(e.message);
}
}
}
//# sourceMappingURL=Assert.js.map