-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting.ts
78 lines (69 loc) · 2.14 KB
/
Sorting.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { DropdownAutocompleteOptions } from '../components/DropdownAutocomplete';
/**
* Sort the items by case insensitive (by comparing the strings with their locale)
*/
export function sortByLocale(a: string, b: string) {
return !a || !b ? 0 : a.localeCompare(b);
}
/**
* Sort booleans, returning true values first
*/
export function sortBoolean(a: boolean, b: boolean) {
return a === b ? 0 : a ? -1 : 1;
}
/**
* Sort the severities using the value
*/
export function sortSeverity(a: number, b: number) {
return a - b;
}
/**
* Sorting the dates by turning strings into dates, and then subtract them
*/
export function sortDates(a: string, b: string) {
return new Date(a).getTime() - new Date(b).getTime();
}
/**
* Sort by Vulnerability Severity (HIGH > MEDIUM > LOW > OTHER)
*/
export function sortByVulnerabilitySeverity(a: string, b: string) {
if (a === b) {
return 0;
// eslint-disable-next-line no-mixed-operators
}
if ((a === 'HIGH' && (b === 'MEDIUM' || b === 'LOW')) || (a === 'MEDIUM' && b === 'LOW')) {
return 1;
}
return -1;
}
/**
* Sort data for the Export File in the same way the table is sorted
* @param data
* @param columnID
* @param sortFunction
*/
function sortTableData(data: any, columnID: string, sortFunction: string) {
switch (sortFunction) {
case 'sortBoolean':
return data.sort((a: any, b: any) => sortBoolean(a[columnID], b[columnID]));
case 'sortSeverity':
return data.sort((a: any, b: any) => sortSeverity(a.vulnerabilitiesWeight, b.vulnerabilitiesWeight));
case 'sortDates':
return data.sort((a: any, b: any) => sortDates(a[columnID], b[columnID]));
default:
return data.sort((a: any, b: any) => sortByLocale(a[columnID], b[columnID]));
}
}
/**
* Sort the Filter Options Data
*/
export function sortOptionsByVulnerabilitySeverity(options: DropdownAutocompleteOptions[]) {
return options.sort((a, b) => sortByVulnerabilitySeverity(a.name, b.name));
}
/**
* Sort the effective Options Data
*/
export function sortOptionsByEffective(options: DropdownAutocompleteOptions[]) {
return options.sort((a, b) => sortSeverity(parseInt(a.value, 10), parseInt(b.value, 10)));
}
export default sortTableData;