JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdn.steemjs.com/lib/latest/steem.min.js"></script>
<pre id="counter"></pre>
JavaScript
(function() {
// call the main function
whoUpvotedMe('pilcrow').then(result => {
// print the results in the HTML
document.getElementById('counter').innerText = JSON.stringify(result, null, 4);
});
function whoUpvotedMe(username) {
return steem.api.getDiscussionsByAuthorBeforeDate(username, null, '2100-01-01T00:00:00', 100)
.then(posts => {
const allUpvoters = getAllUpvoters(posts);
return countUpvoters(allUpvoters);
});
}
function getUpvoters(post) {
return post.active_votes.map(upvote => upvote.voter);
}
function getAllUpvoters(posts) {
return posts.reduce((all, currentPost) => {
const upvoters = getUpvoters(currentPost)
return all.concat(upvoters);
}, []);
}
function countUpvoters(allUpvoters) {
return allUpvoters.reduce((upvoteCounts, upvoter) => {
upvoteCounts[upvoter] = (upvoteCounts[upvoter] || 0) + 1;
return upvoteCounts;
}, {});
}
})();