Skip to content
Permalink
Browse files

Merge pull request #4 from afewminutesofcode/csb-1567001355043

code for part 4
  • Loading branch information
afewminutesofcode committed Aug 28, 2019
2 parents 4a0453e + f4d176c commit 0aab167dba83cf1c399eac87b35fc4bda2a0673b
@@ -0,0 +1,12 @@
import { calcNRR } from "../points-table/calcNRR";

it("calcNRR for IND", () => {
expect(
calcNRR({
runsFor: 2260,
oversFor: 381.0,
runsAgainst: 1998,
oversAgainst: 390.0
})
).toBe("0.809");
});
@@ -0,0 +1,11 @@
import { calcOver } from "../helpers/calcOver";

it("calcOver", () => {
expect(calcOver("1")).toBe(1);
expect(calcOver("1.0")).toBe(1);
expect(calcOver("1.1")).toBe(1.167);
expect(calcOver("1.2")).toBe(1.333);
expect(calcOver("1.3")).toBe(1.5);
expect(calcOver("1.4")).toBe(1.667);
expect(calcOver("1.5")).toBe(1.833);
});
@@ -0,0 +1,28 @@
import { cricketPointsTableStats } from "../points-table/cricketPointsTableStats";
import { matches } from "../data/matches";

const config = { pts4Win: 2, pts4NR: 1 };

const expectedData = {
lost: 1,
netrr: "0.809",
noresult: 1,
oversAgainst: 390,
oversFor: 381,
played: 9,
pts: 15,
runsAgainst: 1998,
runsFor: 2260,
team: "IND",
won: 7
};

it("cricketPointsTableStats IND", () => {
expect(
cricketPointsTableStats({
matches,
team: "IND",
config
})
).toMatchObject(expectedData);
});
@@ -0,0 +1,65 @@
import { cricketTeamStat } from "../points-table/cricketTeamStat";
import { matches } from "../data/matches";
import { SHOW_TEAM } from "../helpers/constants";

it("cricketTeamStat total Played", () => {
expect(
cricketTeamStat({
matches,
teamNum: "t1",
team: "IND",
stat: "fin",
opp: SHOW_TEAM,
filterTeam: true
}) +
cricketTeamStat({
matches,
teamNum: "t2",
team: "IND",
stat: "fin",
opp: SHOW_TEAM,
filterTeam: true
})
).toBe(9);
});

it("cricketTeamStat total Runs", () => {
expect(
cricketTeamStat({
matches,
teamNum: "t1",
team: "IND",
stat: "Ru",
opp: SHOW_TEAM,
filterTeam: false
}) +
cricketTeamStat({
matches,
teamNum: "t2",
team: "IND",
stat: "Ru",
opp: SHOW_TEAM,
filterTeam: false
})
).toBe(2260);
});
it("cricketTeamStat total Overs", () => {
expect(
cricketTeamStat({
matches,
teamNum: "t1",
team: "IND",
stat: "Ov",
opp: SHOW_TEAM,
filterTeam: false
}) +
cricketTeamStat({
matches,
teamNum: "t2",
team: "IND",
stat: "Ov",
opp: SHOW_TEAM,
filterTeam: false
})
).toBe(381);
});
@@ -0,0 +1,14 @@
import { splitOver } from "./splitOver";
import { makeFloat } from "./makeFloat";

export const calcOver = overStr => {
/*
calc nrr overs here
(1): 0.167 (2): 0.333 (3): 0.500 (4): 0.667 (5): 0.833
*/
const over = Number(splitOver(overStr)[0]);
const balls = Number(splitOver(overStr)[1]);
const ballsCalculated =
balls > 0 ? (balls / 6).toFixed(3) : balls;
return over + makeFloat(ballsCalculated);
};
@@ -0,0 +1,29 @@
import { cricketTeamStat } from "../points-table/cricketTeamStat";
import { TEAM1, TEAM2 } from "./constants";

export const cricketTeamTotal = ({
matches,
team,
stat,
opp,
filterTeam
}) => {
return (
cricketTeamStat({
matches,
teamNum: TEAM1,
team,
stat,
opp,
filterTeam
}) +
cricketTeamStat({
matches,
teamNum: TEAM2,
team,
stat,
opp,
filterTeam
})
);
};
@@ -0,0 +1 @@
export const makeFloat = str => parseFloat(str) || 0;
@@ -0,0 +1 @@
export const splitOver = over => (over + "").split(".");
@@ -2,7 +2,6 @@ import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
return (
<div className="App">
@@ -0,0 +1,10 @@
export const calcNRR = ({
runsFor,
oversFor,
runsAgainst,
oversAgainst
}) => {
const forTotal = runsFor / oversFor;
const againstTotal = runsAgainst / oversAgainst;
return (forTotal - againstTotal).toFixed(3);
};
@@ -0,0 +1,85 @@
import { cricketTeamTotal } from "../helpers/cricketTeamTotal";
import {
SHOW_TEAM,
SHOW_OPP,
WON,
LOST,
POINTS
} from "../helpers/constants";
import { cricketTeamTotalCalc } from "./cricketTeamTotalCalc";
import { calcNRR } from "./calcNRR";
export const cricketPointsTableStats = ({
matches,
team,
config
}) => {
const runsFor = cricketTeamTotal({
matches,
team,
stat: "Ru",
opp: SHOW_TEAM
});
const oversFor = cricketTeamTotal({
matches,
team,
stat: "Ov",
opp: SHOW_TEAM
});
const runsAgainst = cricketTeamTotal({
matches,
team,
stat: "Ru",
opp: SHOW_OPP
});
const oversAgainst = cricketTeamTotal({
matches,
team,
stat: "Ov",
opp: SHOW_OPP
});
return {
team: team,
played: cricketTeamTotal({
matches,
team,
stat: "fin",
opp: SHOW_TEAM,
filterTeam: true
}),
won: cricketTeamTotalCalc({
config,
matches,
team,
stat: WON,
SHOW_TEAM
}),
lost: cricketTeamTotalCalc({
config,
matches,
team,
stat: LOST
}),
noresult: cricketTeamTotal({
matches,
team,
stat: "NR",
opp: SHOW_TEAM
}),
runsFor: runsFor,
oversFor: oversFor,
runsAgainst: runsAgainst,
oversAgainst: oversAgainst,
netrr: calcNRR({
runsFor,
oversFor,
runsAgainst,
oversAgainst
}),
pts: cricketTeamTotalCalc({
config,
matches,
team,
stat: POINTS
})
};
};
@@ -0,0 +1,31 @@
import { calcOver } from "../helpers/calcOver";
import { getOppTeam } from "../helpers/getOppTeam";
export const cricketTeamStat = ({
matches,
teamNum = "t1",
team,
stat,
opp,
filterTeam = false
}) => {
const oppTeam = getOppTeam(teamNum);
const showTeam = opp === true ? oppTeam : teamNum;
const statName = filterTeam
? `${stat}`
: `${showTeam}${stat}`;
const statTotal = matches
.filter(
match =>
match[teamNum] === team &&
match["type"] === 1 &&
match["fin"] === 1
)
.reduce((total, match) => {
const value =
stat === "Ov"
? calcOver(match[statName])
: match[statName];
return total + value;
}, 0);
return statTotal;
};

0 comments on commit 0aab167

Please sign in to comment.
You can’t perform that action at this time.