Created
May 24, 2019 12:23
-
-
Save m3hari/d16cdb227ec16f6f277bb807229491bb to your computer and use it in GitHub Desktop.
Restructuring Sample
This file contains hidden or 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
// With out destructuring | |
function isPremium(user, role) { | |
return ( | |
user && | |
user.membershipPlan && | |
user.membershipPlan.type === "PREMIUM" && | |
role === "CUSTOMER" | |
); | |
} | |
// Usage; | |
const user = { | |
id: "123", | |
membershipPlan: { type: "BASIC" } | |
}; | |
isPremium(user, "CUSTOMER"); | |
// =================================================================================== | |
// With destructuring | |
const isPremium = ({ membershipPlan = {}, role = membershipPlan.role } = {}) => | |
membershipPlan.type === "PREMIUM" && role === "CUSTOMER"; | |
// Usage; | |
const user = { | |
id: "123", | |
membershipPlan: { type: "BASIC", role: "ADMIN" } | |
}; | |
// Approach - 1 | |
isPremium(user); | |
// Approach - 2 | |
isPremium({ membershipPlan: { type: "BASIC", role: "ADMIN" } }); | |
// Approach - 3 | |
isPremium({ membershipPlan: { type: "BASIC" }, role: "ADMIN" }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment