Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 5x 5x 50x 1x 49x 1x 48x 1x 47x 47x 47x 43x 1x 42x 1x 41x 41x 2x 39x 43x 1x 42x 1x 41x 41x 2x 39x 43x 2x 41x 1x 40x 40x 17x 23x 48x 1x 47x 1x 46x 46x 46x 2x 44x 6x 38x 42x 1x 41x 1x 40x 40x 6x 34x 49x 1x 48x 1x 47x 47x 10x 37x 6x 1x 5x 1x 4x 4x 44x 44x 46x 46x 46x 42x 4x 4x 3x 1x 45x 45x 1x 44x 50x 1x 49x 49x 49x 49x 3x 3x 1x 3x 3x 1x 2x 2x 1x 1x 6x 6x 1x 20x 20x 4x 16x 16x 12x 1x 11x 5x 3x 3x 3x 3x 2x 2x 2x 2x 2x | const FORM_DATA_KEY = "formData";
const REGISTRATIONS_KEY = "registrations";
/**
* Calculate a person's age in years.
*
* @param {Date} birthDate - The birth date of the person.
* @returns {Number} - The age in years of the person.
*/
function calculateAge(birthDate) {
if (!birthDate) {
throw new Error("missing param birthDate");
}
if (!(birthDate instanceof Date)) {
throw new Error("birthDate is not a Date");
}
if (isNaN(birthDate.getTime())) {
throw new Error("birthDate is not a valid Date");
}
let dateDiff = new Date(Date.now() - birthDate.getTime());
let age = Math.abs(dateDiff.getUTCFullYear() - 1970);
return age;
}
/**
* Validate the name string.
*
* @param {String} name - The name string to validate.
* @returns {String} - The validated name string.
*/
function validateName(name) {
if (!name) {
throw new Error("name is required");
}
if (typeof name !== 'string') {
throw new Error("name must be a string");
}
const trimmedName = name.trim();
if (!trimmedName.match(/^[\p{L}]+(?:[-' ][\p{L}]+)*$/u)) {
throw new Error("name must be a valid name");
}
return trimmedName;
}
/**
* Validate the prenom string.
*
* @param {String} prenom - The prenom string to validate.
* @returns {String} - The validated prenom string.
*/
function validatePrenom(prenom) {
if (!prenom) {
throw new Error("prenom is required");
}
if (typeof prenom !== 'string') {
throw new Error("prenom must be a string");
}
const trimmedPrenom = prenom.trim();
if (!trimmedPrenom.match(/^[\p{L}]+(?:[-' ][\p{L}]+)*$/u)) {
throw new Error("prenom must be a valid prenom");
}
return trimmedPrenom;
}
/**
* Validate the email string.
*
* @param {String} email - The email string to validate.
* @returns {String} - The validated email string.
*/
function validateEmail(email) {
if (!email) {
throw new Error("email is required");
}
if (typeof email !== 'string') {
throw new Error("email must be a string");
}
const trimmedEmail = email.trim();
if (!trimmedEmail.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
throw new Error("email must be a valid email address");
}
return trimmedEmail;
}
/**
* Validate the date of birth string.
*
* @param {String} dateOfBirth - The date of birth string to validate.
* @returns {String} - The validated date of birth string.
*/
function validateDateOfBirth(dateOfBirth) {
if (!dateOfBirth) {
throw new Error("date of birth is required");
}
if (typeof dateOfBirth !== 'string') {
throw new Error("date of birth must be a string");
}
const trimmedDateOfBirth = dateOfBirth.trim();
const parsedDate = parseDateOfBirth(trimmedDateOfBirth);
if (!parsedDate) {
throw new Error("date of birth must be a valid date");
}
if (!isAdult(parsedDate)) {
throw new Error("date of birth must be at least 18 years old");
}
return trimmedDateOfBirth;
}
/**
* Validate the ville string.
*
* @param {String} ville - The ville string to validate.
* @returns {String} - The validated ville string.
*/
function validateVille(ville) {
if (!ville) {
throw new Error("ville is required");
}
if (typeof ville !== 'string') {
throw new Error("ville must be a string");
}
const trimmedVille = ville.trim();
if (!trimmedVille.match(/^[\p{L}]+(?:[-' ][\p{L}]+)*$/u)) {
throw new Error("ville must be a valid ville");
}
return trimmedVille;
}
/**
* Validate the code postal string.
*
* @param {String} codePostal - The code postal string to validate.
* @returns {String} - The validated code postal string.
*/
function validateCodePostal(codePostal) {
if (!codePostal) {
throw new Error("code postal is required");
}
if (typeof codePostal !== 'string') {
throw new Error("code postal must be a string");
}
const trimmedCodePostal = codePostal.trim();
if (!isFrenchCodePostal(trimmedCodePostal)) {
throw new Error("code postal must be a valid code postal");
}
return trimmedCodePostal;
}
/**
* Validate the form data object.
*
* @param {FormData} formData - The form data object to validate.
* @returns {Object} - The validated form data object.
*/
function validateFormData(formData) {
if (!formData) {
throw new Error("form data is required");
}
if (!(formData instanceof FormData)) {
throw new Error("form data must be a FormData");
}
const data = Object.fromEntries(formData);
return {
nom: validateName(data.nom),
prenom: validatePrenom(data.prenom),
email: validateEmail(data.email),
dateOfBirth: validateDateOfBirth(data.dateOfBirth),
ville: validateVille(data.ville),
codePostal: validateCodePostal(data.codePostal)
};
}
/**
* Check if the date of birth string is an adult.
*
* @param {Date} dateOfBirth - The date of birth to check.
* @returns {Boolean} - True if the date of birth string is an adult, false otherwise.
*/
function isAdult(dateOfBirth) {
const age = calculateAge(dateOfBirth);
return age >= 18;
}
/**
* Parse date of birth from supported formats:
* YYYY-MM-DD, YYYY/MM/DD, DD/MM/YYYY, DD-MM-YYYY.
*
* @param {String} rawDateOfBirth - The date string to parse.
* @returns {Date|null} - Parsed date if valid, otherwise null.
*/
function parseDateOfBirth(rawDateOfBirth) {
const dateOfBirth = rawDateOfBirth.trim();
const yearFirstMatch = dateOfBirth.match(/^(\d{4})[-/](\d{2})[-/](\d{2})$/);
if (yearFirstMatch) {
return buildValidDate(
Number(yearFirstMatch[1]),
Number(yearFirstMatch[2]),
Number(yearFirstMatch[3])
);
}
const dayFirstMatch = dateOfBirth.match(/^(\d{2})[-/](\d{2})[-/](\d{4})$/);
if (dayFirstMatch) {
return buildValidDate(
Number(dayFirstMatch[3]),
Number(dayFirstMatch[2]),
Number(dayFirstMatch[1])
);
}
return null;
}
/**
* Build a date and ensure day/month/year are real.
*
* @param {Number} year - Year component.
* @param {Number} month - Month component (1-12).
* @param {Number} day - Day component (1-31).
* @returns {Date|null} - Date when valid, otherwise null.
*/
function buildValidDate(year, month, day) {
const date = new Date(year, month - 1, day);
if (
date.getFullYear() !== year ||
date.getMonth() !== month - 1 ||
date.getDate() !== day
) {
return null;
}
return date;
}
/**
* Check if the code postal is a valid French code postal.
*
* @param {String} codePostal - The code postal string to check.
* @returns {Boolean} - True if the code postal is a valid French code postal, false otherwise.
*/
function isFrenchCodePostal(codePostal) {
if (typeof codePostal !== 'string') {
return false;
}
const trimmedCodePostal = codePostal.trim();
// Metropolitan France (01-95), including Corsica postal codes (20xxx).
const metropolitanPattern = /^(?:0[1-9]|[1-8]\d|9[0-5])\d{3}$/;
// Overseas departments/collectivities (971-978, 984-989).
const overseasPattern = /^(?:97[1-8]|98[4-9])\d{2}$/;
return metropolitanPattern.test(trimmedCodePostal) || overseasPattern.test(trimmedCodePostal);
}
/**
* Save the form data object to localStorage.
*
* @param {Object} formData - The form data object to save.
*/
function saveFormData(formData) {
try {
localStorage.setItem(FORM_DATA_KEY, JSON.stringify(formData));
} catch (error) {
throw new Error("unable to save form data");
}
}
/**
* Get the form data object from localStorage.
*
* @returns {Object} - The form data object.
*/
function getFormData() {
const rawFormData = localStorage.getItem(FORM_DATA_KEY);
if (!rawFormData) {
return null;
}
try {
return JSON.parse(rawFormData);
} catch (error) {
throw new Error("unable to parse saved form data");
}
}
/**
* Clear the form data object from localStorage.
*/
function clearFormData() {
localStorage.removeItem(FORM_DATA_KEY);
}
/**
* Save registrations list to localStorage.
*
* @param {Object[]} registrations - Registrations list.
*/
function saveRegistrations(registrations) {
try {
localStorage.setItem(REGISTRATIONS_KEY, JSON.stringify(registrations));
} catch (error) {
throw new Error("unable to save registrations");
}
}
/**
* Get registrations list from localStorage.
*
* @returns {Object[]} - Registrations list.
*/
function getRegistrations() {
const rawRegistrations = localStorage.getItem(REGISTRATIONS_KEY);
if (!rawRegistrations) {
return [];
}
try {
const parsedRegistrations = JSON.parse(rawRegistrations);
if (!Array.isArray(parsedRegistrations)) {
throw new Error("invalid registrations format");
}
return parsedRegistrations;
} catch (error) {
throw new Error("unable to parse saved registrations");
}
}
/**
* Append a validated registration to localStorage.
*
* @param {Object} registration - Validated registration data.
* @returns {Object} - Saved registration data.
*/
function appendRegistration(registration) {
const registrations = getRegistrations();
const nextRegistrations = [...registrations, registration];
saveRegistrations(nextRegistrations);
return registration;
}
/**
* Handle the form submission event.
*
* @param {Event} e - The form submission event.
*/
function handleSubmit(e) {
e.preventDefault();
const formData = new FormData(e.target);
const validatedData = validateFormData(formData);
appendRegistration(validatedData);
return validatedData;
}
export { calculateAge, validateName, validatePrenom, validateEmail, validateDateOfBirth, validateVille, validateCodePostal, validateFormData, isAdult, isFrenchCodePostal, saveFormData, getFormData, clearFormData, saveRegistrations, getRegistrations, appendRegistration, handleSubmit };
|