forked from Aaronontheweb/node-slugs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslugs.js
24 lines (20 loc) · 793 Bytes
/
slugs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* SLUGS MODULE
* By Aaron Stannard ([email protected])
*/
var slug = module.exports = function slug (incString, separator, preserved) {
var p = ['.', '=', '-'];
var s = '-';
if(typeof preserved != 'undefined') {
p = preserved;
}
if(typeof separator != 'undefined') {
s = separator;
}
return incString.toLowerCase().
replace(new RegExp('[' + p.join('') + ']', 'g'), ' '). // replace preserved characters with spaces
replace(/-{2,}/g, ' '). // remove duplicate spaces
replace(/^\s\s*/, '').replace(/\s\s*$/, ''). // trim both sides of string
replace(/[^\w\ ]/gi, ''). // replaces all non-alphanumeric with empty string
replace(/[\ ]/gi, s); // Convert spaces to dashes
}