-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleResponsibility.js
144 lines (113 loc) · 2.88 KB
/
singleResponsibility.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* SOLID principle. This is the S. Single Resposibility.
FP encourages the Single Resposibility.
This is the used-so-often-its-threadbare example...
Instead of writing one big function called validateInputs
*/
// Not using Single Responsibility
const validateFormValues = formValues => {
const { name, age } = formValues
if (!name) {
//...does some validation
return false
}
if (!age) {
//...does some validation
return false
}
return true
}
// Using Single Responsibility
// Functions created to validate different things
const isValidName = name => {
if (!name) {
//...does some validation
}
}
const isValidAge = age => {
if (!age) {
//...does some validation
}
}
// Consolidates the validations
function validateFormValues(formValues) {
const { name, age } = formValues
if (!isValidFullName(name)) {
return false
}
if (!isValidAge(age)) {
return false
}
return true
}
// Another example takes this further...
/* This code takes an array of numbers represented as strings.
It then transforms the strings into integers.
If a number is less than 100 then it will add that to another array
and eventually output the result
*/
const data = ["99", "100", "999"]
let result = [];
for (let i = 0; i < data.length; i++) {
const num = parseInt(data[i]);
if (num > 100) {
result.push(num);
}
}
// This code does exactly the same thing as the code above
const data = ["99", "100", "999"]
const stringToInt = str => parseInt(str)
const moreThan = compareTo => num => num < compareTo;
const result = data.map(stringToInt).filter(moreThan(100));
/* Code to find organic tea in a list of teas.
Only get teas that have a sustainability rating of more than 8
*/
const teaList = [{
name: "Strawberry and Potassium",
sustainability: 10,
isOrganic: true
}, {
name: "Plastic Loose Leaf",
sustainability: 1,
isOrganic: false
},
{
name: "Tumeric and Sand",
sustainability: 8,
isOrganic: true
},
{
name: "Lemongrass and Dolphin",
sustainability: 3,
isOrganic: true
}];
let result = [];
for (let i = 0; i < teaList.length; i++) {
if (teaList[i].isOrganic === true) {
if (teaList[i].sustainability >= 8) {
result.push(teaList[i])
}
}
}
// Or....
const teaList = [{
name: "Strawberry and Potassium",
sustainability: 10,
isOrganic: true
}, {
name: "Plastic Loose Leaf",
sustainability: 1,
isOrganic: false
},
{
name: "Tumeric and Sand",
sustainability: 8,
isOrganic: true
},
{
name: "Lemongrass and Dolphin",
sustainability: 3,
isOrganic: true
}];
const isOrganic = tea => tea.isOrganic
const sustainabilityRating = rating => teas => teas.sustainability >= rating
const bestTeas = teaList.filter(isOrganic).filter(sustainabilityRating(8));