From 69989b8664ee64cf0285329c1d9527839948233d Mon Sep 17 00:00:00 2001 From: Tomas Klaen <47283320+tomasklaen@users.noreply.github.com> Date: Wed, 27 Apr 2022 17:14:51 +0200 Subject: [PATCH] Fix overstripping of replacement character (#33) --- filenamify.js | 12 ++++++++++++ test.js | 2 ++ 2 files changed, 14 insertions(+) diff --git a/filenamify.js b/filenamify.js index 0e500c2..0399733 100644 --- a/filenamify.js +++ b/filenamify.js @@ -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; diff --git a/test.js b/test.js index f63fd87..8568525 100644 --- a/test.js +++ b/test.js @@ -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');