$j returns an instance of a DOMNodeCollection. If a string is passed as an argument, $j will return a new DOMNodeCollection containing all of the HTML elements on the page that match the argument passed to it. If the argument is a function, or there are multiple functions, these will be stored in a queue, and executed consecutively once the document has fully loaded.
const DOMNodeCollection = require('./dom_node_collection');
window.$j = $j;
const queue = [];
document.addEventListener("DOMContentLoaded", () => {
console.log('loaded!');
queue.forEach((func) => {
func();
});
});
function $j(selector) {
let nodeList;
if (selector instanceof Function) {
if (document.readyState === 'complete') {
selector();
} else {
queue.push(selector);
}
} else if (selector instanceof HTMLElement) {
return new DOMNodeCollection([selector]);
} else {
nodeList = document.querySelectorAll(selector);
return new DOMNodeCollection(Array.from(nodeList));
}
}