I have a function that return some informations about employees from an object database
I'm facing some complexity ESlint Problems, so i need to find a way to minimize it or find a smart way to do this instead of using a whole set of if statements.
I'm also trying to find a way to something like this: if I have name
, I don't need to do the id
verification and vice-versa and i just don't know how to do this... ♂️
The function must receive as argument an object of options that will determinate how it will behave:
- name: the first name or last name of the person that need to be searched at database
- id: the id of the person that need to be searched
e.g:
getEmployeesCoverage({ name: 'Sharonda' });
getEmployeesCoverage({ name: 'Spry' });
getEmployeesCoverage({ id: '4b40a139-d4dc-4f09-822d-ec25e819a5ad' });
I have 3 conditions:
- verify if i receive any arguments e.g
name
orid
isundefined
- verify if
name
exists in database - verify if
id
exists in database
I tried this:
function getEmployeesCoverage({ name, id }) {
if (employees.find((employee) => employee.id === id)) {
return getEmployeeById(id); // 3. verify if id exists in database
}
if (
employees.find((employee) => employee.lastName === name) ||
employees.find((employee) => employee.firstName === name)
) {
return getEmployeeByName(name); // 2. verify if the name exists in database
}
if (!name || !id) {
return getAllEmployees(); // 1. verify if i have any args e.g name or id undefined
}
throw new Error('Invalid Information');
}