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

Prevent stripping of outer replaced characters. #38

Merged
merged 3 commits into from
Apr 23, 2023

Conversation

orsett0
Copy link
Contributor

@orsett0 orsett0 commented Apr 22, 2023

Fixes #11. Just like #12, "now we trim repeat and outer reserved chars before doing a global replace with the replacement char, to preserve instances of that char present in the original string".
The only difference is that I think we should maintain as much of the original string as possible, so we don't strip outer reserved characters, we only replace them with the replacement string.

@sindresorhus
Copy link
Owner

#12 (comment)

filenamify.js Outdated

// Doesn't make sense to have longer filenames
const MAX_FILENAME_LENGTH = 100;

const reRepeatedReservedChars = /([<>:"/\\|?*\u0000-\u001F]){2,}/g; // eslint-disable-line no-control-regex
Copy link
Owner

Choose a reason for hiding this comment

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

You did not fully address the feedback in #12 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't move it because i saw that reControlChars is also a g regex, and it's outside filenamify(). Should I move that too?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes

filenamify.js Outdated
@@ -58,3 +58,5 @@ export default function filenamify(string, options = {}) {

return string;
}

const trimRepeatedReservedChars = string => string.replace(reRepeatedReservedChars, '$1');
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const trimRepeatedReservedChars = string => string.replace(reRepeatedReservedChars, '$1');
const trimRepeatedReservedCharacters = string => string.replace(reRepeatedReservedCharacters, '$1');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking about completely removing const trimRepeatedReservedChars, given that after moving the regex inside filenamify(), calling string.replace(...) inside the if statement would be the easiest way to do it.

@sindresorhus sindresorhus merged commit 7d4846f into sindresorhus:main Apr 23, 2023
1 check passed
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.

Replacement option removes processed string last character
2 participants