Skip to content

Instantly share code, notes, and snippets.

@islam3zzat
Last active October 26, 2018 19:29
Show Gist options
  • Save islam3zzat/cb9e1630e69f26f326c098a3eb5c4339 to your computer and use it in GitHub Desktop.
Save islam3zzat/cb9e1630e69f26f326c098a3eb5c4339 to your computer and use it in GitHub Desktop.
This is an error prone code
// ...
function updateUserName(user, { firstName, lastName }) {
const { name } = user;
if (firstName) name.firstName = firstName;
if (lastName) name.lastName = lastName;
return { ...user, name };
}
function saveUser(users, updatedUser) {
return users.map(user => (user.id === updatedUser.id ? updatedUser : user));
}
// ...
const user = { ...users.find(userByMatcher({ key: 'id', value: 2 })) };
const updatedUser = updateUserName(user, { lastName: 'new last name' });
// no need to call `saveUser`, user has already been saved :(
// check users
console.log(users);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment