-
Notifications
You must be signed in to change notification settings - Fork 6
/
utility.ts
164 lines (138 loc) · 4.12 KB
/
utility.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*********************************
* Utility methods
*********************************/
import {
Grade,
Student
} from 'Student';
import {
Subject
} from 'Subject';
import {
Constant
} from 'Constant';
/**
* Removes the white space
*/
export function remWhiteSpace(str: string): string {
return str.replace(/\W/g, '');
}
/**
* Removes the new line characters
* @param str Input string
*/
export function remNewLine(str: string): string {
return str.replace(/\n/g, '');
}
/**
* Removes extra space into one space
*/
export function remExtraSpaces(str: string): string {
return str.replace(/\s+/g, ' ').trim();
}
/**
* Gets a table element inside an ID
*/
export function setTable(id: string, html: string) {
document.getElementById(id).getElementsByTagName('table')[0].innerHTML = html;
}
/**
* sort Student array by percentage
*/
export function sortStudentByPercentage(studentArr: Array < Student > ) {
return studentArr.sort((studentA, studentB) => {
return studentB.getPercentage() - studentA.getPercentage();
});
}
/**
* sort Subject array by pass percentage
*/
export function sortSubjectByPassPercentage(subjectArr: Array < Subject > ) {
return subjectArr.sort((subjectA, subjectB) => {
return subjectB.getPassPercentage() - subjectA.getPassPercentage();
});
}
/**
* Splits the content into array of result string
* @param str Input text content
*/
export function resultStringSplit(str: string) {
return str.match(/\d{6,9}.*?(PASS|COMP|FAIL|ABST)/gi);
}
/**
* Returns the roll number of student
*/
export function getRoll(str: string): number {
return parseInt((str.match(/(\d{6,9})/)[1]).trim(), 10);
}
/**
* Returns the roll number of student
*/
export function getName(str: string) {
return str.match(/\d{6,9}(.*?)\d/)[1].trim();
}
/**
* Result Enum returned
*/
export function getResult(str: string): string {
return (str.match(/.*\s(.*?)$/)[1]).toUpperCase();
}
/**
* Parses the string to extract the marks details
*/
export function getGradeArr(str: string, student: Student): Array < Grade > {
// Gets the marks string: Examples
// 301 095 A1 041 065 B2 042 075 B1 043 042FP E 044 057FP E A2 A2 A2
// 301 ABA 042 029FT E 043 030FT E 083 028FT E 041 ABA B1 B1 B2
// 301 042 043 044 041
// Number of subjects is not fixed and we shouldn't assume the subject code
/**
* Logic
* If the person is ABST, just take the code and assign NA to marks and grade
* If the person is having ABA, then take 'NA' for the subject
* Trim all the subjects with ABA and take the new string
*/
let marksStr = remExtraSpaces(str.match(new RegExp(student.getName() + '(.*)' + student.getResult()))[1]);
// Iterate the string to fetch Subject, marks and grade
let marksArr = marksStr.split(' ');
let gradeArr: Array < Grade > = [];
function addAbstSubject(code: any) {
if (!!parseInt(code)) {
gradeArr.push({
code: parseInt(code, 10),
marks: 0,
grade: Constant.ABST,
isAbst: true
});
}
}
// Abst scenario
if (student.isAbst()) {
marksArr.forEach(el => {
addAbstSubject(el);
});
} else {
// Subject absent case
let abaArr = marksStr.match(/\d+\sABA/gi);
if (abaArr) {
abaArr = abaArr.join(' ').split(' ');
abaArr.forEach(el => {
addAbstSubject(el);
// Remove the el from the array
marksArr.splice(marksArr.indexOf(el), 1);
});
}
// On the remaining subjects
for (let i = 0; i < marksArr.length; i = i + 3) {
if (parseInt(marksArr[i]) && parseInt(marksArr[i + 1])) {
gradeArr.push({
code: parseInt(marksArr[i], 10),
marks: parseInt(marksArr[i + 1], 10),
grade: marksArr[i + 2],
isAbst: false
});
}
}
}
return gradeArr;
}