Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filenames truncated by maxLength not keeping their extension #27

Merged

Conversation

aakriti-kedia
Copy link
Contributor

@aakriti-kedia aakriti-kedia commented Oct 16, 2021

Fixes #13

Excludes the extension, truncates the string to the required length, appends the extension.

Example -
Input : very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt

Output
very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_.txt

filenamify.js Outdated
@@ -31,7 +31,11 @@ export default function filenamify(string, options = {}) {
}

string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string;
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH);
const allowedLength = typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH;
if (allowedLength < string.length) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it reads better if string.length comes first in this conditional.

@sindresorhus
Copy link
Owner

Any reason you didn't go with my suggestion in #13 (comment) ?

@aakriti-kedia
Copy link
Contributor Author

aakriti-kedia commented Oct 16, 2021

Any reason you didn't go with my suggestion in #13 (comment) ?

Hi @sindresorhus
My thought process was that if this code is part of filenamify instead of filenamify-path, all the processing related to filename would be dealt in filenamify and abstracted from filenamify-path. As per the logic - If the filename is long, we wish to truncate the filename and not its path - as this is an operation on the filename, it would make sense if it resides in filenamify.

Please feel free to let me know if you have any concerns with extending this approach to filenamify and not keeping it restricted to filenamify-path.

@sindresorhus sindresorhus changed the title filename extension preserved for long filenames Fix filenames truncated by maxLength not keeping their extension Oct 17, 2021
@sindresorhus sindresorhus merged commit 8c52cb1 into sindresorhus:main Oct 17, 2021
@papb
Copy link

papb commented Jan 19, 2022

Maybe this could be updated to also account for a few specific "double" extensions such as .tar.gz and .tar.bz2?

@sindresorhus
Copy link
Owner

@papb I don't think it makes sense to handle such extension automatically. That would be surprising behavior. Maybe there could be an option to define a set of known double extensions or something. It's not something I need, so if you need it, open an issue with a proper proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File names truncated by maxLength should keep their extension
3 participants