Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active June 1, 2021 20:37
Show Gist options
  • Save themgoncalves/9e274c26438ad9d5217d5d6cfafa7b9e to your computer and use it in GitHub Desktop.
Save themgoncalves/9e274c26438ad9d5217d5d6cfafa7b9e to your computer and use it in GitHub Desktop.
const maskSensitivityInfos = (card) => {
// we use SPREAD OPERATOR to copy all properties from 'card' parameter
// this will remove the reference binding.
// In other words: NO MUTATION
const cc = { ...card };
cc.number = cc.number.replace(/.(?=.{4})/g, '#');
return cc;
}
const displaySelectedPayment = (card) => {
console.log(`Paying with ${card.type} ${card.number}`);
}
const processPayment = (card) => {
console.log('Card details', card);
};
const creditCard = { type: 'MasterCard', number: '5431111111111111', cvv: '595' };
const maskedCreditCard = maskSensitivityInfos(creditCard);
displaySelectedPayment(maskedCreditCard);
// output: 'Paying with MasterCard ############1111'
processPayment(creditCard);
// output: 'Card details: { type: 'MasterCard', number: '5431111111111111', cvv: '595' }'
// no side effects! data still reliable and ready to process the transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment