slugify vs slug vs url-slug vs slugg
Slug Generation Libraries Comparison
1 Year
slugifyslugurl-slugsluggSimilar Packages:
What's Slug Generation Libraries?

Slug generation libraries in JavaScript are tools that convert strings (like titles or names) into URL-friendly formats by replacing spaces and special characters with hyphens or other specified characters. This process is essential for creating clean, readable, and SEO-friendly URLs. These libraries offer various features such as customizable delimiters, support for different languages, and options to handle duplicates, making them versatile for web development projects. They help ensure that URLs are both user-friendly and compliant with web standards, improving navigation and search engine optimization.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
slugify5,481,5991,66220.9 kB432 years agoMIT
slug342,94537833 kB72 months agoMIT
url-slug248,8928934 kB12 years agoMIT
slugg32,119366-28 years agoBSD
Feature Comparison: slugify vs slug vs url-slug vs slugg

Customization

  • slugify:

    slugify is the most customizable of the four, allowing developers to set delimiters, specify allowed characters, and configure language-specific rules. This makes it ideal for projects that require fine-tuned control over the slugging process.

  • slug:

    slug offers basic customization options, such as changing the delimiter and removing specific characters, but it is relatively limited in terms of configurability.

  • url-slug:

    url-slug allows for simple customization of delimiters and character replacement, but it is designed to be straightforward and does not offer extensive configuration options.

  • slugg:

    slugg provides some customization for handling duplicates but is not highly configurable beyond that. It focuses more on automatically managing duplicate slugs than on offering extensive customization features.

Duplicate Handling

  • slugify:

    slugify does not have built-in duplicate handling. It generates slugs based on the input string, and any duplicate management must be handled separately by the developer.

  • slug:

    slug does not handle duplicate slugs automatically. Developers need to implement their own logic to manage duplicates if required.

  • url-slug:

    url-slug does not provide any duplicate handling features. It generates slugs based on the input string, and managing duplicates is the responsibility of the developer.

  • slugg:

    slugg automatically handles duplicate slugs by appending a number to the end of the slug (e.g., my-slug, my-slug-1, my-slug-2). This feature is built-in and requires no additional implementation.

Performance

  • slugify:

    slugify is efficient, but its performance may vary depending on the complexity of the customization and the number of characters being processed. It is generally fast for most use cases but may be slower than simpler libraries for highly customized slug generation.

  • slug:

    slug is lightweight and performs well for generating slugs quickly. It has minimal overhead, making it suitable for applications where performance is a concern.

  • url-slug:

    url-slug is designed to be fast and efficient, with a focus on minimalism. It performs well for generating slugs quickly without significant resource usage.

  • slugg:

    slugg is also lightweight, but the duplicate handling feature may introduce a slight performance overhead when generating slugs, especially if many duplicates are present.

Ease of Use: Code Examples

  • slugify:

    Customizable slugs with slugify

    const slugify = require('slugify');
    const mySlug = slugify('Hello World!', { lower: true, strict: true });
    console.log(mySlug); // Output: hello-world
    
  • slug:

    Simple slug generation with slug

    const slug = require('slug');
    const mySlug = slug('Hello World!');
    console.log(mySlug); // Output: hello-world
    
  • url-slug:

    Simple and fast slugs with url-slug

    const urlSlug = require('url-slug');
    const mySlug = urlSlug('Hello World!');
    console.log(mySlug); // Output: hello-world
    
  • slugg:

    Automatic duplicate handling with slugg

    const slugg = require('slugg');
    const slug1 = slugg('My Slug');
    const slug2 = slugg('My Slug');
    const slug3 = slugg('My Slug');
    console.log(slug1, slug2, slug3);
    // Output: my-slug my-slug-1 my-slug-2
    
How to Choose: slugify vs slug vs url-slug vs slugg
  • slugify:

    Choose slugify if you require a highly customizable slug generation tool with support for different languages and special character handling. It is suitable for projects that need more control over the slugging process and want to configure settings like delimiters and allowed characters.

  • slug:

    Choose slug if you need a simple and lightweight solution for generating slugs with minimal configuration. It is ideal for projects where you want a quick and straightforward implementation without many dependencies.

  • url-slug:

    Choose url-slug if you need a lightweight library focused on creating SEO-friendly slugs with a simple API. It is perfect for projects that prioritize performance and simplicity without sacrificing functionality.

  • slugg:

    Choose slugg if you want a library that automatically handles duplicate slugs by appending a number to them. This is useful for applications where you need to ensure uniqueness without manually checking or managing duplicates.

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.