-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.ts
137 lines (115 loc) · 3.66 KB
/
utils.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
import convert from 'html-to-json-data';
import { group, text } from 'html-to-json-data/definitions.js';
import { parse } from "date-fns";
import fr from "date-fns/locale/fr/index.js";
import md5 from 'md5'
interface Outage {
date: string;
locality: string;
streets: string;
district: string;
from: string; // Date string in ISO format
to: string; // Date string in ISO format
id: string;
}
interface InputData {
[district: string]: Outage[];
}
interface OutputObject {
today: Outage[];
future: Outage[];
}
function parseDate(date: string, delimiter = 'from', tz = "+0400") {
if (date.length > 0) {
let processing: string[] = date.replace(/\s+/g, ' ').trim().split('à');
const opt = {
locale: fr,
};
const from = parse(processing[0].trim() + tz, "'Le' EEEE d MMMM yyyy 'de' HH:mm:ssxx", new Date(), opt)
const to = parse(processing[1].trim() + tz, 'HH:mm:ssxx', from, opt)
if (delimiter === 'from') {
return from
} else {
return to
}
} else {
return date
}
}
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for (var i in originalArray) {
if (originalArray[i]['date'] !== '') { // also remove empty values when date is null
lookupObject[originalArray[i][prop]] = originalArray[i];
}
}
for (i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
export function makeUniq(obj) {
let newObj = {}
for (let arr in obj) {
let newSet = removeDuplicates(obj[arr], 'id')
newObj[arr] = [...newSet]
}
return newObj
}
export const extractFromSource = (data) => {
const districts = Object.keys(data);
const dataset = {}
for (const district in districts) {
if (Object.prototype.hasOwnProperty.call(districts, district)) {
const element = districts[district];
let currentDistrict = convert(data[element],
group('[id^=table-mauritius] tbody tr',
{
date: text('td:nth-child(1)'),
locality: text('td:nth-child(2)'),
streets: text('td:nth-child(3)'),
district: element,
}
)
)
dataset[element] = currentDistrict.map(item => {
return {
...item,
"from": parseDate(item.date, 'from'),
"to": parseDate(item.date, 'to'),
"id": md5(JSON.stringify(item))
}
})
}
}
return dataset
}
// Source: ChatGPT 3.5 :smirk:
export const categorize = (inputData: InputData): OutputObject => {
const endOfToday = new Date();
endOfToday.setHours(23, 59, 59, 999);
const todayOutages: Outage[] = [];
const futureOutages: Outage[] = [];
// Iterate over each district in the input data.
Object.values(inputData).forEach((outages: Outage[]) => {
outages.forEach((outage: Outage) => {
if (!outage.from) {
// If 'from' field is missing or empty, skip this outage.
return;
}
const from = new Date(outage.from);
// Compare outage date with today's date.
if (from < endOfToday) {
todayOutages.push(outage);
} else {
futureOutages.push(outage);
}
});
});
// Return an object with 'today' and 'future' keys.
return {
today: todayOutages,
future: futureOutages
};
}