Socket
Book a DemoInstallSign in
Socket

filenamify

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filenamify

Convert a string to a valid safe filename

latest
Source
npmnpm
Version
7.0.1
Version published
Weekly downloads
6.1M
0.73%
Maintainers
1
Weekly downloads
 
Created
Source

filenamify

Convert a string to a valid safe filename

On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods and spaces are reserved.

This module also removes non-printable control characters (including Unicode bidirectional marks) and normalizes Unicode whitespace.

Install

npm install filenamify

Usage

import filenamify from 'filenamify';

filenamify('<foo/bar>');
//=> '!foo!bar!'

filenamify('foo:"bar"', {replacement: '🐴'});
//=> 'foo🐴bar🐴'

API

filenamify(string, options?)

Convert a string to a valid filename.

filenamifyPath(path, options?)

Convert the filename in a path to a valid filename and return the augmented path.

import {filenamifyPath} from 'filenamify';

filenamifyPath('foo:bar');
//=> 'foo!bar'

options

Type: object

replacement

Type: string
Default: '!'

String to use as replacement for reserved filename characters.

Cannot contain: < > : " / \ | ? * or control characters.

maxLength

Type: number
Default: 100

Truncate the filename to the given length.

Only the base of the filename is truncated, preserving the extension. If the extension itself is longer than maxLength, you will get a string that is longer than maxLength, so you need to check for that if you allow arbitrary extensions.

Truncation is grapheme-aware and will not split Unicode characters (surrogate pairs or extended grapheme clusters). If the remaining budget (after accounting for the extension) is smaller than a whole grapheme, the base filename may be truncated to an empty string to avoid splitting.

Systems generally allow up to 255 characters, but we default to 100 for usability reasons.

Browser-only import

You can also import filenamify/browser, which only imports filenamify and not filenamifyPath, which relies on path being available or polyfilled. Importing filenamify this way is therefore useful when it is shipped using webpack or similar tools, and if filenamifyPath is not needed.

import filenamify from 'filenamify/browser';

filenamify('<foo/bar>');
//=> '!foo!bar!'

Keywords

filename

FAQs

Package last updated on 05 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts