slug
slugifies even utf-8 chars!
Last updated 2 months ago by trott .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install slug 
SYNC missed versions from official npm registry.

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

Example

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

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

print(slug('i ♥ 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

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

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

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

// We try to provide sensible defaults.
// So Cyrillic text will be transliterated as if it were Russian:
print(slug('компютъра'))
// > kompyutura

// But maybe you know it's Bulgarian:
print(slug('компютъра', { locale: 'bg' }))
// > kompyutara

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

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

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

slug.reset()
print(slug('unicode ♥ is ☢')
// > unicode-love-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 multi-characters
};
slug.defaults.modes['pretty'] = {
    replacement: '-',
    remove: null,
    lower: false,
    charmap: slug.charmap,
    multicharmap: slug.multicharmap
};

Testing Powered By SauceLabs

Current Tags

  • 3.3.4                                ...           latest (2 months ago)

37 Versions

  • 3.3.4                                ...           2 months ago
  • 3.3.3                                ...           2 months ago
  • 3.3.2                                ...           2 months ago
  • 3.3.1                                ...           2 months ago
  • 3.3.0                                ...           4 months ago
  • 3.2.0                                ...           4 months ago
  • 3.1.0                                ...           4 months ago
  • 3.0.1                                ...           4 months ago
  • 3.0.0                                ...           4 months ago
  • 2.1.1                                ...           8 months ago
  • 2.1.0                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           2 years ago
  • 0.9.4                                ...           2 years ago
  • 0.9.3                                ...           2 years ago
  • 0.9.2                                ...           2 years ago
  • 0.9.1                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.0                                ...           6 years ago
  • 0.7.1                                ...           6 years ago
  • 0.7.0                                ...           6 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.0                                ...           6 years ago
  • 0.4.2                                ...           7 years ago
  • 0.4.1                                ...           7 years ago
  • 0.4.0                                ...           7 years ago
  • 0.3.4                                ...           7 years ago
  • 0.3.3                                ...           7 years ago
  • 0.3.2                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.0                                ...           7 years ago
  • 0.2.2                                ...           9 years ago
  • 0.2.1                                ...           9 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 2
This Week 257
This Month 730
Last Day 4
Last Week 334
Last Month 1,226
Dependencies (0)
None
Dev Dependencies (13)
Dependents (775)

Copyright 2014 - 2016 © taobao.org |