Created
September 27, 2019 12:13
-
-
Save rpdiss/04a37ffde5156ceaf3e1ac5c981ee4b7 to your computer and use it in GitHub Desktop.
typescript credit card masking function
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
| export const maskCreditCard = async (creditCard: string, start: number, end: number, mask = `*`): Promise<string> => { | |
| const pattern = `(?<=[0-9]{${start}})(?:.*:?)(?=[0-9]{${end}})`; | |
| const regExp = new RegExp(pattern, 'g'); | |
| const startRes = creditCard.substr(0, start); | |
| const endRes = creditCard.substr(creditCard.length - end); | |
| const maskMatch = '4505290686046367'.match(regExp); | |
| return maskMatch ? `${startRes}${maskMatch[0].replace(/[a-zA-Z0-9]/g, mask)}${endRes}` : creditCard.replace(/[a-zA-Z0-9]/g, mask); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment