1.0.5 • Published 3 years ago
acd-util-help v1.0.5
acd-util-help
Generic functions that support projects. Includes: generateSlug, filterMongooseText...
import { generateSlug } from 'acd-util-help';
generateSlug('some string') // some-string
// if you prefer something other than '-' as separator
generateSlug('some string', '_') // some_stringOptions
generateSlug('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}).
- If the value of
removeis 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, theremoveoption might not work as expected. - If the value of
removeis a string, it should be a single character. Otherwise, theremoveoption might not work as expected.
Extend
Out of the box generateSlug 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:
generateSlug('unicode ♥ is ☢') // unicode-love-is