-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.41 KB
/
index.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function unslugify(slugs,args) {
if (typeof slugs !== 'string') {
throw new Error('unslugify: slug must be string')
}
args = (typeof args === 'string') ? {replacement: args} : args || {}
var capitalizeEachWord = args.capitalizeEachWord === undefined ? false : args.capitalizeEachWord
var lower = args.lower === undefined ? true : args.lower
var replacement = args.replacement === undefined ? "-" : args.replacement
if(replacement === "_"){
slugs = slugs.replace(/_/g, '_');
slugs = slugs.replace(/--/g, '_');
var list = [];
slugs.split('_').forEach(function (slug) {
capitalizeEachWord ? list.push(slug.substr(0, 1).toUpperCase() + slug.substr(1)) : list.push(slug.substr(0, 1)+ slug.substr(1))
})
if(lower){
return list.join(' ');
}
else{
return list.join(' ').toUpperCase();
}
}
else{
slugs = slugs.replace(/_/g, '-');
slugs = slugs.replace(/--/g, '-');
var list = [];
slugs.split('-').forEach(function (slug) {
capitalizeEachWord ? list.push(slug.substr(0, 1).toUpperCase() + slug.substr(1)) : list.push(slug.substr(0, 1)+ slug.substr(1))
})
if(lower){
return list.join(' ');
}
else{
return list.join(' ').toUpperCase();
}
}
}
module.exports = unslugify;