UNPKG

@codingheads/sticky-header

Version:

A library that allows you to create sticky headers. It uses `position: sticky` and IntersectionObserver

79 lines (67 loc) 2.43 kB
// deep compare objects export const deepEquals = (x, y) => { if (x === y) return true; // if both x and y are null or undefined and exactly the same if (!(x instanceof Object) || !(y instanceof Object)) return false; // if they are not strictly equal, they both need to be Objects if (x.constructor !== y.constructor) return false; // they must have the exact same prototype chain, the closest we can do is // test there constructor. for (let p in x) { if (!x.hasOwnProperty(p)) continue; // other properties were tested using x.constructor === y.constructor if (!y.hasOwnProperty(p)) return false; // allows to compare x[ p ] and y[ p ] when set to undefined if (x[p] === y[p]) continue; // if they have the same strict value or identity then they are equal if (typeof x[p] !== 'object') return false; // Numbers, Strings, Functions, Booleans must be strictly equal if (!deepEquals(x[p], y[p])) return false; // Objects and Arrays must be tested recursively } for (let p in y) { if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false; // allows x[ p ] to be set to undefined } return true; }; export const throttle = (func, limit = 100) => { let inThrottle; return function () { const args = arguments, context = this; if (!inThrottle) { func.apply(context, args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; }; // 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. export const debounce = (func, wait, immediate = false) => { let timeout; return function () { const context = this, args = arguments; const later = function () { timeout = null; if (!immediate) func.apply(context, args); }; const callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; }; export const isFunction = value => { // from https://stackoverflow.com/a/55785839 return ( Boolean(value) && (Object.prototype.toString.call(value) === '[object Function]' || 'function' === typeof value || value instanceof Function) ); };