Sanitize a string for use as a filename
Sanitize a string to be safe for use as a filename by removing directory paths and invalid characters.
npm install sanitize-filename
var sanitize = require"sanitize-filename";// Some string that may be unsafe or invalid as a filenamevar UNSAFE_USER_INPUT = "~/.\u0000ssh/authorized_keys";// Sanitize the string to be safe for use as a filename.var filename = sanitizeUNSAFE_USER_INPUT;// -> "~.sshauthorized_keys"
sanitize-filename removes the following:
The resulting string is truncated to 255 bytes in length. The string will not contain any directory paths and will be safe to use as a filename.
An empty string
"" can be returned. For example:
var sanitize = require"sanitize-filename";sanitize".."// -> ""
Two different inputs can return the same value. For example:
var sanitize = require"sanitize-filename";sanitize"file?"// -> "file"sanitize "*file*"// -> "file"
Sanitized filenames will be safe for use on modern Windows, OS X, and
Unix file systems (
FAT 8.3 filenames are not supported.
The test program will use various strings (including the Big List of
Naughty Strings) to create files in the working directory. Run
npm test to run tests against your file system.
inputString by removing or replacing invalid characters.
options.replacement: A string to replace invalid characters with. Optional. Default: