Giter Club home page Giter Club logo

slugify's Introduction

slugify

npm-version coveralls-status

var slugify = require('slugify')

slugify('some string') // some-string

// if you prefer something other than '-' as separator
slugify('some string', '_')  // some_string
  • Vanilla ES2015 JavaScript
    • If you need to use Slugify with older browsers, consider using version 1.4.7
  • No dependencies
  • Coerces foreign symbols to their English equivalent (check out the charMap for more details)
  • Works in the browser (window.slugify) and AMD/CommonJS-flavored module loaders

Options

slugify('some string', {
  replacement: '-',  // replace spaces with replacement character, defaults to `-`
  remove: undefined, // remove characters that match regex, defaults to `undefined`
  lower: false,      // convert to lower case, defaults to `false`
  strict: false,     // strip special characters except replacement, defaults to `false`
  locale: 'vi',      // language code of the locale to use
  trim: true         // trim leading and trailing replacement chars, defaults to `true`
})

Remove

For example, to remove *+~.()'"!:@ from the result slug, you can use slugify('..', {remove: /[*+~.()'"!:@]/g}).

Locales

The main charmap.json file contains all known characters and their transliteration. All new characters should be added there first. In case you stumble upon a character already set in charmap.json, but not transliterated correctly according to your language, then you have to add those characters in locales.json to override the already existing transliteration in charmap.json, but for your locale only.

You can get the correct language code of your language from here.

Extend

Out of the box slugify comes with support for a handful of Unicode symbols. For example the (radioactive) symbol is not defined in the charMap and therefore it will be stripped by default:

slugify('unicode ♥ is ☢') // unicode-love-is

However you can extend the supported symbols, or override the existing ones with your own:

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

Keep in mind that the extend method extends/overrides the default charMap for the entire process. In case you need a fresh instance of the slugify's charMap object you have to clean up the module cache first:

delete require.cache[require.resolve('slugify')]
var slugify = require('slugify')

Contribute

  1. Add chars to charmap.json
  2. Run tests npm test
  3. The tests will build the charmap in index.js and will sort the charmap.json
  4. Commit all modified files

Originally this was a vanilla javascript port of node-slug.
Note that the original slug module has been ported to vanilla javascript too.

slugify's People

Contributors

adrukh avatar ashotjanibekyan avatar avxkim avatar chmac avatar daniel1901 avatar energon7 avatar haltsir avatar httpstersk avatar itzlue avatar lorand-horvath avatar luisprmat avatar maxkrickl avatar neonerd avatar patricknausha avatar realityking avatar rohmanhm avatar roschaefer avatar roydigerhund avatar saadyousfi avatar simov avatar slavkobabic avatar stanley288 avatar stscoundrel avatar suecomarcus avatar sylvaindumont avatar thompsonsj avatar trott avatar ugurh avatar xxzefgh avatar yegorshtonda avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.