-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities Module.code.javascript
More file actions
53 lines (46 loc) · 1.43 KB
/
Utilities Module.code.javascript
File metadata and controls
53 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// js/modules/utils.js
export const copyToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text);
showToast('Copied to clipboard!', 'success');
} catch (err) {
showToast('Failed to copy text', 'error');
}
};
export const showToast = (message, type = 'success') => {
const toastContainer = document.getElementById('toast-container') || createToastContainer();
const toast = document.createElement('div');
toast.className = `toast toast-${type} animate-fadeUp`;
toast.innerHTML = `
<i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-circle'}"></i>
<span>${message}</span>
`;
toastContainer.appendChild(toast);
setTimeout(() => {
toast.classList.add('toast-hide');
setTimeout(() => toast.remove(), 300);
}, 3000);
};
const createToastContainer = () => {
const container = document.createElement('div');
container.id = 'toast-container';
document.body.appendChild(container);
return container;
};
export const formatNumber = (number, decimals = 2) => {
return new Intl.NumberFormat('en-US', {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
}).format(number);
};
export const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};