diff --git a/filenamify.js b/filenamify.js index 0399733..4d644b4 100644 --- a/filenamify.js +++ b/filenamify.js @@ -1,10 +1,9 @@ -import trimRepeated from 'trim-repeated'; import filenameReservedRegex, {windowsReservedNameRegex} from 'filename-reserved-regex'; -import stripOuter from 'strip-outer'; // 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 const reControlChars = /[\u0000-\u001F\u0080-\u009F]/g; // eslint-disable-line no-control-regex const reRelativePath = /^\.+(\\|\/)|^\.+$/; const reTrailingPeriods = /\.+$/; @@ -20,6 +19,10 @@ export default function filenamify(string, options = {}) { throw new Error('Replacement string cannot contain reserved filename characters'); } + if (replacement.length > 0) { + string = trimRepeatedReservedChars(string); + } + string = string.normalize('NFD'); string = string.replace(reRelativePath, replacement); string = string.replace(filenameReservedRegex(), replacement); @@ -29,9 +32,6 @@ export default function filenamify(string, options = {}) { 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; @@ -58,3 +58,5 @@ export default function filenamify(string, options = {}) { return string; } + +const trimRepeatedReservedChars = string => string.replace(reRepeatedReservedChars, '$1'); diff --git a/package.json b/package.json index a87e6f7..78385cf 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,7 @@ "dirname" ], "dependencies": { - "filename-reserved-regex": "^3.0.0", - "strip-outer": "^2.0.0", - "trim-repeated": "^2.0.0" + "filename-reserved-regex": "^3.0.0" }, "devDependencies": { "ava": "^3.15.0", diff --git a/test.js b/test.js index 8568525..ae25403 100644 --- a/test.js +++ b/test.js @@ -8,10 +8,10 @@ const directoryName = path.dirname(fileURLToPath(import.meta.url)); test('filenamify()', t => { t.is(filenamify('foo/bar'), 'foo!bar'); t.is(filenamify('foo//bar'), 'foo!bar'); - t.is(filenamify('//foo//bar//'), 'foo!bar'); + t.is(filenamify('//foo//bar//'), '!foo!bar!'); t.is(filenamify('foo\\\\\\bar'), 'foo!bar'); t.is(filenamify('foo/bar', {replacement: '🐴🐴'}), 'foo🐴🐴bar'); - t.is(filenamify('////foo////bar////', {replacement: '(('}), 'foo((bar'); + t.is(filenamify('////foo////bar////', {replacement: '(('}), '((foo((bar(('); t.is(filenamify('foo\u0000bar'), 'foo!bar'); t.is(filenamify('.'), '!'); t.is(filenamify('..'), '!'); @@ -28,6 +28,11 @@ test('filenamify()', t => { t.is(filenamify('c/n', {replacement: 'o'}), 'cono'); t.is(filenamify('c/n', {replacement: 'con'}), 'cconn'); t.is(filenamify('.dotfile'), '.dotfile'); + t.is(filenamify('my >>--', {replacement: '-'}), '---abc----'); + t.is(filenamify('-<-', {replacement: '-'}), '--abc--'); + t.is(filenamify('my {