JSFiddle - React, Tailwind, and code Playground

by shapeshifta

HTML

<div class="test"></div>
<div class="test"></div>

JavaScript

class utils {
  static $(element) {
    let nodes = null;
    if (element && typeof element === 'string') {
      nodes = Array.prototype.slice.call(document.querySelectorAll(element));
      if (nodes.length === 1) {
        return nodes[0];
      } else {
        return nodes;
      }
    }
    return element;
  }

  static $$(sel, parent) {
    if (sel && typeof sel === 'string') {
      sel = (parent || document).querySelectorAll(sel);
    }
    return Array.prototype.slice.call(sel);
  }

  static hasClass(element, cls) {
    if (!element.length) {
      return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    } else {
      throw "Element must be a single node for now, not an array of nodes.";
    }
  }
}

console.log(utils.hasClass(utils.$('div'), 'test'))