-
Notifications
You must be signed in to change notification settings - Fork 32
/
playground.js
40 lines (29 loc) · 994 Bytes
/
playground.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
const form = document.getElementById('form')
form.addEventListener('submit', function (e) {
e.preventDefault()
if (form === null) {
console.error('form element not found')
return
}
const fd = new FormData(form)
const replacement = fd.get('replacement')
const lowercase = fd.get('lowercase')
const trim = fd.get('trim')
const fallback = fd.get('fallback')
const remove = fd.get('remove')
const regexG = fd.get('regex_g') === null ? '' : 'g'
const regexI = fd.get('regex_i') === null ? '' : 'i'
const opts = {}
if (replacement.length > 0) {
opts.replacement = replacement
}
if (remove !== null && remove.length > 0) {
const regex = new RegExp(`/${remove}/${regexG}${regexI}`)
opts.remove = regex
}
opts.lower = lowercase !== null
opts.trim = trim !== null
opts.fallback = fallback !== null
const output = window.slug(document.getElementById('input').value, opts)
document.getElementById('slugOutput').innerText = output
})