Skip to content

Instantly share code, notes, and snippets.

@cmbaughman
Created June 11, 2025 13:27
Show Gist options
  • Save cmbaughman/328037cdeca25b2574c4e65c37caf8b6 to your computer and use it in GitHub Desktop.
Save cmbaughman/328037cdeca25b2574c4e65c37caf8b6 to your computer and use it in GitHub Desktop.
Cool Javascripts to reuse
// 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