forked from microsoft/CopilotHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
124 lines (110 loc) · 3.89 KB
/
Copy pathfunctions.js
File metadata and controls
124 lines (110 loc) · 3.89 KB
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
/**
* Validates a Spanish phone number.
* @param {string} phoneNumber - The phone number to validate.
* @returns {boolean} - Returns true if the phone number is valid, false otherwise.
*/
function validatePhoneNumber(phoneNumber){
const regEx = /^(\+34|0034|34)?[ -]*(6|7)[ -]*([0-9][ -]*){8}$/;
return regEx.test(phoneNumber);
}
/**
* Calculates the number of days between two dates.
* @param {string} date1 - The first date.
* @param {string} date2 - The second date.
* @returns {number} - The number of days between the two dates.
*/
function daysBetweenDates(date1, date2){
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(date1);
const secondDate = new Date(date2);
const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
return diffDays;
}
/**
* Validates a Spanish DNI.
* @param {string} dni - The DNI to validate.
* @returns {string} - Returns "valid" if the DNI is valid, "invalid" otherwise.
*/
function validateSpanishDNI(dni){
// calculate DNI letter
var dniLetter = dni.charAt(dni.length - 1);
var dniNumber = dni.substring(0, dni.length - 1);
var dniLetterCalc = "TRWAGMYFPDXBNJZSQVHLCKE".charAt(dniNumber % 23);
//if DNI is valid return "valid"
if (dniLetter == dniLetterCalc) {
return "valid";
}
//if DNI is not valid return "invalid"
else {
return "invalid";
}
}
/**
* Returns the memory usage of the current Node.js process.
* @returns {string} - The memory usage of the current Node.js process, in GB.
*/
function memoryUsage(){
const used = process.memoryUsage().heapUsed / 1024 / 1024 / 1024;
return used.toFixed(2).toString();
}
/**
* Returns the size of a gzipped file.
* @param {string} filename - The name of the file to gzip.
* @param {string} gzFilename - The name of the gzipped file.
* @returns {string} - The size of the gzipped file, in bytes.
*/
function zipFile(filename, gzFilename ){
const fs = require('fs');
const zlib = require('zlib');
// Leer el contenido del archivo
const contenido = fs.readFileSync(filename);
//gzip the filename in the directory by default
zlib.gzip(contenido, (err, buffer) => {
if (err) {
console.error(err);
return;
}
//write the gzip file
fs.writeFileSync(gzFilename, buffer);
});
//get the size of the gzip file
const stats = fs.statSync(gzFilename);
const fileSizeInBytes = stats.size;
//cast to string fileSizeInBytes
const fileSizeInBytes_string = fileSizeInBytes.toString();
return fileSizeInBytes_string;
}
//function Make an array of european countries and its iso codes
function getEuropeanCountries() {
const europeanCountries = [
{ name: 'Albania', code: 'AL' },
{ name: 'Andorra', code: 'AD' },
{ name: 'Austria', code: 'AT' },
{ name: 'Belarus', code: 'BY' },
{ name: 'Belgium', code: 'BE' },
{ name: 'Bosnia and Herzegovina', code: 'BA' },
{ name: 'Bulgaria', code: 'BG' },
{ name: 'Croatia', code: 'HR' },
{ name: 'Cyprus', code: 'CY' },
{ name: 'Czech Republic', code: 'CZ' },
{ name: 'Denmark', code: 'DK' },
{ name: 'Estonia', code: 'EE' },
{ name: 'Finland', code: 'FI' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'Greece', code: 'GR' },
{ name: 'Hungary', code: 'HU' },
{ name: 'Iceland', code: 'IS' },
{ name: 'Ireland', code: 'IE' },
{ name: 'Italy', code: 'IT' }]
//return ramdom country of array in json format
return JSON.stringify(europeanCountries[Math.floor(Math.random() * europeanCountries.length)]);
}
module.exports = {
validatePhoneNumber,
daysBetweenDates,
validateSpanishDNI,
memoryUsage,
zipFile,
getEuropeanCountries
};