Skip to content

Commit

Permalink
feat: new function - desensitizeEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed May 24, 2024
1 parent 082e5bc commit cec6bd0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pnpm add @utopia-utils/dom
* desensitizeName: 姓名脱敏。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts)
* desensitizePhone: 手机号脱敏。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts)
* desensitizeIdCard: 身份证脱敏。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts)
* desensitizeEmail: 邮箱脱敏。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts)

### 类型判断

Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/stringDesensitize.test.ts
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', () => {
Expand Down Expand Up @@ -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]"`)
})
})
31 changes: 31 additions & 0 deletions packages/core/src/stringDesensitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit cec6bd0

Please sign in to comment.