Last active
July 31, 2022 14:04
-
-
Save iki/18b2577d6a41b790cf9a92e23f397a43 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 hide = (s: string) => '*'.repeat(s.length); | |
const hideEachSecondMatch = (_match: string, ...matches: string[]) => | |
matches | |
.slice(0, -2) | |
.map((m, i) => (i % 2 ? hide(m) : m)) | |
.join(''); | |
// Hide all characters in email name, except first and last character | |
// FIXME: Better hide at least one character for 1-2 character names | |
const hideEmail = (email: string, options?: { domain?: boolean }) => | |
email.replace( | |
options?.domain ? /^(.?)(.*)(.@.)(.*)(\.[^.]+)$/ : /^(.)(.*)(.@.+)/, | |
hideEachSecondMatch | |
); | |
hideEmail('[email protected]', { domain: true }); // 'e@o******.com' | |
hideEmail('[email protected]', { domain: true }); // 'em@o******.com' | |
hideEmail('[email protected]', { domain: true }); // 'e*a@o******.com' | |
hideEmail('[email protected]', { domain: true }); // 'e***l@o******.com' | |
hideEmail('[email protected]'); // 'e***[email protected]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No words, great solution regexp beast ❤️