Skip to content

Commit

Permalink
Temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendrikse committed Nov 2, 2023
1 parent fc729ca commit 0c5bb69
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { CountryList } = require('../src/CountryList');
const { Country } = require('../src/Country')
const { Country } = require('../src/Country');

const NETHERLANDS = new Country("Netherlands", "Amsterdam", 4)
const PORTUGAL = new Country("Portugal", "Lissabon", 7)
Expand All @@ -20,11 +20,27 @@ class CountriesInputAdapterStub {
}
}

class MockCountriesOutputAdapter {
write(countrList) {
let csvContent = "";

countrList.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});

expect(csvContent).toEqual("Belgium,Brussels,3,1.1\r\n" +
"Netherlands,Amsterdam,4,0.73\r\n" +
"Portugal,Lissabon,7,0.37\r\n" +
"United Kingdom,London,10,1.46\r\n");
}
}

describe('A list without countries (empty list)', function () {
let countryList;

beforeEach(function() {
countryList = new CountryList(new EmptyCountriesInputAdapterStub());
countryList = new CountryList(new EmptyCountriesInputAdapterStub(), new MockCountriesOutputAdapter());
});

it('should sort the empty list', function () {
Expand All @@ -45,7 +61,7 @@ describe('A list with countries', function () {
let countryList;

beforeEach(function() {
countryList = new CountryList(new CountriesInputAdapterStub());
countryList = new CountryList(new CountriesInputAdapterStub(), new MockCountriesOutputAdapter());
});

it('should sort the countries by population size', function () {
Expand Down Expand Up @@ -75,4 +91,8 @@ describe('A list with countries', function () {
["United Kingdom", "London", 10, 1.46]]
expect(countryList.as_nested_array()).toEqual(expected_output);
});

it('writes the country data to CSV', function() {
countryList.to_csv();
})
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
'use strict';
const fs = require('fs');

const { Country } = require('../src/Country');

const userAction = async () => {
const response = fetch('https://restcountries.com/v3.1/all?fields=name,capital,population,cioc,region');
const responseAsJson = await response.json();
let restCountries = JSON.parse(decodeURIComponent(responseAsJson));
let countries = restCountries.map(country => new Country(country.name.common, country.capital[0], country.population));
return countries;
}

const RESPONSE = "[{\"name\":{\"common\":\"Turks and Caicos Islands\",\"official\":\"Turks and Caicos Islands\",\"nativeName\":{\"eng\":{\"official\":\"Turks and Caicos Islands\",\"common\":\"Turks and Caicos Islands\"}}},\"cioc\":\"\",\"capital\":[\"Cockburn Town\"],\"region\":\"Americas\",\"population\":38718}" +
",{\"name\":{\"common\":\"Nigeria\",\"official\":\"Federal Republic of Nigeria\",\"nativeName\":{\"eng\":{\"official\":\"Federal Republic of Nigeria\",\"common\":\"Nigeria\"}}},\"cioc\":\"NGR\",\"capital\":[\"Abuja\"],\"region\":\"Africa\",\"population\":206139587}]";


class RestCountriesInputAdapter {
load_all() {
return [];
console.log("Start")

async(userAction) => {
console.log("Voer request uit");
console.log(await userAction());
}

console.log("Done")

let restCountries1 = JSON.parse(decodeURIComponent(RESPONSE));
let countries1 = restCountries1.map(country => new Country(country.name.common, country.capital[0], country.population));
return countries1;
}
}

class CountriesOutputAdapter {
write(countrList) {
let csvContent = "";

countrList.forEach(function (rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});

fs.writeFileSync('countries.csv', csvContent, (err) => {
if (err) throw err;
console.log('countries.csv saved.');
});
}
}

class CountryList {
constructor(inputPort = new RestCountriesInputAdapter()) {
constructor(inputPort = new RestCountriesInputAdapter(), outputPort = new CountriesOutputAdapter()) {
this.countryList = inputPort.load_all();
this.outputPort = outputPort;
}

compare(country1, country2) {
Expand Down Expand Up @@ -47,10 +90,23 @@ class CountryList {

as_nested_array() {
var sortedCountries = this.sorted_by_population();
return sortedCountries.map(country => [country.name, country.capital, country.population, this.standard_deviations_for(country)]);
return sortedCountries.map(country => [country.name, country.capital, country.population, this.standard_deviations_for(country)]);
}

to_csv() {
this.outputPort.write(this.as_nested_array());
}
}

module.exports = {
CountryList: CountryList
}

async function main() {
let countryList = new CountryList()
countryList.to_csv();
}

if (require.main === module) {
main();
}
13 changes: 6 additions & 7 deletions kata-solutions/stack-kata/stack-kata-javascript/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link rel="stylesheet" href="jasmine.css"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.6.1/jasmine.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.6.1/jasmine-html.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.6.1/boot.js"></script>
<title>Stack specs</title>
<link rel="stylesheet" href="jasmine.css"/>
<script type="text/javascript" src="./node_modules/jasmine/lib/jasmine.js"></script>
<script type="text/javascript" src="./node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
</head>
<body>
<script src="stack.js"></script>
<script src="stackSpecs.js"></script>
<script type="text/javascript" src="src/stack.js"></script>
<script type="text/javascript" src="spec/stack_spec.js"></script>
</body>
</html>
Loading

0 comments on commit 0c5bb69

Please sign in to comment.