export class DOMUtils {
static escapeHtml(str: string): string {
const escapeMap: Record = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return str.replace(/[&<>"']/g, (match) => escapeMap[match]);
}
static escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
static debounce void>(
func: T,
wait: number
): (...args: Parameters) => void {
let timeout: number;
return (...args: Parameters) => {
clearTimeout(timeout);
timeout = window.setTimeout(() => func(...args), wait);
};
}
static throttle void>(
func: T,
limit: number
): (...args: Parameters) => void {
let inThrottle: boolean;
return (...args: Parameters) => {
if (!inThrottle) {
func(...args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
}