Skip to content

Commit

Permalink
Fix overstripping of replacement character (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklaen committed Apr 27, 2022
1 parent 6badd76 commit 69989b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions filenamify.js
Expand Up @@ -27,8 +27,20 @@ export default function filenamify(string, options = {}) {
string = string.replace(reTrailingPeriods, '');

if (replacement.length > 0) {
const startedWithDot = string[0] === '.';

string = trimRepeated(string, replacement);
string = string.length > 1 ? stripOuter(string, replacement) : string;

// We removed the whole filename
if (!startedWithDot && string[0] === '.') {
string = replacement + string;
}

// We removed the whole extension
if (string[string.length - 1] === '.') {
string += replacement;
}
}

string = windowsReservedNameRegex().test(string) ? string + replacement : string;
Expand Down
2 changes: 2 additions & 0 deletions test.js
Expand Up @@ -17,6 +17,8 @@ test('filenamify()', t => {
t.is(filenamify('..'), '!');
t.is(filenamify('./'), '!');
t.is(filenamify('../'), '!');
t.is(filenamify('!.foo'), '!.foo');
t.is(filenamify('foo.!'), 'foo.!');
t.is(filenamify('foo.bar.'), 'foo.bar');
t.is(filenamify('foo.bar..'), 'foo.bar');
t.is(filenamify('foo.bar...'), 'foo.bar');
Expand Down

0 comments on commit 69989b8

Please sign in to comment.