Added debounce, throttle, getUrlParams
Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
parent
6fdace07c8
commit
3d885ec262
1 changed files with 51 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
/* eslint-disable max-classes-per-file */
|
||||
export function bytesToSize(bytes) {
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
if (bytes === 0) return 'n/a';
|
||||
|
@ -33,3 +34,53 @@ export function abbreviateNumber(number) {
|
|||
if (number > 99) return '99+';
|
||||
return number;
|
||||
}
|
||||
|
||||
export class Debounce {
|
||||
constructor() {
|
||||
this.timeoutId = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function} func - callback function
|
||||
* @param {number} wait - wait in milliseconds to call func
|
||||
* @returns {func} debounceCallback - to pass arguments to func callback
|
||||
*/
|
||||
_(func, wait) {
|
||||
const that = this;
|
||||
return function debounceCallback(...args) {
|
||||
clearTimeout(that.timeoutId);
|
||||
that.timeoutId = setTimeout(() => {
|
||||
func.apply(this, args);
|
||||
that.timeoutId = null;
|
||||
}, wait);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class Throttle {
|
||||
constructor() {
|
||||
this.timeoutId = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function} func - callback function
|
||||
* @param {number} wait - wait in milliseconds to call func
|
||||
* @returns {function} throttleCallback - to pass arguments to func callback
|
||||
*/
|
||||
_(func, wait) {
|
||||
const that = this;
|
||||
return function throttleCallback(...args) {
|
||||
if (that.timeoutId !== null) return;
|
||||
that.timeoutId = setTimeout(() => {
|
||||
func.apply(this, args);
|
||||
that.timeoutId = null;
|
||||
}, wait);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function getUrlPrams(paramName) {
|
||||
const queryString = window.location.search;
|
||||
const urlParams = new URLSearchParams(queryString);
|
||||
return urlParams.get(paramName);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue