import * as classy from './classy'

// ┌─────┐
// │ DOM │
// └─────┘
// Handles dom nodes

// returns closest element up the DOM tree matching a given class
export function closest (className, context) {
  var result, current;
  for (current = context; current; current = current.parentNode) {
    if (current.nodeType === 1 && classy.has(current, className)) {
      result = current;
      break;
    }
  }
  return current;
}

// turn a domNodeList into an array
export function nodeListToArray (domNodeList) {
  return Array.prototype.slice.call(domNodeList);
}

// Finds all the elements inside a node, or the document and returns them as an array
export function findElements (query, domNode) {
  var context = domNode || document;
  var elements = context.querySelectorAll(query);
  return nodeListToArray(elements);
}

import * as classy from './classy'

// ┌─────┐
// │ DOM │
// └─────┘
// Handles dom nodes

// returns closest element up the DOM tree matching a given class
export function closest (className, context) {
  var result
  var current
  for (current = context; current; current = current.parentNode) {
    if (current.nodeType === 1 && classy.has(current, className)) {
      result = current
      break
    }
  }
  return current
}

// turn a domNodeList into an array
export function nodeListToArray (domNodeList) {
  if (Array.isArray(domNodeList)) {
    return domNodeList
  }
  return Array.prototype.slice.call(domNodeList)
}

// Finds all the elements inside a node, or the document and returns them as an array
export function findElements (query, domNode) {
  var context = domNode || document
  var elements = context.querySelectorAll(query)
  return nodeListToArray(elements)
}

check if domNodeList is already an array inside of dom.toArray()