Closed KiraElric closed 2 years ago
function isValidDate(date) {
const regex = /^([12]\d{3}\/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01]))$/;
const dateOk = regex.test(date);
if (!dateOk) {
// eslint-disable-next-line no-console
console.log('Formato incorrecto', date);
}
return dateOk;
}
function dataFile(fileName) {
// eslint-disable-next-line consistent-return
const data = fs.readFileSync(fileName, 'utf-8', (err, file) => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
} else {
return file;
}
});
return data;
}
function formatedDataToObject(data) {
const dataFormated = data
.split('\n')
.splice(1)
.map(row => {
if (row === '') {
throw new Error('Existe una fila sin información. Favor eliminarla del archivo.');
}
const workerData = row.split(',');
const worker = {
name: workerData[1].replace('"', '').trim(),
lastname: workerData[0].replace('"', ''),
email: workerData[2],
birthday: workerData[3],
company: workerData[4],
};
return worker;
});
return dataFormated;
}
function show(data) {
data.forEach(person => {
// eslint-disable-next-line no-console
console.log(`${person.birthday} => ${person.name} ${person.lastname}`);
});
}
function dateComparison(date1, date2, birthday) {
const birthdayMonth = parseInt(birthday[1], 10);
const birthdayDay = parseInt(birthday[2], 10);
if (birthdayMonth === date1.getMonth() + 1 && birthdayMonth === date2.getMonth() + 1) {
if (birthdayDay >= date1.getDate() && birthdayDay <= date2.getDate()) {
return true;
}
} else if (birthdayMonth === date1.getMonth() + 1) {
if (birthdayDay >= date1.getDate()) {
return true;
}
} else if (birthdayMonth === date2.getMonth() + 1) {
if (birthdayDay <= date2.getDate()) {
return true;
}
} else if (birthdayMonth > date1.getMonth() + 1 && birthdayMonth < date2.getMonth() + 1) {
return true;
}
return false;
}
const startDate = process.argv[2];
const endDate = process.argv[3];
const date1 = new Date(startDate);
const date2 = new Date(endDate);
const filteredBirthdays = [];
try {
if (isValidDate(startDate) && isValidDate(endDate)) {
// Se va a trabajar la data ya leida y transformada en un objecto.
const fileInfo = dataFile('./mails_y_cumples_03.csv');
const workersData = formatedDataToObject(fileInfo);
// eslint-disable-next-line no-console
show(workersData);
}
} catch (e) {
// eslint-disable-next-line no-console
console.log(e.message);
}
Normalización de idioma en el código.
Posibilidad de normalizar el idioma de las variables, para dejarlas todas en inglés.