ensl_gathers/app/javascripts/helper.js

76 lines
1.6 KiB
JavaScript
Raw Normal View History

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
2016-01-22 21:39:03 +00:00
const rankVotes = exports.rankVotes = function (votes, candidates) {
2015-08-08 19:14:16 +00:00
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
2016-01-22 21:39:03 +00:00
const enslUrl = exports.enslUrl = (gatherer) => {
2015-10-18 15:23:39 +00:00
return `http://www.ensl.org/users/${gatherer.id}`
};
2016-01-22 21:39:03 +00:00
const hiveUrl = exports.hiveUrl = (gatherer) => {
const hiveId = gatherer.user.hive.id;
2015-10-18 15:23:39 +00:00
if (hiveId) {
2016-01-22 21:39:03 +00:00
return `http://hive.naturalselection2.com/profile/${hiveId}`;
2015-10-18 15:23:39 +00:00
} else {
return null;
}
};
2016-01-22 21:39:03 +00:00
const modalId = exports.modalId = (user) => {
return `user-modal-${user.id}`;
2015-10-29 19:50:38 +00:00
};
2016-01-22 21:39:03 +00:00
const storageAvailable = exports.storageAvailable = (type) => {
2015-10-29 19:50:38 +00:00
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
2016-01-22 21:39:03 +00:00
};