ensl_gathers/lib/react/helper.jsx

77 lines
1.5 KiB
React
Raw Normal View History

2015-08-08 19:14:16 +00:00
"use strict";
2015-10-22 09:34:40 +00:00
// Accepts an array of IDs voted
// 1. Creates an array of tally objects,
// with ID as prop and vote count as val { 12: 0 }
// 2. Increments ID vote tally for every vote
// 3. Sorts
2015-08-08 19:14:16 +00:00
var rankVotes = function (votes, candidates) {
var initial = candidates.reduce(function (acc, candidate) {
acc[candidate.id] = 0;
return acc;
}, {});
var scores = votes.reduce(function (acc, id) {
if (acc[id] !== undefined) {
acc[id]++;
}
return acc;
}, initial);
var rank = [];
for (var id in scores) {
if (scores.hasOwnProperty(id)) {
rank.push({
id: parseInt(id, 10),
count: scores[id]
});
}
}
return rank.sort(function (a, b) {
2015-10-22 09:34:40 +00:00
if (b.count === a.count) {
return b.id - a.id;
} else {
return b.count - a.count;
}
2015-08-08 19:14:16 +00:00
}).map(function (tally) {
return tally.id
}).map(function (id) {
return candidates.reduce(function (acc, candidate) {
if (candidate.id === id) return candidate;
return acc;
});
});
};
2015-10-02 14:53:11 +00:00
2015-10-18 15:23:39 +00:00
var enslUrl = (gatherer) => {
return `http://www.ensl.org/users/${gatherer.id}`
};
var hiveUrl = (gatherer) => {
let hiveId = gatherer.user.hive.id;
if (hiveId) {
return `http://hive.naturalselection2.com/profile/${hiveId}`
} else {
return null;
}
};
var modalId = (user) => {
return `user-modal-${user.id}`
2015-10-29 19:50:38 +00:00
};
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
}