9.0.0 • Published 1 month ago

slug v9.0.0

Weekly downloads
160,315
License
MIT
Repository
github
Last release
1 month ago

slug

Slugifies strings, even when they contain Unicode.

Make strings URL-safe.

  • Respects RFC 3986
  • No dependencies
  • Works in browser (window.slug) and AMD/CommonJS-flavoured module loaders
npm install slug

If you are using TypeScript you can install the accompanying types

npm install --save-dev @types/slug

Example

var slug = require('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: false,
    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.

  • 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 '').
  • Stability: slug is planning a new release that will drop support for CommonJS and only support ESM modules.
    slugify will continue to support CommonJS and is likely to remain stable for the foreseeable future.

Playground

A (painfully minimal) web playground is available at https://trott.github.io/slug/. It doesn't allow you to specify options, so it's utility is minimal. Pull requests welcome to add the ability to specify options.

There is also a (similarly minimal) CLI tool available via npx slug. As with the web playground, it doesn't allow you to specify options, so it's utility is minimal.

@sprucelabs/spruce-skill@sayl1th/databless@yad2/service-scaffoldingkampungers-generatormakeover-models@motionbank-js/media-serveragent-interfaceagile-director-backstretchshop-base@component-driven/gatsby-theme-content-collectionsdev-agnitio-clilogdna-workersgenerator-clint-backgenerator-clint-apitaproop-appnuxt-bloggerzdznestjs-migrationtwentyoverten-themesgraphdocartan-static-genlistbucket-generatortv-api-framework@egvelho/next-material-components@egvelho/next-metadatatropicaliasazske-createyela-creategenerator-rise-typescriptvuemanticnetcentric-website-4-febuild@biproxi/modelstv-api-commonbuysellexchangeco-fnc-shopifybuysellexchangeio-fnc-shopifypindown-cli@elwood-studio/distribute-compiler@elwood-studio/distribute-nextjsgenerator-click@infinitebrahmanuniverse/nolb-sluapi-cataloguesteletronnocodb-daily@everything-registry/sub-chunk-2782transfer-mp4-to-hlsdelpostodesign-manualsimple-blogsequelize-articlescrape-cachesequelize-sluggifyscs-dependency-visualizerscreenshootersearch-meinfernbus-locationssearch-flix-locationssherry_graphdocdrone-aclsimple-web-crawlerdstatuspagesmusteemclisparql-routerembellishjsstelloepic-db-coreent-hapi-sequelizestasis-generatorentitizer.concepts-storageentitizer.entities-storagestaticmanstaticman-azure-fnstackticdocsnowdocmaticdocpad-plugin-catalogsdomain-driven-pouchdb-persistence-plugindraft5-post-content-rendererdefmodedortcreate-helio-appcreate-ivx-appcreate-junit-report@das.laboratory/gulp-plugin-interactiveqa-screenshotscyboardcthrecord-radiorss-to-jekyllrufio-filter-mediametarufio-filter-yamlmetarpgparser-pagesryuuryuu-betaryuu-debugsails-slugsails-hook-sluggablesakinsaturn-migratorreptarreactdown
9.0.0

1 month ago

8.2.3

9 months ago

6.1.0

2 years ago

8.2.2

2 years ago

8.1.0

2 years ago

9.0.0-beta.1

2 years ago

7.0.0

2 years ago

8.0.0

2 years ago

8.2.1

2 years ago

8.2.0

2 years ago

6.0.0

2 years ago

7.0.0-beta.1

2 years ago

6.0.0-beta.3

2 years ago

6.0.0-beta.4

2 years ago

6.0.0-beta.1

2 years ago

6.0.0-beta.2

2 years ago

5.3.0

2 years ago

5.1.1

2 years ago

5.2.0

2 years ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.1.0

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.5.2

3 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.3.6

4 years ago

3.4.0

4 years ago

3.3.5

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.0

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.4

10 years ago

0.3.3

11 years ago

0.3.2

11 years ago

0.3.1

11 years ago

0.3.0

11 years ago

0.2.2

12 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.1

12 years ago

0.1.0

13 years ago