UNPKG

@rxxuzi/gumi

Version:

Clean & minimal design system with delightful interactions

186 lines 4.97 kB
// utils/helpers.ts // Gumi.js v1.0.0 - Helper Utilities /** * Debounce function */ export function debounce(func, wait) { let timeout = null; return function executedFunction(...args) { const later = () => { timeout = null; func(...args); }; if (timeout) clearTimeout(timeout); timeout = setTimeout(later, wait); }; } /** * Throttle function */ export function throttle(func, limit) { let inThrottle = false; return function executedFunction(...args) { if (!inThrottle) { func(...args); inThrottle = true; setTimeout(() => { inThrottle = false; }, limit); } }; } /** * Deep merge objects */ export function deepMerge(target, ...sources) { if (!sources.length) return target; const source = sources.shift(); if (isObject(target) && isObject(source)) { for (const key in source) { if (isObject(source[key])) { if (!target[key]) Object.assign(target, { [key]: {} }); deepMerge(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } } } return deepMerge(target, ...sources); } /** * Check if value is object */ export function isObject(item) { return item && typeof item === 'object' && !Array.isArray(item); } /** * Generate unique ID */ export function generateId(prefix = 'apple') { return `${prefix}-${Math.random().toString(36).substr(2, 9)}`; } /** * Format number with commas */ export function formatNumber(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } /** * Clamp number between min and max */ export function clamp(num, min, max) { return Math.max(min, Math.min(max, num)); } /** * Get cookie value */ export function getCookie(name) { var _a; const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) { return ((_a = parts.pop()) === null || _a === void 0 ? void 0 : _a.split(';').shift()) || null; } return null; } /** * Set cookie */ export function setCookie(name, value, days = 365) { const expires = new Date(); expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000); document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`; } /** * Remove cookie */ export function removeCookie(name) { document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/`; } /** * Copy text to clipboard */ export async function copyToClipboard(text) { try { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); return true; } else { // Fallback for older browsers const textArea = document.createElement('textarea'); textArea.value = text; textArea.style.position = 'fixed'; textArea.style.left = '-999999px'; textArea.style.top = '-999999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); const successful = document.execCommand('copy'); textArea.remove(); return successful; } } catch (err) { console.error('Failed to copy text:', err); return false; } } /** * Parse JSON safely */ export function parseJSON(json, fallback) { try { return JSON.parse(json); } catch (_a) { return fallback; } } /** * Format date */ export function formatDate(date, format = 'YYYY-MM-DD') { const d = new Date(date); const year = d.getFullYear(); const month = String(d.getMonth() + 1).padStart(2, '0'); const day = String(d.getDate()).padStart(2, '0'); const hours = String(d.getHours()).padStart(2, '0'); const minutes = String(d.getMinutes()).padStart(2, '0'); const seconds = String(d.getSeconds()).padStart(2, '0'); return format .replace('YYYY', String(year)) .replace('MM', month) .replace('DD', day) .replace('HH', hours) .replace('mm', minutes) .replace('ss', seconds); } /** * Get query string parameter */ export function getQueryParam(name) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(name); } /** * Set query string parameter */ export function setQueryParam(name, value) { const url = new URL(window.location.href); url.searchParams.set(name, value); window.history.pushState({}, '', url); } /** * Remove query string parameter */ export function removeQueryParam(name) { const url = new URL(window.location.href); url.searchParams.delete(name); window.history.pushState({}, '', url); } //# sourceMappingURL=helpers.js.map