@codingheads/sticky-header
Version:
A library that allows you to create sticky headers. It uses `position: sticky` and IntersectionObserver
87 lines (65 loc) • 3.27 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isFunction = exports.debounce = exports.throttle = exports.deepEquals = void 0; // deep compare objects
var deepEquals = function 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 (var 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 (!exports.deepEquals(x[p], y[p])) return false; // Objects and Arrays must be tested recursively
}
for (var _p in y) {
if (y.hasOwnProperty(_p) && !x.hasOwnProperty(_p)) return false; // allows x[ p ] to be set to undefined
}
return true;
};
exports.deepEquals = deepEquals;
var throttle = function throttle(func) {
var limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
var inThrottle;
return function () {
var args = arguments,
context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(function () {
return inThrottle = false;
}, limit);
}
};
};
exports.throttle = throttle; // 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.
var debounce = function debounce(func, wait) {
var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var timeout;
return function () {
var context = this,
args = arguments;
var later = function later() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
exports.debounce = debounce;
var isFunction = function isFunction(value) {
// from https://stackoverflow.com/a/55785839
return Boolean(value) && (Object.prototype.toString.call(value) === '[object Function]' || 'function' === typeof value || value instanceof Function);
};
exports.isFunction = isFunction;
;