Skip to content

Instantly share code, notes, and snippets.

@m3hari
Created May 24, 2019 12:23
Show Gist options
  • Save m3hari/d16cdb227ec16f6f277bb807229491bb to your computer and use it in GitHub Desktop.
Save m3hari/d16cdb227ec16f6f277bb807229491bb to your computer and use it in GitHub Desktop.
Restructuring Sample
// 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