Skip to content

Commit 1e4ea34

Browse files
karl-runmikaelrss
andcommitted
implement oppgaves for part 3
Co-authored-by: Mikael Solstad <[email protected]>
1 parent 02bf47e commit 1e4ea34

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 100
3+
}

workshop/oppgaver/01-pure-functions/pure-functions.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
import { Transaction } from "../../createMockData";
1111

1212
import { convertCurrency, isCurrency, takeFirstN } from "./pure-functions";
13-
import { createFilterByMordi } from "../02-higher-order-functions/higher-order-functions";
1413

1514
describe("OPPGAVE 1.1", () => {
1615
it("correctly converts from DKK to SEK", () => {

workshop/oppgaver/03-currying-partial-application/currying-partial-application.test.ts

+12-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import transactions from "../../data/transactions";
44
import { EUR_TO_NOK } from "../constants";
55
import { currencyFilter, toAmount } from "./__spoilers/dontopen";
66

7-
import { filterByCurrency } from "./currying-partial-application";
7+
import { dkkToSek, ninetyDkkInNok } from "./currying-partial-application";
88
import * as CPA from "./currying-partial-application";
99

1010
describe("OPPGAVE 3.1", () => {
@@ -18,38 +18,26 @@ describe("OPPGAVE 3.1", () => {
1818
.slice(0, 5);
1919

2020
expect(result).toEqual([
21-
-176572.8336, -180234.66, -195101.136, -292522.99919999996,
22-
-42670.547999999995,
21+
-176572.8336, -180234.66, -195101.136, -292522.99919999996, -42670.547999999995,
2322
]);
2423
});
2524
});
2625

2726
describe("OPPGAVE 3.2", () => {
27+
it("tell if transaction is the given currency", () => {
28+
const isCurrency = CPA.isCurrencyCurried("NOK");
2829

29-
});
30-
31-
describe("TODO maybe use, maybe garb", () => {
32-
it("filters out all transactions in NOK", () => {
33-
expect(filterByCurrency("NOK", transactions)).toHaveLength(165);
34-
});
35-
36-
it("filters out all transactions in SEK", () => {
37-
expect(filterByCurrency("SEK", transactions)).toHaveLength(150);
38-
});
39-
40-
it("filters out all transactions in DKK", () => {
41-
expect(filterByCurrency("DKK", transactions)).toHaveLength(195);
42-
});
43-
44-
it("filters out all transactions in EUR", () => {
45-
expect(filterByCurrency("EUR", transactions)).toHaveLength(159);
30+
expect(isCurrency(transactions[0])).toBe(true);
31+
expect(isCurrency(transactions[1])).toBe(false);
4632
});
33+
});
4734

48-
it("filters out all transactions in USD", () => {
49-
expect(filterByCurrency("USD", transactions)).toHaveLength(172);
35+
describe("OPPGAVE 3.3", () => {
36+
it("convert from DKK to SEK", () => {
37+
expect(dkkToSek(100)).toBe(154);
5038
});
5139

52-
it("filters out all transactions in GBP", () => {
53-
expect(filterByCurrency("GBP", transactions)).toHaveLength(171);
40+
it("should have the correct NOK", () => {
41+
expect(Number(ninetyDkkInNok.toFixed(1))).toBe(151.3);
5442
});
5543
});

workshop/oppgaver/03-currying-partial-application/currying-partial-application.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,34 @@ Copilot sier:
77

88
import { Transaction } from "../../createMockData";
99
import { Currency } from "../constants";
10+
import * as ExchangeRates from "../constants";
1011

1112
/**
1213
* OPPGAVE 3.1: Gjør om convertCurrency fra oppgave 1 til å være curried.
1314
*/
14-
export const convertCurrency = (exchangeRate: number, amount: number) =>
15-
exchangeRate * amount;
15+
export const convertCurrency = (exchangeRate: number) => (amount: number) => exchangeRate * amount;
1616

1717
/**
1818
* OPPGAVE 3.2: Gjør om isCurrency fra oppgave 1 til å være curried.
1919
*/
20-
21-
// garb???
2220
export const isCurrencyCurried = (currency: Currency) => {
2321
return (transaction: Transaction) => transaction.currency === currency;
2422
};
2523

26-
export const filterByCurrency = (
27-
currency: Currency,
28-
transactions: Transaction[]
29-
) => {
30-
return transactions.filter(isCurrencyCurried(currency));
31-
};
24+
/**
25+
* OPPGAVE 3.3: Bruk din curried versjon av convertCurrency fra oppgave 3.1 til
26+
* å lage en funksjon som konverterer fra DKK til SEK.
27+
*
28+
* Hint: Du finner riktig konverteringsrate i ExchangeRates
29+
*/
30+
31+
type FromCurrencyToCurrency = (currency: number) => number;
32+
export const dkkToSek: FromCurrencyToCurrency = convertCurrency(ExchangeRates.DKK_TO_SEK);
33+
export const sekToUsd: FromCurrencyToCurrency = convertCurrency(ExchangeRates.SEK_TO_USD);
34+
export const usdToGbp: FromCurrencyToCurrency = convertCurrency(ExchangeRates.USD_TO_GBP);
35+
export const gbpToEur: FromCurrencyToCurrency = convertCurrency(ExchangeRates.GBP_TO_EUR);
36+
export const eurToNok: FromCurrencyToCurrency = convertCurrency(ExchangeRates.EUR_TO_NOK);
37+
38+
// skal de skrive dette selv?
39+
export const ninetyDkkInNok = eurToNok(gbpToEur(usdToGbp(sekToUsd(dkkToSek(100)))));
40+
// export const ninetyDkkInNok = undefined;

0 commit comments

Comments
 (0)