slug vs slugify vs speakingurl vs url-slug
URL Slug Generation Libraries
slugslugifyspeakingurlurl-slug

URL Slug Generation Libraries

URL slug generation libraries are essential tools in web development that help create user-friendly, SEO-optimized URLs by transforming strings into a format suitable for web addresses. These libraries typically convert spaces and special characters into hyphens, lowercase letters, and remove unwanted characters, ensuring that the resulting slugs are both readable and compliant with web standards. By using these libraries, developers can enhance the usability and accessibility of their web applications, making it easier for users to share and navigate links.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
slug039933.2 kB65 months agoMIT
slugify01,73016.1 kB429 days agoMIT
speakingurl01,121-299 years agoBSD-3-Clause
url-slug09126.4 kB016 days agoMIT

Feature Comparison: slug vs slugify vs speakingurl vs url-slug

Customization

  • slug:

    The 'slug' library offers minimal customization options, focusing on a straightforward approach to slug generation. It is designed to be simple and effective, making it easy to use without complex configurations.

  • slugify:

    'slugify' provides extensive customization options, allowing developers to define their own replacement characters and handle various edge cases, such as special characters and accents, making it versatile for different use cases.

  • speakingurl:

    'speakingurl' emphasizes human-readable slugs and allows for customization in terms of how slugs are generated, including options for preserving certain characters and formatting, which is beneficial for content-heavy sites.

  • url-slug:

    'url-slug' offers a range of customization features, including support for localization, enabling developers to create slugs that cater to different languages and cultural contexts.

Performance

  • slug:

    Being a lightweight library, 'slug' is optimized for performance and is suitable for applications where speed is a priority. It has minimal overhead, ensuring fast slug generation.

  • slugify:

    'slugify' is designed to handle complex strings efficiently, but it may introduce slight overhead due to its additional features. It balances performance with flexibility, making it suitable for most applications.

  • speakingurl:

    'speakingurl' focuses on generating readable slugs, which may involve more processing compared to simpler libraries. However, its performance is generally acceptable for most content-driven applications.

  • url-slug:

    'url-slug' is efficient in generating slugs but may have a performance trade-off due to its extensive options for localization and customization, making it ideal for applications that prioritize these features.

Ease of Use

  • slug:

    The 'slug' library is extremely easy to use, with a simple API that allows developers to generate slugs with minimal effort. This makes it ideal for beginners or projects that require quick implementation.

  • slugify:

    'slugify' is user-friendly and provides a straightforward API, but its additional options may require a bit of learning for new users. It strikes a good balance between ease of use and functionality.

  • speakingurl:

    'speakingurl' has a slightly more complex API due to its focus on generating human-readable slugs, but it is still accessible for developers familiar with slug generation concepts.

  • url-slug:

    'url-slug' offers a comprehensive API that may have a steeper learning curve for beginners, but it provides powerful features for those needing advanced slug generation capabilities.

Community and Support

  • slug:

    'slug' has a smaller community compared to other libraries, but it is well-maintained and provides sufficient documentation for basic use cases.

  • slugify:

    'slugify' benefits from a larger community and extensive documentation, making it easier to find support and examples for various use cases, which is advantageous for developers.

  • speakingurl:

    'speakingurl' has a dedicated user base and offers good documentation, though it may not be as widely adopted as some other libraries. It provides enough resources for effective implementation.

  • url-slug:

    'url-slug' has a growing community and offers decent documentation, but it may not have as many resources available compared to more popular slug libraries.

Localization Support

  • slug:

    The 'slug' library does not offer built-in localization support, making it less suitable for applications that require slugs in multiple languages or cultural contexts.

  • slugify:

    'slugify' has limited localization capabilities, primarily focusing on English and basic character replacements, which may not suffice for multilingual applications.

  • speakingurl:

    'speakingurl' excels in generating slugs that are human-readable and can be customized for different languages, making it a strong choice for content-heavy applications with diverse audiences.

  • url-slug:

    'url-slug' is designed with localization in mind, providing extensive options for generating slugs that cater to various languages and cultural nuances, making it ideal for global applications.

