-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (104 loc) · 3.31 KB
/
app.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function start() {
const ucBtn = document.querySelector('#uc');
const lcBtn = document.querySelector('#lc');
const tcBtn = document.querySelector('#tc');
const scBtn = document.querySelector('#sc');
const copyBtn = document.querySelector('#copy');
ucBtn.addEventListener('click', ucFun);
lcBtn.addEventListener('click', lcFun);
tcBtn.addEventListener('click', tcFun);
scBtn.addEventListener('click', scFun);
copyBtn.addEventListener('click', copyFun);
setInterval(() => {
countLine(); countWord();
}, 500);
}
start();
function countLine() {
const txt = document.querySelector('#txt');
let lines = document.querySelector('#lines');
let textValue = txt.value.trim();
const line = [...textValue].reduce((a, c) => a + (c === '\n' ? 1 : 0), 0)
if (textValue != "") {
lines.innerHTML = line + 1;
}
else {
lines.innerHTML = line;
}
}
function countWord() {
const txt = document.querySelector('#txt');
const characters = document.querySelector('#letters');
const words = document.querySelector('#words');
let textValue = txt.value.trim(); // đoạn văn bản
let textLength = textValue.length; // số kí tự
let wordNum = textValue.split(' ').length + textValue.split('\n').length - 1;
if (textValue != "") {
characters.innerHTML = textLength;
words.innerHTML = 500000 - (500000 - wordNum);
}
else {
characters.innerHTML = textValue.length
words.innerHTML = wordNum - 1;
}
}
function ucFun() {
const txt = document.querySelector('#txt');
let textValueUp = txt.value.trim().toUpperCase();
txt.value = textValueUp;
}
function lcFun() {
const txt = document.querySelector('#txt');
let textValue = txt.value.trim().toLowerCase();
txt.value = textValue;
}
// function titleCase(str) {
// let rs = [];
// str = str.split(/\r?\n/);
// let strLength = str.length;
// for (let i = 0; i < strLength; i++) {
// let stringTmp = str[i];
// stringTmp = stringTmp.split(' ');
// console.log(stringTmp);
// for (let j = 0; j < stringTmp.length; j++) {
// stringTmp[j] = stringTmp[j].charAt(0).toUpperCase() + stringTmp[j].slice(1) + '\n';
// rs.push(stringTmp[j]);
// }
// }
// console.log(rs);
// return rs.join(' ');
// }
function toTitleCase(str) {
return str.replace(
/\w\S*/g,
function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
);
}
function tcFun() {
const txt = document.querySelector('#txt');
let textValue = txt.value.trim().toLowerCase();
txt.value = toTitleCase(textValue);
}
function capitalize(s) {
return s[0].toUpperCase() + s.slice(1);
}
function capitalize2(str) {
str = str.split('/n');
for (var i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}
function scFun() {
const txt = document.querySelector('#txt');
let textValueUp = txt.value.trim().toLowerCase();
txt.value = capitalize(textValueUp);
}
function copyFun() {
const txt = document.querySelector('#txt');
txt.select();
txt.setSelectionRange(0, 99999); // For mobile devices
navigator.clipboard.writeText(txt.value);
}