Git Product home page Git Product logo

node-sanitize-filename-esm's Introduction

node-sanitize-filename-esm

A functionally verbatim fork of the node-sanitize-filename package which is named sanitize-filename on npm.

The only changes which I have made are to the package.json file to add the module field and to the index.js file to add the export default statement.

This fork is intended to be used as a drop-in replacement for the original package when using ES modules.

I am using it as part of my Marchive product.

Following is the original README.md file from the original package, with updated examples;

node-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

import sanitize from "sanitize-filename";

// Some string that may be unsafe or invalid as a filename
const UNSAFE_USER_INPUT = "~/.\u0000ssh/authorized_keys";

// Sanitize the string to be safe for use as a filename.
const 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:

import sanitize from "sanitize-filename";
sanitize("..")
// -> ""

Non-unique Filenames

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

import sanitize from "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.

node-sanitize-filename-esm's People

Contributors

parshap avatar joelmukuthu avatar mikeralphson avatar paulirish avatar greenkeeperio-bot avatar carltonf avatar actionshrimp avatar martinshaw avatar patontheback avatar tjenkinson avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.