Skip to content

Commit

Permalink
Remove trailing periods for consistency on Windows (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx authored Apr 26, 2021
1 parent 5aefeb5 commit 34da1ce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions filenamify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MAX_FILENAME_LENGTH = 100;

const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex
const reRelativePath = /^\.+/;
const reTrailingPeriods = /\.+$/;

const filenamify = (string, options = {}) => {
if (typeof string !== 'string') {
Expand All @@ -23,6 +24,7 @@ const filenamify = (string, options = {}) => {
string = string.replace(filenameReservedRegex(), replacement);
string = string.replace(reControlChars, replacement);
string = string.replace(reRelativePath, replacement);
string = string.replace(reTrailingPeriods, '');

if (replacement.length > 0) {
string = trimRepeated(string, replacement);
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Convert a string to a valid safe filename
On Unix-like systems `/` is reserved and [`<>:"/\|?*`](http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29#naming_conventions) on Windows.
On Unix-like systems, `/` is reserved. On Windows, [`<>:"/\|?*`](http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29#naming_conventions) along with trailing periods are reserved.

## Install

Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ test('filnamify()', t => {
t.is(filenamify('..'), '!');
t.is(filenamify('./'), '!');
t.is(filenamify('../'), '!');
t.is(filenamify('foo.bar.'), 'foo.bar');
t.is(filenamify('foo.bar..'), 'foo.bar');
t.is(filenamify('foo.bar...'), 'foo.bar');
t.is(filenamify('con'), 'con!');
t.is(filenamify('foo/bar/nul'), 'foo!bar!nul');
t.is(filenamify('con', {replacement: '🐴🐴'}), 'con🐴🐴');
Expand Down

0 comments on commit 34da1ce

Please sign in to comment.