diff --git a/typescript-backend-katas/src/streetFighter/dataAccess/db.spec.ts b/typescript-backend-katas/src/streetFighter/dataAccess/db.spec.ts new file mode 100644 index 0000000..acf82fc --- /dev/null +++ b/typescript-backend-katas/src/streetFighter/dataAccess/db.spec.ts @@ -0,0 +1,62 @@ +import { Match } from "../models/models"; +import { getMatch, getMatches } from "./db"; +import * as path from "path"; + +describe("getMatch", () => { + it('should parse a string from the CSV to get a match', async () => { + const input: string = "1,chun-li,2,1,sagat"; + const match: Match = getMatch(input); + + const expected: Match = { + fighter1: "chun-li", + fighter2: "sagat", + winner: "chun-li" + } + expect(match).toEqual(expected); + }) + it('should parse a string from the CSV to get another match', async () => { + const input: string = "2,chun-li,1,2,ryu"; + const match: Match = getMatch(input); + + const expected: Match = { + fighter1: "chun-li", + fighter2: "ryu", + winner: "ryu" + } + expect(match).toEqual(expected); + }) + it('should parse a string from the CSV to get a draw', async () => { + const input: string = "2,chun-li,2,2,ryu"; + const match: Match = getMatch(input); + + const expected: Match = { + fighter1: "chun-li", + fighter2: "ryu", + winner: null + } + expect(match).toEqual(expected); + }) +}); + +describe("getMatches", () => { + it('should parse the CSV and get two Matches', async () => { + const input_path = path.resolve("src/streetFighter/dataAccess/testResources/test.csv"); + const matches: Match[] = getMatches(input_path); + + const expected_matches: Match[] = [ + { + fighter1: "chun-li", + fighter2: "sagat", + winner: "chun-li" + }, + { + fighter1: "chun-li", + fighter2: "ryu", + winner: "ryu" + } + ] + expect(matches).toEqual(expected_matches); + }) +}); + + diff --git a/typescript-backend-katas/src/streetFighter/dataAccess/db.ts b/typescript-backend-katas/src/streetFighter/dataAccess/db.ts new file mode 100644 index 0000000..4275534 --- /dev/null +++ b/typescript-backend-katas/src/streetFighter/dataAccess/db.ts @@ -0,0 +1,31 @@ +import { Match } from "../models/models"; +import * as fs from "fs"; + + +export function getMatch(input: string): Match { + const splitString = input.split(","); + + const fighter1 = splitString[1]; + const roundsWon1 = Number(splitString[2]); + const roundsWon2 = Number(splitString[3]); + const fighter2 = splitString[4]; + + let winner = null; + if (roundsWon1 > roundsWon2) { + winner = fighter1; + } else if (roundsWon1 < roundsWon2) { + winner = fighter2; + } + + return{ + fighter1: fighter1, + fighter2: fighter2, + winner: winner, + } +} + +export function getMatches(filePath: string): Match[] { + const allContents = fs.readFileSync(filePath, 'utf-8'); + return allContents.split(/\r?\n/).slice(1).filter(line => line).map(getMatch); +} + diff --git a/typescript-backend-katas/src/streetFighter/dataAccess/testResources/test.csv b/typescript-backend-katas/src/streetFighter/dataAccess/testResources/test.csv new file mode 100644 index 0000000..e9bf407 --- /dev/null +++ b/typescript-backend-katas/src/streetFighter/dataAccess/testResources/test.csv @@ -0,0 +1,3 @@ +timeslot,home,roundsWon1,roundsWon2,away +1,chun-li,2,1,sagat +1,chun-li,1,2,ryu diff --git a/typescript-backend-katas/src/streetFighter/models/models.ts b/typescript-backend-katas/src/streetFighter/models/models.ts new file mode 100644 index 0000000..79ae5fe --- /dev/null +++ b/typescript-backend-katas/src/streetFighter/models/models.ts @@ -0,0 +1,5 @@ +export interface Match { + fighter1: string; + fighter2: string; + winner: string | null; +}