1 import React from 'react';
2 import Icon from '../components/common/Icon';
4 export const compareResult = (a, b) => {
5 const a_placement = a && a.placement ? a.placement : 0;
6 const b_placement = b && b.placement ? b.placement : 0;
9 if (a_placement < b_placement) return -1;
10 if (b_placement < a_placement) return 1;
11 return compareUsername(a, b);
18 return compareUsername(a, b);
21 export const compareUsername = (a, b) => {
22 const a_name = (a && a.user && a.user.username) || '';
23 const b_name = (b && b.user && b.user.username) || '';
24 return a_name.localeCompare(b_name);
27 export const formatTime = result => {
28 const hours = `${Math.floor(result.time / 60 / 60)}`;
29 let minutes = `${Math.floor((result.time / 60) % 60)}`;
30 let seconds = `${Math.floor(result.time % 60)}`;
31 while (minutes.length < 2) {
32 minutes = `0${minutes}`;
34 while (seconds.length < 2) {
35 seconds = `0${seconds}`;
37 return `${hours}:${minutes}:${seconds}`;
40 export const getIcon = (result, maySee) => {
41 if (!result || !result.has_finished) {
42 return <Icon.PENDING className="text-muted" size="lg" />;
44 if (result.forfeit && maySee) {
45 return <Icon.FORFEIT className="text-danger" size="lg" />;
47 if (result.placement === 1 && maySee) {
48 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
50 if (result.placement === 2 && maySee) {
51 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
53 if (result.placement === 3 && maySee) {
54 return <Icon.THIRD_PLACE className="text-bronze" size="lg" />;
56 return <Icon.FINISHED className="text-success" size="lg" />;
59 export const getTime = (result, maySee) => {
60 if (!result || !maySee) {
64 return formatTime(result);
72 export const parseTime = str => {
73 if (!str) return null;
74 return `${str}`.split(/[-.: ]+/).reduce((acc,time) => (60 * acc) + +time, 0);