You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
'use strict'; |
|
|
|
const escapeDiacritic = require('./escape_diacritic'); |
|
const escapeRegExp = require('./escape_regexp'); |
|
// eslint-disable-next-line no-control-regex |
|
const rControl = /[\u0000-\u001f]/g; |
|
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g; |
|
|
|
function slugize(str, options = {}) { |
|
if (typeof str !== 'string') throw new TypeError('str must be a string!'); |
|
|
|
const separator = options.separator || '-'; |
|
const escapedSep = escapeRegExp(separator); |
|
|
|
const result = escapeDiacritic(str) |
|
// Remove control characters |
|
.replace(rControl, '') |
|
// Replace special characters |
|
.replace(rSpecial, separator) |
|
// Remove continous separators |
|
.replace(new RegExp(`${escapedSep}{2,}`, 'g'), separator) |
|
// Remove prefixing and trailing separtors |
|
.replace(new RegExp(`^${escapedSep}+|${escapedSep}+$`, 'g'), ''); |
|
|
|
switch (options.transform) { |
|
case 1: |
|
return result.toLowerCase(); |
|
|
|
case 2: |
|
return result.toUpperCase(); |
|
|
|
default: |
|
return result; |
|
} |
|
} |
|
|
|
module.exports = slugize;
|
|
|