-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new function - desensitizeEmail
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { desensitizeIDCard, desensitizeName, desensitizePhone } from './stringDesensitize' | ||
import { desensitizeEmail, desensitizeIDCard, desensitizeName, desensitizePhone } from './stringDesensitize' | ||
|
||
describe('stringDesensitize', () => { | ||
it('desensitizeName', () => { | ||
|
@@ -35,4 +35,14 @@ describe('stringDesensitize', () => { | |
expect(desensitizeIDCard('12345678901234567x', 'high')).toMatchInlineSnapshot(`"1****************x"`) | ||
expect(desensitizeIDCard('123456789012345678', 'high')).toMatchInlineSnapshot(`"1****************8"`) | ||
}) | ||
|
||
it('desensitizeEmail', () => { | ||
expect(desensitizeEmail()).toBeUndefined() | ||
expect(desensitizeEmail('')).toMatchInlineSnapshot(`""`) | ||
expect(desensitizeEmail('[email protected]')).toMatchInlineSnapshot(`"230****[email protected]"`) | ||
expect(desensitizeEmail('[email protected]')).toMatchInlineSnapshot(`"230****[email protected]"`) | ||
expect(desensitizeEmail('[email protected]')).toMatchInlineSnapshot(`"[email protected]"`) | ||
expect(desensitizeEmail('@gmail.com')).toMatchInlineSnapshot(`"@gmail.com"`) | ||
expect(desensitizeEmail('[email protected]')).toMatchInlineSnapshot(`"[email protected]"`) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,34 @@ export function desensitizeIDCard(idCard?: string, level: Level = 'low') { | |
|
||
return idCard.replace(/(\d{1})\d{16}([\dxX]{1})/, '$1****************$2') | ||
} | ||
|
||
/** | ||
* Desensitizes an email address by replacing part of the name with asterisks. | ||
* if the email address is not a string, it will be returned as is. | ||
* @param email - The email address to desensitize. | ||
* @returns The desensitized email address. | ||
* @example | ||
* ```ts | ||
* desensitizeEmail('[email protected]') // '230****[email protected] | ||
* desensitizeEmail('[email protected]') // '[email protected]' | ||
* desensitizeEmail('') // '' | ||
* ``` | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts | ||
*/ | ||
export function desensitizeEmail(email?: string) { | ||
if (!isString(email)) | ||
return email | ||
|
||
try { | ||
const [name, domain] = email.split('@') | ||
|
||
if (!name || !domain) | ||
return email | ||
|
||
return `${name.replace(/(\w{3})\w*(\w{2})/, '$1****$2')}@${domain}` | ||
} | ||
catch (err) { | ||
console.error(err) | ||
return email | ||
} | ||
} |