# All files / src/utils events.js

44.83% Statements 13/29
33.33% Branches 6/18
40% Functions 2/5
47.83% Lines 11/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    6917x
    17x
    17x
    17x

    17x
    364x
    364x
    45x

    17x

    17x

    180x

     var bind = window.addEventListener ? 'addEventListener' : 'attachEvent',
      unbind = window.removeEventListener ? 'removeEventListener' : 'detachEvent',
      prefix = bind !== 'addEventListener' ? 'on' : '',
      toArray = require('./to-array')

    /**
     * Bind `el` event `type` to `fn`.
     *
     * @param {Element} el, NodeList, HTMLCollection or Array
     * @param {String} type
     * @param {Function} fn
     * @param {Boolean} capture
     * @api public
     */

    exports.bind = function (el, type, fn, capture) {
      el = toArray(el)
      for (var i = 0, il = el.length; i < il; i++) {
        el[i][bind](prefix + type, fn, capture || false)
      }
    }

    /**
     * Unbind `el` event `type`'s callback `fn`.
     *
     * @param {Element} el, NodeList, HTMLCollection or Array
     * @param {String} type
     * @param {Function} fn
     * @param {Boolean} capture
     * @api public
     */

    exports.unbind = function (el, type, fn, capture) {
      el = toArray(el)
      for (var i = 0, il = el.length; i < il; i++) {
        el[i][unbind](prefix + type, fn, capture || false)
      }
    }

    /**
     * Returns a function, that, as long as it continues to be invoked, will not
     * be triggered. The function will be called after it stops being called for
     * `wait` milliseconds. If `immediate` is true, trigger the function on the
     * leading edge, instead of the trailing.
     *
     * @param {Function} fn
     * @param {Integer} wait
     * @param {Boolean} immediate
     * @api public
     */

    exports.debounce = function (fn, wait, immediate) {
      var timeout
      return wait
        ? function () {
            var context = this,
              args = arguments
            var later = function () {
              timeout = null
              if (!immediate) fn.apply(context, args)
            }
            var callNow = immediate && !timeout
            clearTimeout(timeout)
            timeout = setTimeout(later, wait)
            if (callNow) fn.apply(context, args)
          }
        : fn
    }