How to Choose: slug vs slugify vs speakingurl vs url-slug

  • slug:

    Choose 'slug' if you need a straightforward and lightweight solution for generating slugs without additional dependencies. It is ideal for simple projects where performance and minimalism are priorities.

  • slugify:

    Opt for 'slugify' if you require a more comprehensive slug generation with options for customization and handling various edge cases. It is suitable for applications that demand flexibility and robustness in slug creation.

  • speakingurl:

    Select 'speakingurl' if you want to generate slugs that are not only SEO-friendly but also human-readable, making them ideal for content-heavy applications where clarity is essential. It is particularly useful for blogs and articles.

  • url-slug:

    Use 'url-slug' if you need a library that focuses on generating slugs while providing extensive options for localization and customization, making it a good choice for multilingual applications.

README for slug

slug

Slugifies strings, even when they contain Unicode.

Make strings URL-safe.

  • Respects RFC 3986
  • No dependencies
  • Works in the browser or in Node.js
npm install slug

If you are using TypeScript you can install the accompanying types

npm install --save-dev @types/slug

Example

import slug from 'slug'
var print = console.log.bind(console, '>')

print(slug('i love unicode'))
// > i-love-unicode

print(slug('i love unicode', '_')) // If you prefer something else than `-` as separator
// > i_love_unicode

slug.charmap['♥'] = 'freaking love' // change default charmap or use option {charmap:{…}} as 2. argument
print(slug('I ♥ UNICODE'))
// > i-freaking-love-unicode

// To reset modifications to slug.charmap, use slug.reset():
slug.reset()
print(slug('I ♥ UNICODE'))
// > i-unicode

print(slug('Telephone-Number')) // lower case by default
// > telephone-number

print(slug('Telephone-Number', {lower: false})) // If you want to preserve case
// > Telephone-Number

// We try to provide sensible defaults.
// So Cyrillic text will be transliterated as if it were Russian:
print(slug('маленький подъезд'))
// > malenkij-poduezd

// But maybe you know it's Bulgarian:
print(slug('маленький подъезд', { locale: 'bg' }))
// > malenykiy-podaezd

// To set the default locale:
slug.setLocale('bg')
print(slug('маленький подъезд'))
// > malenykiy-podaezd

print(slug('unicode is ☢'))
// > unicode-is

slug.extend({'☢': 'radioactive'})
print(slug('unicode ♥ is ☢'))
// > unicode-is-radioactive

// slug.extend() modifies the default charmap for the entire process.
// If you need to reset charmap, multicharmap, and the default locale, use slug.reset():

slug.reset()
print(slug('unicode ♥ is ☢'))
// > unicode-is

// Custom removal of characters from resulting slug. Let's say that we want to
// remove all numbers for some reason.
print(slug('one 1 two 2 three 3'))
// > one-1-two-2-three-3
print(slug('one 1 two 2 three 3', { remove: /[0-9]/g }))
// > one-two-three

options

// options is either object or replacement (sets options.replacement)
slug('string', [{options} || 'replacement']);
slug.defaults.mode ='pretty';
slug.defaults.modes['rfc3986'] = {
    replacement: '-',      // replace spaces with replacement
    remove: null,          // (optional) regex to remove characters
    lower: true,           // result in lower case
    charmap: slug.charmap, // replace special characters
    multicharmap: slug.multicharmap, // replace multiple code unit characters
    trim: true,             // trim leading and trailing replacement chars
    fallback: true          // use base64 to generate slug for empty results
};
slug.defaults.modes['pretty'] = {
    replacement: '-',
    remove: null,
    lower: true,
    charmap: slug.charmap,
    multicharmap: slug.multicharmap,
    trim: true,
    fallback: true
};

Differences between slug and slugify packages

Here are some key differences between this package and slugify.

  • Stability: slug is ESM-only.
    slugify supports CommonJS and ESM.
  • Defaults: slug has the lower option enabled by default, lowercasing all slugs ('On SALE' becomes 'on-sale').
    slugify has the lower option disabled by default ('On SALE' becomes 'On-SALE').
  • Symbols: slug removes unrecognized symbols ('$100' becomes '100', '<5' becomes '5', etc.).
    slugify maps them to words ('$100' becomes 'dollar100', '<5' becomes 'less5', etc.).
  • Empty Output: slug will return a short, predictable hash (' ' becomes 'icag' and '🎉' becomes '8joiq').
    slugify will return an empty string (' ' and '🎉' become '').

Playground

A web playground is available at https://trott.github.io/slug/.

There is also a CLI tool available via npx slug. It doesn't allow you to specify options, so it's utility is minimal.