Skip to content

Commit

Permalink
Account for dotfiles (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklaen committed Apr 26, 2022
1 parent a16c224 commit 6badd76
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions filenamify.js
Expand Up @@ -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 = {}) {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion test.js
Expand Up @@ -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');
Expand All @@ -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 => {
Expand All @@ -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');
});

0 comments on commit 6badd76

Please sign in to comment.