-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathslugs.js
28 lines (24 loc) · 913 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
25
26
27
28
/*
* SLUGS MODULE
* By Aaron Stannard (aaron@stannardlabs.com)
*/
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(/ü/g, 'ue').
replace(/ä/g, 'ae').
replace(/ö/g, 'oe').
replace(/ß/g, 'ss').
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
}