slug
slugifies even utf-8 chars!
Last updated a month 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

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: /[.]/g,
    lower: false,
    charmap: slug.charmap,
    multicharmap: slug.multicharmap
};

Current Tags

  • 3.3.0                                ...           latest (a month ago)

33 Versions

  • 3.3.0                                ...           a month ago
  • 3.2.0                                ...           a month ago
  • 3.1.0                                ...           a month ago
  • 3.0.1                                ...           2 months ago
  • 3.0.0                                ...           2 months ago
  • 2.1.1                                ...           5 months ago
  • 2.1.0                                ...           6 months ago
  • 2.0.0                                ...           6 months ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.9.4                                ...           a year 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                                ...           6 years ago
  • 0.4.1                                ...           6 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                                ...           8 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 18
This Week 76
This Month 268
Last Day 25
Last Week 1,366
Last Month 2,022
Dependencies (0)
None
Dev Dependencies (11)
Dependents (761)

Copyright 2014 - 2016 © taobao.org |