-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
55 lines (53 loc) · 2.21 KB
/
scripts.js
File metadata and controls
55 lines (53 loc) · 2.21 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
50
51
52
53
54
55
document.getElementById('changeButton').addEventListener('click', changeTextCase);
document.getElementById('copyButton').addEventListener('click', copyToClipboard);
function changeTextCase() {
const caseType = document.getElementById('caseSelector').value;
let text = document.getElementById('inputText').value;
switch(caseType) {
case 'upperCase':
text = text.toUpperCase();
break;
case 'lowerCase':
text = text.toLowerCase();
break;
case 'titleCase':
text = text.toLowerCase().replace(/\b(\w)/g, char => char.toUpperCase());
break;
case 'camelCase':
text = text.toLowerCase().replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match, index) => index === 0 ? match.toLowerCase() : match.toUpperCase()).replace(/\s+/g, '');
break;
case 'snakeCase':
text = text.toLowerCase().replace(/\s+/g, '_');
break;
case 'hyphenCase':
text = text.toLowerCase().replace(/\s+/g, '-');
break;
case 'dotCase':
text = text.toLowerCase().replace(/\s+/g, '.');
break;
case 'alternatingCase':
text = text.split('').map((char, index) => index % 2 === 0 ? char.toUpperCase() : char.toLowerCase()).join('');
break;
case 'switchCase':
text = text.split('').map(char => char === char.toLowerCase() ? char.toUpperCase() : char.toLowerCase()).join('');
break;
case 'randomCase':
text = text.split('').map(char => Math.random() > 0.5 ? char.toUpperCase() : char.toLowerCase()).join('');
break;
case 'sentenceCase':
text = text.toLowerCase().replace(/(^\s*\w|[.!?]\s*\w)/g, char => char.toUpperCase());
break;
default:
break;
}
document.getElementById('inputText').value = text;
}
function copyToClipboard() {
const text = document.getElementById('inputText').value;
navigator.clipboard.writeText(text).then(() => {
alert('Text copied to clipboard');
}).catch(err => {
console.error('Failed to copy text: ', err);
alert('Failed to copy text. Please try again.');
});
}