From 05db540b41ae39d35b1483de716629db1afcced2 Mon Sep 17 00:00:00 2001 From: tomasklaen Date: Mon, 25 Apr 2022 21:04:44 +0200 Subject: [PATCH] Account for dotfiles --- filenamify.js | 4 ++-- test.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/filenamify.js b/filenamify.js index 6d810b4..0e500c2 100644 --- a/filenamify.js +++ b/filenamify.js @@ -6,7 +6,7 @@ import stripOuter from 'strip-outer'; const MAX_FILENAME_LENGTH = 100; const reControlChars = /[\u0000-\u001F\u0080-\u009F]/g; // eslint-disable-line no-control-regex -const reRelativePath = /^\.+/; +const reRelativePath = /^\.+(\\|\/)|^\.+$/; const reTrailingPeriods = /\.+$/; export default function filenamify(string, options = {}) { @@ -21,9 +21,9 @@ export default function filenamify(string, options = {}) { } string = string.normalize('NFD'); + string = string.replace(reRelativePath, replacement); string = string.replace(filenameReservedRegex(), replacement); string = string.replace(reControlChars, replacement); - string = string.replace(reRelativePath, replacement); string = string.replace(reTrailingPeriods, ''); if (replacement.length > 0) { diff --git a/test.js b/test.js index 0072651..f63fd87 100644 --- a/test.js +++ b/test.js @@ -5,7 +5,7 @@ import filenamify, {filenamifyPath} from './index.js'; const directoryName = path.dirname(fileURLToPath(import.meta.url)); -test('filnamify()', t => { +test('filenamify()', t => { t.is(filenamify('foo/bar'), 'foo!bar'); t.is(filenamify('foo//bar'), 'foo!bar'); t.is(filenamify('//foo//bar//'), 'foo!bar'); @@ -25,6 +25,7 @@ test('filnamify()', t => { t.is(filenamify('con', {replacement: '🐴🐴'}), 'con🐴🐴'); t.is(filenamify('c/n', {replacement: 'o'}), 'cono'); t.is(filenamify('c/n', {replacement: 'con'}), 'cconn'); + t.is(filenamify('.dotfile'), '.dotfile'); }); test('filenamifyPath()', t => { @@ -46,4 +47,5 @@ test('filenamify length', t => { const filenameNoExt = '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'; t.is(filenamify(filenameNoExt), 'very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_'); t.is(filenamify(filenameNoExt, {maxLength: 20}), 'very_very_very_very_'); + t.is(filenamify('.asdfghjkl', {maxLength: 2}), '.asdfghjkl'); });