chuwanghui 6d115466a5 electron库 | 6 years ago | |
---|---|---|
.. | ||
.gitmodules | 6 years ago | |
.npmignore | 6 years ago | |
.travis.yml | 6 years ago | |
.zuul.yml | 6 years ago | |
AUTHORS | 6 years ago | |
Changelog.md | 6 years ago | |
LICENSE.md | 6 years ago | |
README.md | 6 years ago | |
index.js | 6 years ago | |
package.json | 6 years ago | |
test.js | 6 years ago |
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 filename
var UNSAFE_USER_INPUT = "~/.\u0000ssh/authorized_keys";
// Sanitize the string to be safe for use as a filename.
var filename = sanitize(UNSAFE_USER_INPUT);
// -> "~.sshauthorized_keys"
sanitize-filename removes the following:
0x00
–0x1f
and 0x80
–0x9f
)/
, ?
, <
, >
, \
, :
, *
, |
, and
"
).
and ..
)CON
, PRN
, AUX
, NUL
, COM1
,
COM2
, COM3
, COM4
, COM5
, COM6
, COM7
, COM8
, COM9
,
LPT1
, LPT2
, LPT3
, LPT4
, LPT5
, LPT6
, LPT7
, LPT8
, and
LPT9
)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.
""
ResultAn 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 (NTFS
, ext
, etc.).
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.
sanitize(inputString, [options])
Sanitize inputString
by removing or replacing invalid characters.
Options:
options.replacement
: A string to replace invalid characters with.
Optional. Default: ""
.