Skip to content

Instantly share code, notes, and snippets.

@taingmeng
Last active November 17, 2019 08:08
Show Gist options
  • Save taingmeng/df0dc4d596c7cb0f38596beccbdfee38 to your computer and use it in GitHub Desktop.
Save taingmeng/df0dc4d596c7cb0f38596beccbdfee38 to your computer and use it in GitHub Desktop.
Example of refactoring duplicated code
// Before
const isUserTagged = (user, photo) => {
return photo.taggedUsers.map(user => user.id).includes(user.id)
}
const isUserBlocked = (user, photo) => {
return photo.user.blockedFriends.map(user => user.id).includes(user.id)
}
// After
const isUserIn = (user, userPool) => {
return userPool.map(user => user.id).includes(user.id);
}
const isUserTagged = (user, photo) => {
return isUserIn(user, photo.taggedUsers);
}
const isUserTagged = (user, photo) => {
return isUserIn(user, photo.blockedFriends);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment