-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
295 lines (263 loc) · 7.48 KB
/
utils.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const v = require('voca')
/**
* Count the number of the left zeros.
* @param input The strings that contain the number of zero to count.
* @returns The number of the left zero of the input strings.
*/
function countLeftZeros (input) {
let zeros = 0
if (input.length > 1) {
while (v.startsWith(input[0], 0) && v.startsWith(input[1], 0)) {
input[0] = v.substring(input[0], 1)
input[1] = v.substring(input[1], 1)
zeros++
}
}
return zeros
}
/**
* Remove all the characters after the parenthesis.
* @returns The string without the content after the parenthesis.
*/
function removeAllAfterParenthesis (input) {
return v.first(input, input.indexOf(' ('))
}
exports.removeAllAfterParenthesis = removeAllAfterParenthesis
/**
* Get the content in the parenthesis.
* @returns The string between the parenthesis.
*/
exports.getParenthesisContent = function getParenthesisContent (input) {
let startingIndex = input.indexOf('(')
if (startingIndex === -1) {
return ''
}
startingIndex++
let openingParenthesisCount = 1
for (let i = startingIndex + 1; i < input.length; i++) {
if (input[i] === ')') {
openingParenthesisCount--
if (openingParenthesisCount === 0) {
return input.substring(startingIndex, i)
}
} else if (input[i] === '(') {
openingParenthesisCount++
}
}
return input.substring(startingIndex)
}
/**
* Remove the parenthesis from the input string.
* @returns The input string without the parenthesis.
*/
exports.removeParenthesis = function removeParenthesis (input) {
return input.replace(/ *\([^)]*\) */g, '') // remove () and everything in them
}
/**
* Remove the brackets from the input string.
* @returns The input string without brackets.
*/
exports.removeBrackets = function removeBrackets (input) {
return input.replace(/ *\[[^)]*\] */g, '') // remove [] and everything in them
}
/**
* Pad the given string for the given width for the given symbol.
* @param n The string to pad.
* @param width The width of the padding.
* @param z The symbol to use for the padding.
* @returns The padded string.
*/
function pad (n, width, z) {
z = z || '0'
n = n + ''
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n
}
/**
* Find the sindaco name.
* @param string The string from which retrieve the sindaco name.
* @returns Returns the sindaco name.
*/
function findSindacoNome (string) {
if (!v.includes(string, ' dal')) {
return string
}
let sindacoName = v.first(string, v.indexOf(string, ' dal'))
if (v.includes(sindacoName, '(')) {
sindacoName = exports.removeAllAfterParenthesis(sindacoName)
}
return sindacoName
}
const MONTH_NAMES = [{
name: 'gennaio',
value: 1
},
{
name: 'febbraio',
value: 2
},
{
name: 'marzo',
value: 3
},
{
name: 'aprile',
value: 4
},
{
name: 'maggio',
value: 5
},
{
name: 'giugno',
value: 6
},
{
name: 'luglio',
value: 7
},
{
name: 'agosto',
value: 8
},
{
name: 'settembre',
value: 9
},
{
name: 'ottobre',
value: 10
},
{
name: 'novembre',
value: 11
},
{
name: 'dicembre',
value: 12
}]
/**
* Replace the month with the corresponding month value.
* @param {string} string The string containing the month.
* @returns Returns the string with the replaced month.
*/
function replaceMonthNameWithDigit (string) {
MONTH_NAMES.forEach(month => {
const regex = new RegExp(month.name, 'i')
string = string.replace(regex, month.value)
})
return string
}
/**
* Find the sindaco inzio carica
* @param {string} string The string from which retrieve the sindaco inizio carica field.
* @returns Returns the sindaco inizio carica.
*/
function findSindacoInizioCarica (string) {
const contentWithoutParenthesis = exports.removeParenthesis(string)
let stringBeforeDate = 'dal '
if (v.includes(contentWithoutParenthesis, 'dall\'')) {
stringBeforeDate = 'dall\''
}
if (!v.includes(contentWithoutParenthesis, stringBeforeDate)) {
return ''
}
let containingDate = v.substring(contentWithoutParenthesis, v.indexOf(contentWithoutParenthesis, stringBeforeDate))
if (v.includes(containingDate, ' riconfermato')) {
containingDate = v.substring(containingDate, 0, v.indexOf(containingDate, ' riconfermato'))
}
containingDate = replaceMonthNameWithDigit(containingDate)
containingDate = v.replace(containingDate, 'º', '')
// Handle bad case: "26-27 5 2019" or "3-4 10 2019" or "26/27 5 2019" or "26/27-5-2019"
let matches = containingDate.match(/(\d?\d(-|\/)\d?\d)( |-)(\d*)( |-)(\d\d\d\d)/)
if (matches) {
containingDate = v.first(matches[1], v.indexOf(matches[1], matches[2])) + ' ' + matches[4] + ' ' + matches[6]
}
const regex = /((\d?\d)(\/|-| |\.)(\d?\d)(\/|-| |\.)(\d?\d?\d\d)|(\d?\d)(\/|-| |\.)(\d?\d?\d\d)|(\d\d\d\d))/
matches = regex.exec(containingDate)
if (matches === null) {
return ''
} else {
if (matches[2] && matches[4] && matches[6]) {
// DD-MM-YYYY
containingDate = matches[2] + '-' + matches[4] + '-' + matches[6]
} else if (matches[7] && matches[9]) {
// MM-YYYY
containingDate = '01-' + matches[7] + '-' + matches[9]
} else if (matches[10]) {
// YYYY
containingDate = '01-01-' + matches[10]
}
}
const date = v.split(containingDate, '-')
const day = pad(date[0], 2)
const month = pad(date[1], 2)
let year = v.first(date[2], 4)
if (year.length === 2) {
year = new Date().getFullYear().toString().substring(0, 2) + year
}
return day + '/' + month + '/' + year
}
/**
* Build the sindaco from the input string.
* @param string The string from which retrieve the sindaco information.
* @returns Returns the sindaco object.
*/
exports.buildSindaco = function buildSindaco (string) {
string = string.trim()
return {
nome: findSindacoNome(string),
partito: exports.getParenthesisContent(string),
inizioCarica: findSindacoInizioCarica(string)
}
}
/**
* Build the img url from the input string.
* @param string The string from which build the img url.
* @returns Returns the img url.
*/
exports.buildImgUrl = function buildImgUrl (string) {
let stemma = v.first(string, string.lastIndexOf('/'))
stemma = v.replaceAll(stemma, 'thumb/', '')
if (!v.startsWith(stemma, 'https:')) {
stemma = 'https:' + stemma
}
return stemma
}
/**
* Build codici postali array from the input string.
* @param string The string from which build codici postali array.
* @returns Returns codici postali array.
*/
exports.buildCodiciPostali = function buildCodiciPostali (string) {
const codiciPostaliArray = []
if (string.match(/\(/g)) {
// Remove parenthesis if the string contains them
string = removeAllAfterParenthesis(string)
}
string = string.trim()
// Multiple case
if (string.length > 5) {
const numberPattern = /\d+/g
const codiciPostali = string.match(numberPattern)
const zeros = countLeftZeros(codiciPostali)
// console.log(codiciPostali)
// console.log('Zeros: ', zeros)
codiciPostali[0] = parseInt(codiciPostali[0])
codiciPostali[1] = parseInt(codiciPostali[1])
for (; codiciPostali[0] <= codiciPostali[1]; codiciPostali[0]++) {
let codiceToInsert = codiciPostali[0]
codiceToInsert = v.repeat('0', zeros) + codiceToInsert
codiciPostaliArray.push(codiceToInsert)
}
} else {
codiciPostaliArray.push(string)
}
return codiciPostaliArray
}
exports.generateAllCodPostaliBetweenTwo = function (start, end, prefix = '00') {
const output = []
for (; start <= end; start++) {
output.push(prefix + start)
}
return output
}