Skip to content

Commit d24d787

Browse files
author
Andy Zehady
committed
Multi comma separated tag functionality.
1 parent dd62d11 commit d24d787

1 file changed

Lines changed: 52 additions & 7 deletions

File tree

src/transactions/vote.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,59 @@ module.exports = {
5353
// update top tags
5454
var topTags = []
5555
for (let i = 0; i < content.votes.length; i++) {
56-
var exists = false
57-
for (let y = 0; y < topTags.length; y++)
58-
if (topTags[y].tag === content.votes[i].tag) {
59-
exists = true
60-
topTags[y].vt += Math.abs(content.votes[i].vt)
56+
var newTags = {}
57+
for (let y = 0; y < topTags.length; y++) {
58+
contentTags = []
59+
tmpTags = content.votes[i].tag
60+
if (tmpTags.length > config.tagMaxLength)
61+
tmpTags = tmpTags.substring(0, config.tagMaxLength)
62+
tmpTags = content.votes[i].tag.trim().split(",")
63+
for (let j = 0; j < tmpTags.length; j++) {
64+
contentTags.push(tmpTags[j].trim())
6165
}
62-
if (!exists && content.votes[i].tag)
63-
topTags.push({tag: content.votes[i].tag, vt: Math.abs(content.votes[i].vt)})
66+
67+
// idea is to distribute the vt equally to all comma separated tags, the extra is given to the first tag
68+
// process for each comma separated tags
69+
nTag = contentTags.length
70+
totalTagVt = content.votes[i].vt
71+
for (let j = 0; j < nTag; j++) {
72+
curTag = contentTags[j]
73+
curTagVt = (totalTagVt / nTag) + (totalTagVt % nTag) // the modulo part will be zero for j > 0 because of the next if block, see example below
74+
75+
if (j == 0) {
76+
totalTagVt = totalTagVt - (totalTagVt % nTag) // removing the extra modulo part for all other tags except the first one
77+
}
78+
79+
if (topTags[y].tag === curTag) {
80+
topTags[y].vt += Math.abs(curTagVt)
81+
newTags[curTag] = null // tag already exists, so nullifying pre-pushed tag
82+
} else {
83+
newTags[curTag] = {tag: curTag, vt: Math.abs(curTagVt)}
84+
}
85+
}
86+
/*
87+
Example:
88+
content.votes[i].tag = "hike, hiking, hiker"
89+
totalTagVt = 313
90+
nt = 3
91+
92+
topTags[0] = {tag: "hike", vt: 313/3 + 313%3 = 104 + 1 = 105} // hike
93+
94+
totalTagVt = 313 - (313%3) = 313 - 1 = 312
95+
96+
topTags[1] = {tag: "hiking", vt: 312/3 + 312%3 = 104} // hiking
97+
98+
topTags[2] = {tag: "hiker", vt: 312/3 + 312%3 = 104} // hiker
99+
100+
105 + 104 + 104 = 313 total
101+
*/
102+
}
103+
104+
for (let [tagKey, tagVal] of newTags) {
105+
if (!tagVal && tagVal.tag) {// if tagKey not null, then exists false, then push
106+
topTags.push(tagVal)
107+
}
108+
}
64109
}
65110

66111
topTags = topTags.sort(function(a,b) {

0 commit comments

Comments
 (0)