Skip to content

Commit

Permalink
feat: new function - desensitizeIdCard
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed May 24, 2024
1 parent 035ec85 commit 082e5bc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pnpm add @utopia-utils/dom
* formatterIdCard: 身份证呈格式化。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts)
* 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)

### 类型判断

Expand Down
19 changes: 18 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 { desensitizeName, desensitizePhone } from './stringDesensitize'
import { desensitizeIDCard, desensitizeName, desensitizePhone } from './stringDesensitize'

describe('stringDesensitize', () => {
it('desensitizeName', () => {
Expand All @@ -18,4 +18,21 @@ describe('stringDesensitize', () => {
expect(desensitizePhone('')).toBe('')
expect(desensitizePhone('12345678910')).toMatchInlineSnapshot(`"123****8910"`)
})

it('desensitizeIDCard', () => {
expect(desensitizeIDCard()).toBeUndefined()
expect(desensitizeIDCard('')).toBe('')

expect(desensitizeIDCard('12345678901234567X')).toMatchInlineSnapshot(`"123456********567X"`)
expect(desensitizeIDCard('12345678901234567x')).toMatchInlineSnapshot(`"123456********567x"`)
expect(desensitizeIDCard('123456789012345678')).toMatchInlineSnapshot(`"123456********5678"`)

expect(desensitizeIDCard('12345678901234567X', 'medium')).toMatchInlineSnapshot(`"123************67X"`)
expect(desensitizeIDCard('12345678901234567x', 'medium')).toMatchInlineSnapshot(`"123************67x"`)
expect(desensitizeIDCard('123456789012345678', 'medium')).toMatchInlineSnapshot(`"123************678"`)

expect(desensitizeIDCard('12345678901234567X', 'high')).toMatchInlineSnapshot(`"1****************X"`)
expect(desensitizeIDCard('12345678901234567x', 'high')).toMatchInlineSnapshot(`"1****************x"`)
expect(desensitizeIDCard('123456789012345678', 'high')).toMatchInlineSnapshot(`"1****************8"`)
})
})
35 changes: 35 additions & 0 deletions packages/core/src/stringDesensitize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isString } from '@utopia-utils/share'

type Level = 'low' | 'medium' | 'high'

/**
* Desensitizes a name,
* If the name is not a string, it will be returned as is.
Expand Down Expand Up @@ -48,3 +50,36 @@ export function desensitizePhone(phone?: string) {

return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

/**
* 根据指定的级别对身份证号进行脱敏处理。
* 如果身份证号不是字符串,则原样返回。
*
* 高级:保留前一位与后一位,其余 * 表示,仅能识别该人属于哪个地区。 6*************2
*
* 中级:保留前三位与后三位,其余 * 表示,仅能识别该人的省市与是男是女。213***********203
*
* 低级:保留前六位与后四位,其余 * 表示,仅能识别该人的省市区与是男是女。212912******2233
* @param idCard - 要脱敏的身份证号。
* @param level - 脱敏级别。可以是 'low'、'medium' 或 'high'。默认为 'low'。
* @returns 脱敏后的身份证号。
* @example
* ```ts
* desensitizeIDCard('110101199001011234', 'low') // '110101********1234'
* desensitizeIDCard('110101199001011234', 'medium') // '110************234'
* desensitizeIDCard('110101199001011234', 'high') // '1*****************4'
* desensitizeIDCard(undefined) // undefined
* ```
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringDesensitize.ts
*/
export function desensitizeIDCard(idCard?: string, level: Level = 'low') {
if (!isString(idCard))
return idCard

if (level === 'low')
return idCard.replace(/(\d{6})\d{8}([\dxX]{4})/, '$1********$2')
else if (level === 'medium')
return idCard.replace(/(\d{3})\d{12}([\dxX]{3})/, '$1************$2')

return idCard.replace(/(\d{1})\d{16}([\dxX]{1})/, '$1****************$2')
}

0 comments on commit 082e5bc

Please sign in to comment.