-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathslugit.js
More file actions
49 lines (39 loc) · 1.44 KB
/
Copy pathslugit.js
File metadata and controls
49 lines (39 loc) · 1.44 KB
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
47
48
49
class Slugit {
slugFunc(text, owner){
var isCalled = owner["Slugit"] != "Slugit"
if(isCalled) {
throw new ReferenceError('slugFunc cannot be implemented!');
return false;
}
var char_map = {
'Ç' : 'c', 'ç' : 'c', 'Ğ' : 'g', 'ğ' : 'g',
'Ş' : 's', 'ş' : 's', 'Ü' : 'u', 'ü' : 'u',
'Ö' : 'o', 'ö' : 'o', 'ı' : 'i'
}
text = text.replace(/[^\w ]/g, function(char) {
return char_map[char] || char;
});
return text.toString().toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
}
doSlug(data) {
var el = document.querySelector(data.el)
var target = document.querySelector(data.target)
var elems = ["form", "input", "textarea",
"select", "option", "button"]
var isFormElem = elems.indexOf(target.tagName.toLowerCase())
el.addEventListener(data.event, function() {
setTimeout(function() {
if(isFormElem > 0) {
target.value = Slugit.prototype.slugFunc(el.value, {Slugit: "Slugit"})
} else {
target.innerText = Slugit.prototype.slugFunc(el.value, {Slugit: "Slugit"})
}
}, data.timeout)
})
}
}