all files / src/utils/ classList.js

38.1% Statements 16/42
20% Branches 4/20
9.09% Functions 1/11
29.73% Lines 11/37
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84          1× 1×   1×       1×               1×           1×                   1×                               1×                 1×           1×                                
/**
 * Adapted from dominus v6.0.1
 * https://github.com/bevacqua/dominus/blob/master/src/classes.js
 */
 
var trim = /^\s+|\s+$/g
var whitespace = /\s+/g
 
function interpret (input) {
  return typeof input === 'string' ? input.replace(trim, '').split(whitespace) : input
}
 
function classes (el) {
  if (isElement(el)) {
    return el.className.replace(trim, '').split(whitespace)
  }
 
  return []
}
 
function set (el, input) {
  if (isElement(el)) {
    el.className = interpret(input).join(' ')
  }
}
 
function add (el, input) {
  var current = remove(el, input)
  var values = interpret(input)
 
  current.push.apply(current, values)
  set(el, current)
 
  return current
}
 
function remove (el, input) {
  var current = classes(el)
  var values = interpret(input)
 
  values.forEach(function (value) {
    var i = current.indexOf(value)
    if (i !== -1) {
      current.splice(i, 1)
    }
  })
 
  set(el, current)
 
  return current
}
 
function contains (el, input) {
  var current = classes(el)
  var values = interpret(input)
 
  return values.every(function (value) {
    return current.indexOf(value) !== -1
  })
}
 
function isElement (o) {
  var elementObjects = typeof window.HTMLElement === 'object'
 
  return elementObjects ? o instanceof window.HTMLElement : isElementObject(o)
}
 
function isElementObject (o) {
  return o &&
    typeof o === 'object' &&
    typeof o.nodeName === 'string' &&
    o.nodeType === 1
}
 
export default {
  add,
  remove,
  contains,
  has: contains,
  set,
  get: classes
}