Created
June 11, 2025 13:27
-
-
Save cmbaughman/328037cdeca25b2574c4e65c37caf8b6 to your computer and use it in GitHub Desktop.
Cool Javascripts to reuse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Download file | |
export function downloadFile(url, filename) { | |
const a = document.createElement("a"); | |
a.href = url; | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
// Example: downloadFile("/resume.pdf", "MyResume.pdf"); | |
// Remove dupes from an array | |
export function uniqueArray(arr) { | |
return [...new Set(arr)]; | |
} | |
// Copy to clipboard | |
export async function copyToClipboard(text) { | |
try { | |
await navigator.clipboard.writeText(text); | |
return true; | |
} catch (err) { | |
console.error("Copy failed:", err); | |
return false; | |
} | |
} | |
// Example: <button onClick={() => copyToClipboard(url)}>Copy</button> | |
// Is object empty? | |
export function isEmpty(obj) { | |
return obj && Object.keys(obj).length === 0 && obj.constructor === Object; | |
} | |
// Pretty date | |
export function formatDate(dateStr) { | |
return new Date(dateStr).toLocaleDateString("en-US", { | |
year: "numeric", | |
month: "short", | |
day: "numeric", | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment