refactor: cleanup assert() & AssertionError definitions (#13859)

This commit is contained in:
Leo Kettmeir 2022-03-19 13:57:37 +01:00 committed by GitHub
parent ad8e238348
commit e55dee7fd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 60 deletions

View file

@ -11,6 +11,7 @@
((window) => {
const core = Deno.core;
const {
Error,
RegExp,
ArrayPrototypeMap,
StringPrototypeCharCodeAt,
@ -275,6 +276,24 @@
return StringPrototypeMatch(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? "";
}
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
/**
* @param {unknown} cond
* @param {string=} msg
* @returns {asserts cond}
*/
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
window.__bootstrap.infra = {
collectSequenceOfCodepoints,
ASCII_DIGIT,
@ -299,5 +318,7 @@
collectHttpQuotedString,
forgivingBase64Encode,
forgivingBase64Decode,
AssertionError,
assert,
};
})(globalThis);