slugify vs filenamify vs sanitize-filename
File Name and URL Slug Management Libraries Comparison
1 Year
slugifyfilenamifysanitize-filenameSimilar Packages:
What's File Name and URL Slug Management Libraries?

These libraries are designed to help developers create safe, valid, and user-friendly file names and URL slugs from various input strings. They ensure that the generated names are free from problematic characters that could cause issues in file systems or web URLs, enhancing both usability and security. Each library has its unique approach and features, making them suitable for different use cases in web development.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
slugify5,634,1831,61220.9 kB422 years agoMIT
filenamify4,915,1894948.29 kB52 years agoMIT
sanitize-filename2,331,004347-236 years agoWTFPL OR ISC
Feature Comparison: slugify vs filenamify vs sanitize-filename

Character Replacement

  • slugify:

    slugify transforms a string into a URL-friendly slug by converting it to lowercase and replacing spaces and special characters with hyphens, focusing on creating readable and SEO-friendly URLs.

  • filenamify:

    filenamify replaces invalid characters in a string with a specified character (default is a dash), ensuring that the resulting string is a valid file name across different operating systems and file systems.

  • sanitize-filename:

    sanitize-filename removes or replaces invalid characters from the input string, providing a clean and safe file name that adheres to the rules of the underlying file system without providing customization options for character replacement.

Customization

  • slugify:

    slugify offers some customization options, such as the ability to specify custom replacement characters for spaces and special characters, allowing for more control over the resulting slug.

  • filenamify:

    filenamify allows for customization of the character used for replacement, giving developers flexibility in how they want to handle invalid characters in file names.

  • sanitize-filename:

    sanitize-filename has limited customization options, primarily focusing on sanitizing input without providing extensive configuration for character replacement.

Use Cases

  • slugify:

    slugify is perfect for generating SEO-friendly slugs for URLs in web applications, making it essential for routing and content management systems.

  • filenamify:

    filenamify is ideal for scenarios where you need to generate valid file names from user input or other strings, such as when saving files uploaded by users in a web application.

  • sanitize-filename:

    sanitize-filename is best suited for applications that require strict validation of file names to prevent issues when saving files to disk, especially in environments with varying file system rules.

Performance

  • slugify:

    slugify is designed for speed and efficiency, quickly converting strings into slugs, which is crucial for applications that require real-time URL generation.

  • filenamify:

    filenamify is optimized for performance, handling string transformations efficiently, even for longer input strings, making it suitable for real-time applications.

  • sanitize-filename:

    sanitize-filename performs well for typical use cases, but its performance may vary based on the complexity of the input string and the number of characters to sanitize.

Community and Maintenance

  • slugify:

    slugify enjoys a robust community and is frequently updated, making it a reliable choice for developers looking for a well-supported library.

  • filenamify:

    filenamify has a growing community and is actively maintained, ensuring that it stays up-to-date with best practices and user needs.

  • sanitize-filename:

    sanitize-filename is a widely used library with a stable user base, but it may not receive frequent updates compared to newer libraries.

How to Choose: slugify vs filenamify vs sanitize-filename
  • slugify:

    Select slugify when you need to generate URL-friendly slugs from strings, particularly for use in web applications where SEO and readability are important, as it converts strings into lowercase and replaces spaces and special characters with hyphens.

  • filenamify:

    Choose filenamify if you need a library that transforms any string into a valid file name by replacing invalid characters with a specified character, making it suitable for various file systems.

  • sanitize-filename:

    Opt for sanitize-filename if you require a straightforward solution to sanitize file names by removing or replacing invalid characters, ensuring compatibility across different operating systems and environments.

README for slugify

slugify

npm-version coveralls-status

var slugify = require('slugify')

slugify('some string') // some-string

// if you prefer something other than '-' as separator
slugify('some string', '_')  // some_string
  • Vanilla ES2015 JavaScript
    • If you need to use Slugify with older browsers, consider using version 1.4.7
  • No dependencies
  • Coerces foreign symbols to their English equivalent (check out the charMap for more details)
  • Works in the browser (window.slugify) and AMD/CommonJS-flavored module loaders

Options

slugify('some string', {
  replacement: '-',  // replace spaces with replacement character, defaults to `-`
  remove: undefined, // remove characters that match regex, defaults to `undefined`
  lower: false,      // convert to lower case, defaults to `false`
  strict: false,     // strip special characters except replacement, defaults to `false`
  locale: 'vi',      // language code of the locale to use
  trim: true         // trim leading and trailing replacement chars, defaults to `true`
})

Remove

For example, to remove *+~.()'"!:@ from the result slug, you can use slugify('..', {remove: /[*+~.()'"!:@]/g}).

  • If the value of remove is a regular expression, it should be a character class and only a character class. It should also use the global flag. (For example: /[*+~.()'"!:@]/g.) Otherwise, the remove option might not work as expected.
  • If the value of remove is a string, it should be a single character. Otherwise, the remove option might not work as expected.

Locales

The main charmap.json file contains all known characters and their transliteration. All new characters should be added there first. In case you stumble upon a character already set in charmap.json, but not transliterated correctly according to your language, then you have to add those characters in locales.json to override the already existing transliteration in charmap.json, but for your locale only.

You can get the correct language code of your language from here.

Extend

Out of the box slugify comes with support for a handful of Unicode symbols. For example the (radioactive) symbol is not defined in the charMap and therefore it will be stripped by default:

slugify('unicode ♥ is ☢') // unicode-love-is

However you can extend the supported symbols, or override the existing ones with your own:

slugify.extend({'☢': 'radioactive'})
slugify('unicode ♥ is ☢') // unicode-love-is-radioactive

Keep in mind that the extend method extends/overrides the default charMap for the entire process. In case you need a fresh instance of the slugify's charMap object you have to clean up the module cache first:

delete require.cache[require.resolve('slugify')]
var slugify = require('slugify')

Contribute

  1. Add chars to charmap.json
  2. Run tests npm test
  3. The tests will build the charmap in index.js and will sort the charmap.json
  4. Commit all modified files

Originally this was a vanilla javascript port of node-slug.
Note that the original slug module has been ported to vanilla javascript too.