Skip to content

Instantly share code, notes, and snippets.

@iki
Last active July 6, 2025 15:34
Show Gist options
  • Save iki/18b2577d6a41b790cf9a92e23f397a43 to your computer and use it in GitHub Desktop.
Save iki/18b2577d6a41b790cf9a92e23f397a43 to your computer and use it in GitHub Desktop.

Revisions

  1. iki revised this gist Jul 30, 2022. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions hideEmailName.ts
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,19 @@
    const hide = (s: string) => '*'.repeat(s.length);
    const hideOddMatches = (_: string, ...matches: string[]) =>
    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 ? /^(.?)(.*)(.@.)(.*)(\.[^.]+)$/ : /^(.)(.*)(.@.+)/,
    hideOddMatches
    hideEachSecondMatch
    );

    hideEmail('[email protected]');
    // 'e***[email protected]'

    hideEmail('[email protected]', { domain: true });
    // 'u***l@o******.com'

    hideEmail('[email protected]', { domain: true });
    // 'e@o******.com'
    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]'
  2. iki created this gist Jul 29, 2022.
    20 changes: 20 additions & 0 deletions hideEmailName.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    const hide = (s: string) => '*'.repeat(s.length);
    const hideOddMatches = (_: string, ...matches: string[]) =>
    matches
    .slice(0, -2)
    .map((m, i) => (i % 2 ? hide(m) : m))
    .join('');
    const hideEmail = (email: string, options?: { domain?: boolean }) =>
    email.replace(
    options?.domain ? /^(.?)(.*)(.@.)(.*)(\.[^.]+)$/ : /^(.)(.*)(.@.+)/,
    hideOddMatches
    );

    hideEmail('[email protected]');
    // 'e***[email protected]'

    hideEmail('[email protected]', { domain: true });
    // 'u***l@o******.com'

    hideEmail('[email protected]', { domain: true });
    // 'e@o******.com'