Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
4e8e737
commit d8795d3
Showing
8 changed files
with
125 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import filenamify = require('./filenamify'); | ||
|
||
/** | ||
Convert the filename in a path a valid filename and return the augmented path. | ||
*/ | ||
declare const filenamifyPath: (path: string, options?: filenamify.Options) => string; | ||
|
||
export = filenamifyPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const filenamify = require('./filenamify'); | ||
|
||
const filenamifyPath = (filePath, options) => { | ||
filePath = path.resolve(filePath); | ||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options)); | ||
}; | ||
|
||
module.exports = filenamifyPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
declare namespace filenamify { | ||
interface Options { | ||
/** | ||
String to use as replacement for reserved filename characters. | ||
Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*` | ||
@default '!' | ||
*/ | ||
readonly replacement?: string; | ||
|
||
/** | ||
Truncate the filename to the given length. | ||
Systems generally allow up to 255 characters, but we default to 100 for usability reasons. | ||
@default 100 | ||
*/ | ||
readonly maxLength?: number; | ||
} | ||
} | ||
|
||
/** | ||
Convert a string to a valid filename. | ||
@example | ||
``` | ||
import filenamify = require('filenamify'); | ||
filenamify('<foo/bar>'); | ||
//=> 'foo!bar' | ||
filenamify('foo:"bar"', {replacement: '🐴'}); | ||
//=> 'foo🐴bar' | ||
``` | ||
*/ | ||
declare const filenamify: (string: string, options?: filenamify.Options) => string; | ||
|
||
export = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const trimRepeated = require('trim-repeated'); | ||
const filenameReservedRegex = require('filename-reserved-regex'); | ||
const stripOuter = require('strip-outer'); | ||
|
||
// Doesn't make sense to have longer filenames | ||
const MAX_FILENAME_LENGTH = 100; | ||
|
||
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex | ||
const reRelativePath = /^\.+/; | ||
|
||
const filenamify = (string, options = {}) => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
|
||
const replacement = options.replacement === undefined ? '!' : options.replacement; | ||
|
||
if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) { | ||
throw new Error('Replacement string cannot contain reserved filename characters'); | ||
} | ||
|
||
string = string.replace(filenameReservedRegex(), replacement); | ||
string = string.replace(reControlChars, replacement); | ||
string = string.replace(reRelativePath, replacement); | ||
|
||
if (replacement.length > 0) { | ||
string = trimRepeated(string, replacement); | ||
string = string.length > 1 ? stripOuter(string, replacement) : string; | ||
} | ||
|
||
string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string; | ||
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH); | ||
|
||
return string; | ||
}; | ||
|
||
module.exports = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,8 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const trimRepeated = require('trim-repeated'); | ||
const filenameReservedRegex = require('filename-reserved-regex'); | ||
const stripOuter = require('strip-outer'); | ||
const filenamify = require('./filenamify'); | ||
const filenamifyPath = require('./filenamify-path'); | ||
|
||
// Doesn't make sense to have longer filenames | ||
const MAX_FILENAME_LENGTH = 100; | ||
|
||
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; // eslint-disable-line no-control-regex | ||
const reRelativePath = /^\.+/; | ||
|
||
const filenamify = (string, options = {}) => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
|
||
const replacement = options.replacement === undefined ? '!' : options.replacement; | ||
|
||
if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) { | ||
throw new Error('Replacement string cannot contain reserved filename characters'); | ||
} | ||
|
||
string = string.replace(filenameReservedRegex(), replacement); | ||
string = string.replace(reControlChars, replacement); | ||
string = string.replace(reRelativePath, replacement); | ||
|
||
if (replacement.length > 0) { | ||
string = trimRepeated(string, replacement); | ||
string = string.length > 1 ? stripOuter(string, replacement) : string; | ||
} | ||
|
||
string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string; | ||
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH); | ||
|
||
return string; | ||
}; | ||
|
||
filenamify.path = (filePath, options) => { | ||
filePath = path.resolve(filePath); | ||
return path.join(path.dirname(filePath), filenamify(path.basename(filePath), options)); | ||
}; | ||
const filenamifyCombined = filenamify; | ||
filenamifyCombined.path = filenamifyPath; | ||
|
||
module.exports = filenamify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters