All files / src/utils index.js

47.37% Statements 9/19
12.5% Branches 1/8
50% Functions 2/4
47.37% Lines 9/19
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    3x       3x 16x 16x                                 3x 21x 21x       3x 3x            
/*
 * 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
 * N milliseconds. If `immediate` is passed, trigger the function on the
 * leading edge, instead of the trailing.
*/
const debounce = (func, wait, immediate) => {
  let timeout;
  return () => {
    const context = this;
    const later = () => {
      timeout = null;
      if (!immediate) {
        func.apply(context);
      }
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) {
      func.apply(context);
    }
  };
};
 
/**
 *E Run a function only if window exists
 */
const runIfBrowser = (func) => {
  if (typeof window !== 'undefined') {
    func();
  }
};
 
export {
  debounce,
  runIfBrowser,
};