Skip to content

Commit eb62df4

Browse files
committed
setup initial examples + test + CI
1 parent 76a4885 commit eb62df4

File tree

8 files changed

+372
-44
lines changed

8 files changed

+372
-44
lines changed

package-lock.json

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"type": "module",
3-
"name": "refactoring-catalog-template",
3+
"name": "replace-conditional-with-polymorphism-refactoring",
44
"version": "1.0.0",
55
"main": "index.js",
6-
"repository": "https://github.com/kaiosilveira/refactoring-catalog-template.git",
6+
"repository": "https://github.com/kaiosilveira/replace-conditional-with-polymorphism-refactoring.git",
77
"author": "Kaio Silveira <silveira.kaio@icloud.com>",
88
"license": "MIT",
99
"scripts": {
@@ -14,4 +14,4 @@
1414
"jest": "^29.0.3",
1515
"@kaiosilveira/refactoring-catalog-cli": "0.7.34"
1616
}
17-
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function plumages(birds) {
2+
return new Map(birds.map(b => [b.name, plumage(b)]));
3+
}
4+
5+
export function speeds(birds) {
6+
return new Map(birds.map(b => [b.name, airSpeedVelocity(b)]));
7+
}
8+
9+
export function plumage(bird) {
10+
switch (bird.type) {
11+
case 'EuropeanSwallow':
12+
return 'average';
13+
case 'AfricanSwallow':
14+
return bird.numberOfCoconuts > 2 ? 'tired' : 'average';
15+
case 'NorwegianBlueParrot':
16+
return bird.voltage > 100 ? 'scorched' : 'beautiful';
17+
default:
18+
return 'unknown';
19+
}
20+
}
21+
22+
export function airSpeedVelocity(bird) {
23+
switch (bird.type) {
24+
case 'EuropeanSwallow':
25+
return 35;
26+
case 'AfricanSwallow':
27+
return 40 - 2 * bird.numberOfCoconuts;
28+
case 'NorwegianBlueParrot':
29+
return bird.isNailed ? 0 : 10 + bird.voltage / 10;
30+
default:
31+
return null;
32+
}
33+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { speeds, airSpeedVelocity, plumage, plumages } from './index';
2+
3+
describe('plumage', () => {
4+
it('should return "average" for EuropeanSwallow', () => {
5+
const bird = { type: 'EuropeanSwallow' };
6+
expect(plumage(bird)).toBe('average');
7+
});
8+
9+
it('should return "tired" for AfricanSwallow with more than 2 coconuts', () => {
10+
const bird = { type: 'AfricanSwallow', numberOfCoconuts: 3 };
11+
expect(plumage(bird)).toBe('tired');
12+
});
13+
14+
it('should return "average" for AfricanSwallow with 2 or less coconuts', () => {
15+
const bird = { type: 'AfricanSwallow', numberOfCoconuts: 2 };
16+
expect(plumage(bird)).toBe('average');
17+
});
18+
19+
it('should return "scorched" for NorwegianBlueParrot with voltage greater than 100', () => {
20+
const bird = { type: 'NorwegianBlueParrot', voltage: 101 };
21+
expect(plumage(bird)).toBe('scorched');
22+
});
23+
24+
it('should return "beautiful" for NorwegianBlueParrot with voltage 100 or less', () => {
25+
const bird = { type: 'NorwegianBlueParrot', voltage: 100 };
26+
expect(plumage(bird)).toBe('beautiful');
27+
});
28+
29+
it('should return "unknown" for unknown bird type', () => {
30+
const bird = { type: 'Unknown' };
31+
expect(plumage(bird)).toBe('unknown');
32+
});
33+
});
34+
35+
describe('plumages', () => {
36+
it('should return a map containing the plumage of all birds in the list', () => {
37+
const birds = [
38+
{ name: 'bird1', type: 'EuropeanSwallow' },
39+
{ name: 'bird2', type: 'AfricanSwallow', numberOfCoconuts: 1 },
40+
{ name: 'bird3', type: 'AfricanSwallow', numberOfCoconuts: 3 },
41+
{ name: 'bird4', type: 'NorwegianBlueParrot', voltage: 100 },
42+
{ name: 'bird5', type: 'NorwegianBlueParrot', voltage: 101 },
43+
{ name: 'bird6', type: 'Unknown' },
44+
];
45+
46+
const result = plumages(birds);
47+
48+
expect(result.size).toBe(6);
49+
expect(result.get('bird1')).toBe(plumage(birds[0]));
50+
expect(result.get('bird2')).toBe(plumage(birds[1]));
51+
expect(result.get('bird3')).toBe(plumage(birds[2]));
52+
expect(result.get('bird4')).toBe(plumage(birds[3]));
53+
expect(result.get('bird5')).toBe(plumage(birds[4]));
54+
expect(result.get('bird6')).toBe(plumage(birds[5]));
55+
});
56+
});
57+
58+
describe('airSpeedVelocity', () => {
59+
it('should return 35 for EuropeanSwallow', () => {
60+
const bird = { type: 'EuropeanSwallow' };
61+
expect(airSpeedVelocity(bird)).toBe(35);
62+
});
63+
64+
it('should return 38 for AfricanSwallow with 1 coconut', () => {
65+
const bird = { type: 'AfricanSwallow', numberOfCoconuts: 1 };
66+
expect(airSpeedVelocity(bird)).toBe(38);
67+
});
68+
69+
it('should return 36 for AfricanSwallow with 2 coconuts', () => {
70+
const bird = { type: 'AfricanSwallow', numberOfCoconuts: 2 };
71+
expect(airSpeedVelocity(bird)).toBe(36);
72+
});
73+
74+
it('should return 0 for nailed NorwegianBlueParrot', () => {
75+
const bird = { type: 'NorwegianBlueParrot', isNailed: true };
76+
expect(airSpeedVelocity(bird)).toBe(0);
77+
});
78+
79+
it('should return 10 for NorwegianBlueParrot with voltage 0', () => {
80+
const bird = { type: 'NorwegianBlueParrot', voltage: 0 };
81+
expect(airSpeedVelocity(bird)).toBe(10);
82+
});
83+
84+
it('should return 20 for NorwegianBlueParrot with voltage 100', () => {
85+
const bird = { type: 'NorwegianBlueParrot', voltage: 100 };
86+
expect(airSpeedVelocity(bird)).toBe(20);
87+
});
88+
});
89+
90+
describe('speeds', () => {
91+
it('should return a map containing the air speed velocity of all birds in the list', () => {
92+
const birds = [
93+
{ name: 'bird1', type: 'EuropeanSwallow' },
94+
{ name: 'bird2', type: 'AfricanSwallow', numberOfCoconuts: 1 },
95+
{ name: 'bird3', type: 'AfricanSwallow', numberOfCoconuts: 2 },
96+
{ name: 'bird4', type: 'NorwegianBlueParrot', voltage: 100 },
97+
{ name: 'bird5', type: 'NorwegianBlueParrot', voltage: 50 },
98+
{ name: 'bird6', type: 'Unknown' },
99+
];
100+
101+
const result = speeds(birds);
102+
103+
expect(result.size).toBe(6);
104+
expect(result.get('bird1')).toBe(35);
105+
expect(result.get('bird2')).toBe(38);
106+
expect(result.get('bird3')).toBe(36);
107+
expect(result.get('bird4')).toBe(20);
108+
expect(result.get('bird5')).toBe(15);
109+
expect(result.get('bird6')).toBe(null);
110+
});
111+
});

src/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/index.test.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export function rating(voyage, history) {
2+
const vpf = voyageProfitFactor(voyage, history);
3+
const vr = voyageRisk(voyage);
4+
const chr = captainHistoryRisk(voyage, history);
5+
if (vpf * 3 > vr + chr * 2) return 'A';
6+
else return 'B';
7+
}
8+
9+
export function voyageRisk(voyage) {
10+
let result = 1;
11+
if (voyage.length > 4) result += 2;
12+
if (voyage.length > 8) result += voyage.length - 8;
13+
if (['china', 'east-indies'].includes(voyage.zone)) result += 4;
14+
return Math.max(result, 0);
15+
}
16+
17+
export function captainHistoryRisk(voyage, history) {
18+
let result = 1;
19+
if (history.length < 5) result += 4;
20+
result += history.filter(v => v.profit < 0).length;
21+
if (voyage.zone === 'china' && hasChina(history)) result -= 2;
22+
return Math.max(result, 0);
23+
}
24+
25+
export function hasChina(history) {
26+
return history.some(v => 'china' === v.zone);
27+
}
28+
29+
export function voyageProfitFactor(voyage, history) {
30+
let result = 2;
31+
if (voyage.zone === 'china') result += 1;
32+
if (voyage.zone === 'east-indies') result += 1;
33+
if (voyage.zone === 'china' && hasChina(history)) {
34+
result += 3;
35+
if (history.length > 10) result += 1;
36+
if (voyage.length > 12) result += 1;
37+
if (voyage.length > 18) result -= 1;
38+
} else {
39+
if (history.length > 8) result += 1;
40+
if (voyage.length > 14) result -= 1;
41+
}
42+
return result;
43+
}

0 commit comments

Comments
 (0)