👔Node+ext

🔰 JSbrowserDOMtypeNode ⟩ +ext

extend Node with custom methods.

👉 custom objects

// 2023.01.12 - 21:46 - add `elem()`
// 2023.01.12 - 08:35 - first version
// ---------------------------------------------------------

// export
export { $, $all, elem };

// ⭐️ $(): select first element
function $(selector, parent = document){
  return parent.querySelector(selector);
}

// ⭐️ $all(): select all element
function $all(selector, parent = document){
  return parent.querySelectorAll(selector);
}

// ⭐ elem(): create & config element
function elem(tagName, config) {
    const elem = document.createElement(tagName);
    if (config) config(elem);
    return elem;
}

// --------------------------
// ⭐️ Node extension
// --------------------------
// 🔸 node.isTextNode
// 🔸 node.isElementNode
// 🔹 node.$()
// 🔹 node.$all()

Object.defineProperties(Node.prototype, {

    // 🔸 node.isTextNode
    isTextNode: {
        get() {
            return this.nodeType === Node.TEXT_NODE;
        },
    },

    // 🔸 node.isElementNode
    isElementNode: {
        get() {
            return this.nodeType === Node.ELEMENT_NODE;
        },
    },

    // 🔹 node.$()
    $: {
        value: function(selector){
            return $(selector, this);
        }
    },

    // 🔹 node.$all()
    $all: {
        value: function(selector){
            return $all(selector, this);
        }
    },
});

Last updated