-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffCalculator.js
61 lines (52 loc) · 2.11 KB
/
diffCalculator.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
import { isEqual } from "lodash-es";
/**
* Create diff of two grade objects.
* @param {object | null} before - Data before update.
* @param {object} now - Current, updated data.
* @param {boolean} [returnEmpty=false] - Whether it should return empty object instead of undefined when there are no changes
* @returns {object} Changes in grades compared to **before**.
*/
export function generateDiff(before, now, returnEmpty = false) {
// Skip whole diff if data is the same as before
if(isEqual(before, now)) {
if(returnEmpty) {
return {};
}else{
return;
}
}
// If before is empty return current data
if(!before) {
return {
date: new Date().toISOString(),
subjects: now
};
}
const diffBuilder = {};
for(const subjectName in now) {
const subjectNow = now[subjectName];
const subjectBefore = before[subjectName];
// Skip if subject data is the same as before
if(isEqual(subjectBefore, subjectNow)) continue;
// If subject hasn't existed before, just push current data to diff
if(!subjectBefore && subjectNow) {
diffBuilder[subjectName] = subjectNow;
} else { // If subject existed before, calculate changes and push to diff
const subjectDiffBuilder = {grades: {}, average: subjectNow.average};
// Iterate over every name of grade. If current data exists it always contains all grade names so we can use subjectNow
for(const gradeName in subjectNow.grades) {
const gradeNow = subjectNow.grades[gradeName];
const gradeBefore = subjectBefore.grades[gradeName];
// If current grade count is different than before, calculate difference and push to diff
if(gradeNow != gradeBefore) {
subjectDiffBuilder.grades[gradeName] = gradeNow - gradeBefore;
}
}
diffBuilder[subjectName] = subjectDiffBuilder;
}
}
return {
date: new Date().toISOString(),
subjects: diffBuilder
}
}