2024-02-25 22:10:44 +00:00
|
|
|
/**
|
2024-02-20 18:32:52 +00:00
|
|
|
* @file Utils for leftychan javascript.
|
|
|
|
* @author jonsmy
|
|
|
|
*/
|
|
|
|
|
2024-02-27 19:55:30 +00:00
|
|
|
function cont(value_to_test, fn) {
|
|
|
|
if (value_to_test != null) {
|
|
|
|
return fn(value_to_test);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function text(s) {
|
|
|
|
return document.createTextNode(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepend(elem, children) {
|
|
|
|
var child = elem.firstChild;
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
elem.insertBefore(children, child);
|
|
|
|
} else {
|
|
|
|
elem.appendChild(children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _log() {
|
|
|
|
for (var arg of arguments) {
|
|
|
|
if (arg == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pre = document.createElement('pre');
|
|
|
|
pre.appendChild(text(arg.toString()));
|
|
|
|
document.body.appendChild(pre);
|
|
|
|
try {
|
|
|
|
prepend(document.body, pre);
|
|
|
|
} catch (e) {
|
|
|
|
var pre = document.createElement('pre');
|
|
|
|
pre.appendChild(text(e.toString()));
|
|
|
|
document.body.appendChild(pre);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var console = {
|
|
|
|
log: _log,
|
|
|
|
error: _log
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
}
|
2024-02-20 23:23:44 +00:00
|
|
|
|
|
|
|
for (const signal of signals) {
|
2024-02-27 02:58:55 +00:00
|
|
|
signal.addEventListener("abort", abortFn);
|
2024-02-20 23:23:44 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 02:58:55 +00:00
|
|
|
return controller.signal;
|
2024-02-20 23:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 19:55:30 +00:00
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|