Extracting common methods
In this section, we’ll pull out the validation clauses that are duplicated in the addNew
and replace
functions, moving them into a shared validate
function.
Let’s start now with the validate
function:
- Below the definitions of
addNew
andreplace
, add the following function namedvalidate
. This contains the two guard clauses that appeared in each of the original functions. As a simplification, theitem
argument has been destructured intoname
anddob
arguments:const validate = ({ name, dob }) => { if (empty(name)) { return { error: 'Please provide a name.' }; } if (invalidDob(dob)) { return { error: 'Please provide a date of birth in the YYYY- MM-DD format.' }; &...