-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagz.js
70 lines (52 loc) · 1.55 KB
/
tagz.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
function countTags() {
var inputText = document.getElementById('tags').value;
// var cleanedText = inputText.replace(/['"]+/g, '');
// console.log(cleanedText);
var array = inputText.replace(/"/g, ',').split(',');
var trimmedArray = function (array) {
for (i = 0; i < array.length; i++) {
trimmedArray[i] = array[i].trim();
}
}
console.log(array);
var tagsCount = array.length;
document.getElementById("total-tags").innerHTML = tagsCount;
array.sort();
var deduped = array.reduce( function (result, item) {
var lastItem = result[result.length - 1] || {};
if (lastItem.text && lastItem.text === item) {
lastItem.count++;
} else {
result.push({
text: item,
count: 1
});
}
return result;
}, [])
deduped.sort(function(row1, row2) {
if (row1.count < row2.count) {
return 1;
}
if (row1.count > row2.count) {
return -1;
}
return 0;
});
var rowCount = 1;
for (var i = 0; i <= deduped.length; i++) {
var itemCount = deduped[i].count;
var itemText = deduped[i].text;
if (itemText.trim().length !== 0) {
var table = document.getElementById("tableBody");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0)
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = rowCount;
cell2.innerHTML = itemText;
cell3.innerHTML = itemCount;
rowCount++;
}
}
}