sanitize-filename
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
truncate-utf8-bytes | 1.0.2 | 2.18 kB | WTFPL | prod | 2 |
The sanitize-filename is a JavaScript npm package designed to sanitize a string for safe usage as a filename. It achieves this by removing directory paths and invalid characters. This makes the package particularly useful when dealing with user-generated input that may contain unsafe or invalid strings for filenames. The resulting string will not contain any directory paths, making it safe for use as a filename.
To use the sanitize-filename package in your JavaScript project, you need to first install it via npm using the command npm install sanitize-filename
. Once installed, you can require it in your JavaScript file and use its main function to sanitize a potentially unsafe filename. Here is a basic example of how to use it:
var sanitize = require("sanitize-filename");
// Assume you have a string that may be unsafe as a filename
var UNSAFE_USER_INPUT = "~/.\u0000ssh/authorized_keys";
var filename = sanitize(UNSAFE_USER_INPUT);
// The result, assigned to the variable filename, is a sanitized version of the unsafe string.
// In this example, the result would be: "~.sshauthorized_keys"
In addition to the base functionality, you can also pass an options object to the sanitize function to specify a replacement for invalid characters. The replacement can be either a string or a function.
The detailed documentation and usage guidelines for sanitize-filename can be found here. The page includes installation instructions, examples, and information on the algorithm used by the package to sanitize filenames, covering various edge cases and specificities of different operating systems. You can also find details about available options and their usage. This package is hosted on GitHub, and the repository can be accessed at git+ssh://git@github.com/parshap/node-sanitize-filename.git.