Last active
October 26, 2018 19:29
-
-
Save islam3zzat/cb9e1630e69f26f326c098a3eb5c4339 to your computer and use it in GitHub Desktop.
This is an error prone code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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