Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for dotfiles #32

Merged
merged 1 commit into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});