-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-refactoring-functions-avoid-else.ts
64 lines (51 loc) · 2.07 KB
/
04-refactoring-functions-avoid-else.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(() => {
// Avoid excesive if else, includes in array is an option
function isRedFruit(fruit: string): boolean {
const redFruits = ['manzana', 'cereza', 'ciruela'];
return redFruits.includes(fruit);
}
// Avoid if else, use literal objects instead
type FruitColor = 'red' | 'yellow' | 'purple';
function getFruitsByColor(color: FruitColor): string[] {
const fruitsByColor = {
red: ['manzana', 'fresa'],
yellow: ['piña', 'banana'],
purple: ['moras', 'uvas'],
};
if (!fruitsByColor[color]) { throw Error('the color must be: red, yellow, purple'); }
return fruitsByColor[color];
// Alternative, using switch statement
// switch (color) {
// case 'red':
// return ['manzana', 'fresa'];
// case 'yellow':
// return ['piña', 'banana'];
// case 'purple':
// return ['moras', 'uvas'];
// default:
// throw Error('the color must be: red, yellow, purple')
// }
}
let isFirstStepWorking = true;
let isSecondStepWorking = true;
let isThirdStepWorking = true;
let isFourthStepWorking = true;
// For simplicity, we could set many exceptions flow on top.
function workingSteps() {
if (!isFirstStepWorking) { return 'First step broken.'; }
if (!isSecondStepWorking) { return 'Second step broken.'; }
if (!isThirdStepWorking) { return 'Third step broken.'; }
if (!isFourthStepWorking) { return 'Fourth step broken.'; }
return 'Working properly!';
}
// isRedFruit
console.log({ isRedFruit: isRedFruit('cereza'), fruit: 'cereza' }); // true
console.log({ isRedFruit: isRedFruit('piña'), fruit: 'piña' }); // true
//getFruitsByColor
console.log({ redFruits: getFruitsByColor('red') }); // ['manzana', 'fresa']
console.log({ yellowFruits: getFruitsByColor('yellow') }); // ['piña', 'banana']
console.log({ purpleFruits: getFruitsByColor('purple') }); // ['moras', 'uvas']
// console.log({ pinkFruits: getFruitsByColor('pink') }); // Error: the color must be: red, yellow, purple
// workingSteps
console.log({ workingSteps: workingSteps() });
})();