node-slug

slugifies even utf-8 chars!

  • Owner: dodo/node-slug
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

slug

slugifies every string, even when it contains unicode!

Make strings url-safe.

  • respecting RFC 3986
  • Comprehensive tests
  • No dependencies (except the unicode table)
  • Not in coffee-script (except the tests lol)
  • Coerces foreign symbols to their english equivalent
  • Works in browser (window.slug) and AMD/CommonJS-flavoured module loaders (except the unicode symbols unless you use browserify but who wants to download a ~2mb js file, right?)
npm install slug
bower install slug

example

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

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

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

print(slug('i ♥ unicode', '_')) // If you prefer something else then `-` as seperator
// > 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('☏-Number', {lower: true})) // If you prefer lower case
// > telephone-number

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

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
    symbols: true,         // replace unicode symbols or not
    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: '-',
    symbols: true,
    remove: /[.]/g,
    lower: false,
    charmap: slug.charmap,
    multicharmap: slug.multicharmap
};

browser

When using browserify you might want to remove the symbols table from your bundle by using --ignore similar to this:

# generates a standalone slug browser bundle:
browserify slug.js --ignore unicode/category/So -s slug > slug-browser.js

Build Status

Bitdeli Badge

Main metrics

Overview
Name With Ownerdodo/node-slug
Primary LanguageCoffeeScript
Program languageJavaScript (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2011-09-19 19:46:36
Pushed At2019-04-05 15:49:19
Last Commit At2015-04-18 11:43:07
Release Count19
Last Release Name0.9.1 (Posted on )
First Release Name0.2.1 (Posted on )
用户参与
Stargazers Count1.1k
Watchers Count14
Fork Count94
Commits Count133
Has Issues Enabled
Issues Count49
Issue Open Count25
Pull Requests Count16
Pull Requests Open Count17
Pull Requests Close Count16
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private