-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
229 lines (185 loc) · 6.15 KB
/
utilities.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
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcryptjs');
async function hash_password (password) {
const salt = await bcrypt.genSalt(10);
const hashed_password = await bcrypt.hash(password, salt);
return hashed_password;
}
async function compare_password (plain_password, hashed_password) {
const is_match = await bcrypt.compare(plain_password, hashed_password);
return is_match;
}
function parse_timestamp (timestamp) {
const [datePart, timePart] = timestamp.split(' ');
const [year, month, day] = datePart.split('-').map(Number);
const [hours, minutes, seconds] = timePart.split(':').map(Number);
return new Date(year, month - 1, day, hours, minutes, seconds);
}
function valid_email (email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function valid_password (password) {
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,60}$/;
return passwordRegex.test(password);
}
function simple_valid_password (password) {
const passwordRegex = /^.{6,60}$/;
return passwordRegex.test(password);
}
function valid_name (name) {
const nameRegex = /^[A-Za-zÀ-ÿ]{3,}([-'][A-Za-zÀ-ÿ]{3,})*$/;
return nameRegex.test(name);
}
function valid_surname (surname) {
const surnameRegex = /^[A-Za-zÀ-ÿ]{3,}([-'][A-Za-zÀ-ÿ]{3,})*$/;
return surnameRegex.test(surname);
}
function valid_biography (biography) {
const bioRegex = /^[\s\S]{10,2000}$/;
return bioRegex.test(biography);
}
function valid_phone (phone) {
const phoneRegex = /^(?:\+?\d{1,3})?[-.\s]?(?:\(?\d{1,4}?\)?)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/;
return phoneRegex.test(phone);
}
function valid_address (address) {
const addressRegex = /^[a-zA-Z0-9\s,.'-]{5,100}$/;
return addressRegex.test(address);
}
function valid_auction_name (name) {
if (name.length < 3 || name.length >= 200) {
return false;
}
return true;
}
function valid_auction_description (description) {
if (description.length < 10 || description.length >= 3000) {
return false;
}
return true;
}
function valid_id (id) {
if (id <= 0) {
return false;
}
return true;
}
function valid_starting_price (price) {
if (price <= 0 || price >= 50000) {
return false;
}
return true;
}
function valid_duration (duration) {
if (duration <= 0) {
return false;
}
return true;
}
function valid_report_reason (reason) {
const reasonRegex = /^[\s\S]{10,3000}$/;
return reasonRegex.test(reason);
}
function valid_ban_reason (reason) {
const reasonRegex = /^[\s\S]{10,3000}$/;
return reasonRegex.test(reason);
}
function valid_feedback (feedback) {
const feedbackRegex = /^[\s\S]{10,3000}$/;
return feedbackRegex.test(feedback);
}
function valid_rating (rating_number) {
if (Number(rating_number) >= 1 && Number(rating_number) <= 5) {
return true;
}
return false;
}
function round_minutes_to_interval (timestamp, interval = 10) {
const date = new Date(timestamp);
const remainder = interval - (date.getMinutes() % interval);
date.setMinutes(date.getMinutes() + remainder);
date.setSeconds(0);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const roundedMinutes = String(date.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${roundedMinutes}:00`;
}
function add_time_to_timestamp (timestamp, value, unit) {
let hours_to_add = Number(value);
switch (Number(unit)) {
case 1:
break;
case 2:
hours_to_add *= 24;
break;
case 3:
hours_to_add *= 24 * 7;
break;
case 4:
hours_to_add *= 24 * 30;
break;
default:
throw new Error('Invalid time unit');
}
const date = parse_timestamp(timestamp);
date.setHours(date.getHours() + hours_to_add);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); //Riempie la stringa con 0
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function get_current_timestamp () {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function read_config_file (filename) {
const config_path = path.join(__dirname, filename);
const config = fs.readFileSync(config_path, 'utf-8');
const config_obj = {};
config.split(/\r?\n/).forEach(line => {
const [key, value] = line.split('=');
if (key && value) {
config_obj[key.trim()] = value.trim();
}
});
return config_obj;
}
module.exports = {
hash_password,
compare_password,
parse_timestamp,
valid_email,
valid_password,
simple_valid_password,
valid_name,
valid_surname,
valid_biography,
valid_phone,
valid_address,
valid_auction_name,
valid_auction_description,
valid_id,
valid_starting_price,
valid_duration,
valid_report_reason,
valid_ban_reason,
valid_feedback,
valid_rating,
round_minutes_to_interval,
add_time_to_timestamp,
get_current_timestamp,
read_config_file
};