leftypol_lainchan/js/lcn/utils.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
2024-02-20 18:32:52 +00:00
* @file Utils for leftychan javascript.
* @author jonsmy
*/
function cont(value_to_test, fn) {
if (value_to_test != null) {
return fn(value_to_test);
} else {
return null;
}
}
2024-02-20 18:32:52 +00:00
const assert = {
"equal": (actual, expected, message="No message set") => {
if (actual !== expected) {
2024-02-27 02:58:55 +00:00
const err = new Error(`Assertion Failed. ${message}`);
2024-02-20 18:32:52 +00:00
err.data = { actual, expected}
2024-02-27 02:58:55 +00:00
//Error.captureStackTrace?.(err, assert.equal);
debugger;
throw err;
2024-02-20 18:32:52 +00:00
}
},
"ok": (actual, message="No message set") => {
if (!actual) {
2024-02-27 02:58:55 +00:00
const err = new Error(`Assertion Failed. ${message}`);
2024-02-20 18:32:52 +00:00
err.data = { actual }
2024-02-27 02:58:55 +00:00
//Error.captureStackTrace?.(err, assert.ok);
debugger;
throw err;
2024-02-20 18:32:52 +00:00
}
}
2024-02-27 02:58:55 +00:00
};
if (AbortSignal.any == null) {
AbortSignal.any = (signals) => {
const controller = new AbortController();
const abortFn = () => {
for (const signal of signals) {
signal.removeEventListener("abort", abortFn);
}
controller.abort();
}
for (const signal of signals) {
2024-02-27 02:58:55 +00:00
signal.addEventListener("abort", abortFn);
}
2024-02-27 02:58:55 +00:00
return controller.signal;
}
}
// polyfill for replaceChildren
if( Node.prototype.replaceChildren === undefined) {
Node.prototype.replaceChildren = function(addNodes) {
while(this.lastChild) {
this.removeChild(this.lastChild);
}
if (addNodes !== undefined) {
this.append(addNodes);
}
}
}
2024-02-27 20:42:36 +00:00
if (Array.prototype.at === undefined) {
Array.prototype.at = function(index) {
if (index >= 0) {
return this[index];
} else {
return this[this.length + index];
}
};
}