X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=resources%2Fjs%2Fhelpers%2FParticipant.js;h=e5a135f1b14c47f09182898f0d92aa8aaf5cfa13;hb=d920d896d4357289112e31d3b33d386ab754a12d;hp=af6eecc4d13aedf4f7aba5765a7218a9ee408410;hpb=edd0e97bfdc544114f30bf4c13a929631c44a555;p=alttp.git diff --git a/resources/js/helpers/Participant.js b/resources/js/helpers/Participant.js index af6eecc..e5a135f 100644 --- a/resources/js/helpers/Participant.js +++ b/resources/js/helpers/Participant.js @@ -1,9 +1,43 @@ +export const compareResult = round => (a, b) => { + const a_result = findResult(a, round); + const b_result = findResult(b, round); + const a_time = a_result ? a_result.time : 0; + const b_time = b_result ? b_result.time : 0; + if (a_time) { + if (b_time) { + if (a_time < b_time) return -1; + if (b_time < a_time) return 1; + return 0; + } + return -1; + } + if (b_time) { + return 1; + } + return 0; +}; + +export const compareUsername = (a, b) => { + const a_name = a && a.user && a.user.username ? a.user.username : ''; + const b_name = b && b.user && b.user.username ? b.user.username : ''; + return a_name.localeCompare(b_name); +}; + export const findResult = (participant, round) => { if (!participant || !participant.user_id) return null; if (!round || !round.results || !round.results.length) return null; return round.results.find(result => result.user_id === participant.user_id); }; +export const sortByResult = (participants, round) => { + if (!participants || !participants.length) return participants; + if (!round || !round.results || !round.results.length) return participants; + return participants.sort(compareResult(round)); +}; + export default { + compareResult, + compareUsername, findResult, + sortByResult, };