Skip to content

Instantly share code, notes, and snippets.

@schalkneethling
Created April 4, 2025 13:32
Show Gist options
  • Save schalkneethling/0c7dcf6f074da178aa63ac16545946f6 to your computer and use it in GitHub Desktop.
Save schalkneethling/0c7dcf6f074da178aa63ac16545946f6 to your computer and use it in GitHub Desktop.
Truncate a filename with JS
function getFilename(filename, truncateAt = 15) {
if (filename.length <= truncateAt) {
return filename;
}
const extension = filename.slice(filename.lastIndexOf("."));
const shortenedFilename = filename.slice(0, truncateAt);
return `${shortenedFilename}..${extension}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment