Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
FinnIckler committed Feb 18, 2024
1 parent e42fea9 commit 4ba7b8c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
30 changes: 15 additions & 15 deletions src/helpers/result.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AttemptResult } from '../models/attemptResult';
import {centiSecondsToHumanReadable, formatCentiseconds} from './time';
import {EventId} from "../models";
import {getEventResultType} from "./event";
import { centiSecondsToHumanReadable, formatCentiseconds } from './time';
import { EventId } from '../models';
import { getEventResultType } from './event';

type DnfMultiResult = { isDnf: true };
type DnsMultiResult = { isDns: true };
Expand Down Expand Up @@ -124,26 +124,26 @@ function decodeNewMultiResult(result: AttemptResult): DecodedMultiResult {
}

interface AttemptResultToStringParams {
attemptResult: number
eventId: EventId
attemptResult: number;
eventId: EventId;
}

function attemptResultToMbPoints(mbValue: number) {
const { solved, attempted } = decodeMultiResult(mbValue)
const missed = attempted - solved
return solved - missed
const { solved, attempted } = decodeMultiResult(mbValue);
const missed = attempted - solved;
return solved - missed;
}

export function attemptResultToString({
attemptResult,
eventId,
}: AttemptResultToStringParams) {
const type = getEventResultType(eventId)
attemptResult,
eventId,
}: AttemptResultToStringParams) {
const type = getEventResultType(eventId);
if (type === 'time') {
return centiSecondsToHumanReadable({ c: attemptResult })
return centiSecondsToHumanReadable({ c: attemptResult });
}
if (type === 'number') {
return `${attemptResult} moves`
return `${attemptResult} moves`;
}
return `${attemptResultToMbPoints(attemptResult)} points`
return `${attemptResultToMbPoints(attemptResult)} points`;
}
64 changes: 32 additions & 32 deletions src/helpers/time.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getEventResultType} from "./event";
import {EventId} from "../models";
import {decodeMultiResult} from "./result";
import { getEventResultType } from './event';
import { EventId } from '../models';
import { decodeMultiResult } from './result';

export function formatCentiseconds(centiTime: number): string {
if (centiTime === -1) {
Expand All @@ -25,69 +25,69 @@ function prefix(n: number): string {
return `${n}`;
}

export const SECOND_IN_CS = 100
export const MINUTE_IN_CS = 60 * SECOND_IN_CS
export const HOUR_IN_CS = 60 * MINUTE_IN_CS
export const SECOND_IN_CS = 100;
export const MINUTE_IN_CS = 60 * SECOND_IN_CS;
export const HOUR_IN_CS = 60 * MINUTE_IN_CS;

interface PluralizeParams {
count: number
word: string
count: number;
word: string;
options?: {
fixed?: number
abbreviate?: boolean
}
fixed?: number;
abbreviate?: boolean;
};
}

function pluralize({ count, word, options = {} }: PluralizeParams) {
const countStr =
options.fixed && count % 1 > 0 ? count.toFixed(options.fixed) : count
options.fixed && count % 1 > 0 ? count.toFixed(options.fixed) : count;
const countDesc = options.abbreviate
? word[0]
: ` ${count === 1 ? word : `${word}s`}`
return countStr + countDesc
? word[0]
: ` ${count === 1 ? word : `${word}s`}`;
return countStr + countDesc;
}

interface CentiSecondsToHumanReadableParams {
c: number
c: number;
options?: {
short?: boolean
}
short?: boolean;
};
}
export function centiSecondsToHumanReadable({
c,
options = {},
}: CentiSecondsToHumanReadableParams) {
let centiseconds = c
let str = ''
c,
options = {},
}: CentiSecondsToHumanReadableParams) {
let centiseconds = c;
let str = '';

const hours = centiseconds / HOUR_IN_CS
centiseconds %= HOUR_IN_CS
const hours = centiseconds / HOUR_IN_CS;
centiseconds %= HOUR_IN_CS;
if (hours >= 1) {
str += `${pluralize({
count: Math.floor(hours),
word: 'hour',
options: { abbreviate: options.short },
})} `
})} `;
}

const minutes = centiseconds / MINUTE_IN_CS
centiseconds %= MINUTE_IN_CS
const minutes = centiseconds / MINUTE_IN_CS;
centiseconds %= MINUTE_IN_CS;
if (minutes >= 1) {
str += `${pluralize({
count: Math.floor(minutes),
word: 'minute',
options: { abbreviate: options.short },
})} `
})} `;
}

const seconds = centiseconds / SECOND_IN_CS
const seconds = centiseconds / SECOND_IN_CS;
if (seconds > 0 || str.length === 0) {
str += `${pluralize({
count: seconds,
word: 'second',
options: { fixed: 2, abbreviate: options.short },
})} `
})} `;
}

return str.trim()
return str.trim();
}

0 comments on commit 4ba7b8c

Please sign in to comment.