-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperFunctions.js
More file actions
47 lines (39 loc) · 1.27 KB
/
helperFunctions.js
File metadata and controls
47 lines (39 loc) · 1.27 KB
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
const sortElements = (responses, sortOptions, sortBy, direction) => {
switch (sortOptions.findIndex((option) => option == sortBy)) {
case 0:
if (direction == "asc")
responses.sort((postA, postB) => postA.id - postB.id);
else responses.sort((postA, postB) => postB.id - postA.id);
break;
case 1:
if (direction == "asc")
responses.sort((postA, postB) => postA.reads - postB.reads);
else responses.sort((postA, postB) => postB.reads - postA.reads);
break;
case 2:
if (direction == "asc")
responses.sort((postA, postB) => postA.likes - postB.likes);
else responses.sort((postA, postB) => postB.likes - postA.likes);
break;
case 3:
if (direction == "asc")
responses.sort((postA, postB) => postA.popularity - postB.popularity);
else
responses.sort((postA, postB) => postB.popularity - postA.popularity);
break;
}
return responses;
};
const removeRepeatedElements = (responses) => {
responses.reduce((first, second) => {
if (!first.some((obj) => obj.id === second.id)) {
first.push(second);
}
return first;
}, []);
return responses;
};
module.exports = {
sortElements: sortElements,
removeRepeatedElements: removeRepeatedElements,
};