• Stars
    star
    337
  • Rank 125,272 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 11 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sanitize string for use as filename

sanitize-filename build status

Sanitize a string to be safe for use as a filename by removing directory paths and invalid characters.

Install

npm: sanitize-filename

npm install sanitize-filename

Example

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"

Details

sanitize-filename removes the following:

  • Control characters (0x00–0x1f and 0x80–0x9f)
  • Reserved characters (/, ?, <, >, \, :, *, |, and ")
  • Unix reserved filenames (. and ..)
  • Trailing periods and spaces (for Windows)
  • Windows reserved filenames (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.

Empty String "" Result

An empty string "" can be returned. For example:

var sanitize = require("sanitize-filename");
sanitize("..")
// -> ""

Non-unique Filenames

Two different inputs can return the same value. For example:

var sanitize = require("sanitize-filename");
sanitize("file?")
// -> "file"
sanitize ("*file*")
// -> "file"

File Systems

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.

Test Your File System

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.

API

sanitize(inputString, [options])

Sanitize inputString by removing or replacing invalid characters.

Options:

  • options.replacement: optional, string/function, default: "". If passed as a string, it's used as the replacement for invalid characters. If passed as a function, the function will be called with the invalid characters and it's return value will be used as the replacement. See String.prototype.replace for more info.

More Repositories

1

node-libs-react-native

Node core modules for React Native
JavaScript
204
star
2

node-ffmetadata

Read and write media metadata using ffmpeg
JavaScript
123
star
3

check-node-version

Check installed versions of node and npm
JavaScript
75
star
4

node-fpcalc

Calculate AcoustID audio fingerprint
JavaScript
68
star
5

css-eliminator

CSS dead code elimination
CSS
43
star
6

vtree-select

Select virtual-dom nodes using css selectors
JavaScript
31
star
7

node-acoustid

Get music metadata from AcoustID web service
JavaScript
28
star
8

node-stream-reduce

Reduce stream data to a single value
JavaScript
20
star
9

js-opc

Open Pixel Control protocol
JavaScript
16
star
10

truncate-utf8-bytes

Truncate string to length in bytes
JavaScript
15
star
11

js-is-integer

ES2015 (ES6) Number.isInteger polyfill
JavaScript
12
star
12

html-linkify

Turn plain text links into safe HTML anchor elements
JavaScript
11
star
13

html-escape

Escape string to be safe for use in html
JavaScript
7
star
14

nextcaltrain

Find the next Caltrain
JavaScript
7
star
15

music-organizer

Automated music metadata using MusicBrainz data
JavaScript
7
star
16

utf8-byte-length

Get byte length of utf8 string
JavaScript
5
star
17

node-stream-filter

Filter stream data
JavaScript
5
star
18

opc-simulator

Web-based Open Pixel Control simulator
JavaScript
5
star
19

nextcaltrain.com

Caltrain schedule web application
JavaScript
4
star
20

immutable-to-js

Convert immutable-js value to regular js value
JavaScript
4
star
21

spheron-accel

Stream accelerometer data from your Sphero
JavaScript
3
star
22

js-multi-compare

Compose array sort compare functions
JavaScript
3
star
23

window-scroll

Polyfill for window.scrollX and window.scrollY
JavaScript
3
star
24

devbox

Automated development machine configuration
Shell
2
star
25

bunyan-express-serializer

Bunyan serializer for Express objects
JavaScript
2
star
26

search-requires

Find require() calls to a given module
JavaScript
2
star
27

simple-passport

Minimal implementation of the PassportJS interface
JavaScript
2
star
28

boxjump

Multiplayer html5 game
CoffeeScript
2
star
29

js-map-values

Map object values, return a new object
JavaScript
2
star
30

map-stream-limit

Like map-stream but with concurrency limit
JavaScript
2
star
31

http-route

Functional HTTP routing
JavaScript
2
star
32

format-json-log

Human-readable json logs
JavaScript
1
star
33

set-input-value

Set input value and keep cursor position
JavaScript
1
star
34

jQuery-insertAt

Insert content as n-th child of element
JavaScript
1
star
35

data-uri-stream

Transform stream to create a data uri
JavaScript
1
star
36

node-stream-buffer

Buffer a stream and replay data at any time
JavaScript
1
star
37

dotfiles

My personal dotfiles
Shell
1
star
38

parse-binary-stream

Parse n-byte chunks from a binary stream
JavaScript
1
star
39

tote

Real-time networked 3D fighting game using Python and OGRE
Python
1
star
40

pageinfo

Get information (title, images, etc.) about a HTML document
JavaScript
1
star