Skip to content

[feat] Add photo util#2

Open
taingmeng wants to merge 3 commits into
masterfrom
refactor-duplicated-codee
Open

[feat] Add photo util#2
taingmeng wants to merge 3 commits into
masterfrom
refactor-duplicated-codee

Conversation

@taingmeng
Copy link
Copy Markdown
Owner

Description

Context

Checklist:

  • Updated tests for this change.
  • Tested the changes locally.
  • Updated documentation.

Screenshots (if available)

Link to story (if available)

Comment thread photoUtil.js

export const isUserBlocked = (user, photo) => {
return photo.user.blockedFriends.map(user => user.id).includes(user.id);
};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 functions contain similar code. I'm okay if you leave it like that. If you want to refactor it a bit, you extract the similar part to:

const isUserIn = (user, userPool) => {
  return userPool.map(user => user.id).includes(user.id);
}

and use it like this:

export const isUserTagged = (user, photo) => {
  return isUserIn(user, photo.taggedUsers);
}

export const isUserTagged = (user, photo) => {
  return isUserIn(user, photo.user.blockedFriends);
}

@taingmeng taingmeng force-pushed the refactor-duplicated-codee branch from 7e03db6 to 37ddf4a Compare November 17, 2019 08:24
Comment thread getTotalClaps.js
claps += fan.claps.length;
}
return claps;
};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Javascript, you can achieve the same result by using .reduce if you want to accumulate a result from an array. For example:

export const getTotalClaps = story => {
  return story.fans.reduce((claps, fan) => claps + fan.claps.length, 0);
};

Comment thread isValidAgeForDriving.js
return true;
}
return false;
};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this function to one line like this:

export const isValidAgeForDriving = age => age >= 18 && age <= 80;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant