Last active
June 1, 2021 20:37
-
-
Save themgoncalves/9e274c26438ad9d5217d5d6cfafa7b9e to your computer and use it in GitHub Desktop.
This file contains 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
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