UNPKG

@ooopenlab/quiz-shared

Version:

Shared utilities and components for SuperQuiz modules

90 lines (83 loc) 2.2 kB
/** * Utility functions for quiz operations */ /** * Generate a unique ID for quiz elements */ export function generateId(prefix: string = 'quiz'): string { return `${prefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } /** * Validate email format */ export function isValidEmail(email: string): boolean { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } /** * Validate URL format */ export function isValidUrl(url: string): boolean { try { new URL(url); return true; } catch { return false; } } /** * Sanitize HTML content */ export function sanitizeHtml(html: string): string { return html .replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '') .replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi, '') .replace(/javascript:/gi, ''); } /** * Deep clone an object */ export function deepClone<T>(obj: T): T { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Date) return new Date(obj.getTime()) as unknown as T; if (obj instanceof Array) return obj.map(item => deepClone(item)) as unknown as T; if (typeof obj === 'object') { const clonedObj = {} as { [key: string]: any }; for (const key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = deepClone(obj[key]); } } return clonedObj as T; } return obj; } /** * Debounce function calls */ export function debounce<T extends (...args: any[]) => any>( func: T, wait: number ): (...args: Parameters<T>) => void { let timeout: ReturnType<typeof setTimeout>; return (...args: Parameters<T>) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; } /** * Throttle function calls */ export function throttle<T extends (...args: any[]) => any>( func: T, limit: number ): (...args: Parameters<T>) => void { let inThrottle: boolean; return (...args: Parameters<T>) => { if (!inThrottle) { func(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; }