ultima-slug
v1.0.0
Published
'UltimaSlug' is a term used to describe the process of converting a string into a URL-friendly `slug`. This typically involves removing spaces and special characters, converting to lowercase, and replacing spaces with hyphens. The resulting string can be
Downloads
2
Maintainers
Readme
ultima-slug
var ultimaSlug = require('ultima-slug')
ultimaSlug('some string') // some-string
// if you prefer something other than '-' as separator
ultimaSlug('some string', '_') // some_string
Options
ultimaSlug('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 ultimaSlug('..', {remove: /[*+~.()'"!:@]/g})
.
- If the value of
remove
is a regular expression, it should be a character class and only a character class. It should also use the global flag. (For example:/[*+~.()'"!:@]/g
.) Otherwise, theremove
option might not work as expected. - If the value of
remove
is a string, it should be a single character. Otherwise, theremove
option might not work as expected.
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 ultimaSlug
comes with support for a handful of Unicode symbols. For example the ☢
(radioactive) symbol is not defined in the [charMap
][charmap] and therefore it will be stripped by default:
ultimaSlug('unicode ♥ is ☢') // unicode-love-is
However you can extend the supported symbols, or override the existing ones with your own:
ultimaSlug.extend({'☢': 'radioactive'})
ultimaSlug('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 ultimaSlug's charMap
object you have to clean up the module cache first:
delete require.cache[require.resolve('ultima-slug')]
var ultimaSlug = require('ultima-slug')
Contribute
- Add chars to
charmap.json
- Run tests
npm test
- The tests will build the charmap in
index.js
and will sort thecharmap.json
- 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.