Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active July 19, 2016 22:00
Show Gist options
  • Save johntran/fa61c65575108277997844f80c94a129 to your computer and use it in GitHub Desktop.
Save johntran/fa61c65575108277997844f80c94a129 to your computer and use it in GitHub Desktop.
Function Parameter Default Pattern
/**
* Let's say you have
* function getPromoCodeErrors(accountId, cart, cartProductItems, cartItems)
*
* If you don't have one or more of these parameters, then you need nulls in to fill out the parameters
* getPromoCodeErrors(accountId, null, null, cartItems)
*
* With the below this is valid
* getPromoCodeErrors({accountId, cartItems})
*
* This too
* getPromoCodeErrors({accountId, cartProductItems, cartItems, cart})
*
* This too
* getPromoCodeErrors()
*
* Line 24 sets an empty parameter to an object.
*/
export async function getPromoCodeErrors({
accountId = '',
cart = {},
cartProductItems = [],
cartItems = {},
} = {}) {
const promoCodeInfo = await getPromoCodeByCode(accountId, cart.promoCodes[0]);
const orderItems = getOrderItems(cart, cartProductItems);
const { validProducts, validCategories } = checkPromoCodeValidityForCart(cart, cartItems, promoCodeInfo);
const { promoCodeError } = await checkPromoCodeThresholds({
cart,
cartProductItems,
orderItems,
promoCode: promoCodeInfo,
validProducts,
validCategories
});
return promoCodeError
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment