Skip to content

Commit c92e329

Browse files
committed
feat(utils/privacy): enhance maskEmail to mask non-email strings
Adds logic to `maskEmail` to differentiate between email and non-email inputs. Masks email addresses as `******@domain.com` and parenthesized content. Masks non-email strings (e.g., usernames) by keeping the first character and replacing the rest with asterisks (e.g., `Kiro` -> `K***`). Updates JSDoc to reflect new masking behavior for non-email strings. This provides a more versatile privacy masking utility for user identifiers.
1 parent fdd6ace commit c92e329

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/utils/privacy.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,33 @@
44

55
/**
66
* Masks an email address while keeping the domain visible.
7-
* Example: aaaaaa@gmail.com -> ******@gmail.com
8-
*/
9-
/**
10-
* Masks an email address while keeping the domain visible.
11-
* Handles both standalone emails and emails strings with suffixes like " (project-id)".
7+
* For non-email strings (usernames), masks all but the first character.
128
* Example: aaaaaa@gmail.com -> ******@gmail.com
139
* Example: aaaaaa@gmail.com (my-project) -> ******@gmail.com (******)
10+
* Example: 0xtbug -> 0*****
11+
* Example: Kiro -> K***
1412
*/
1513
export function maskEmail(input: string): string {
1614
if (!input) return '';
1715

18-
let result = input.replace(/([a-zA-Z0-9._-]+)(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi, (_, __, domainPart) => {
19-
return '******' + domainPart;
20-
});
16+
// Check if input contains an email pattern
17+
const hasEmail = /[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+/i.test(input);
2118

22-
result = result.replace(/\([a-zA-Z0-9-]+\)/g, '(******)');
19+
if (hasEmail) {
20+
// Mask email addresses
21+
let result = input.replace(/([a-zA-Z0-9._-]+)(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi, (_, __, domainPart) => {
22+
return '******' + domainPart;
23+
});
24+
25+
// Mask parenthesized content (like project IDs)
26+
result = result.replace(/\([a-zA-Z0-9-]+\)/g, '(******)');
27+
28+
return result;
29+
}
2330

24-
return result;
31+
// For non-email strings (usernames), keep first char and mask the rest
32+
if (input.length <= 1) return input;
33+
return input.charAt(0) + '*'.repeat(Math.min(input.length - 1, 5));
2534
}
2635

2736
/**

0 commit comments

Comments
 (0)