Skip to content

Commit

Permalink
feat(utils): 新增 desensitize 文本脱敏
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 15, 2023
1 parent 10e5f21 commit e5d056f
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/utils/desensitize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { DesensitizeStrategy, desensitize } from './desensitize'

describe('desensitize', () => {
test('CHINESE_NAME', () => {
expect(
desensitize('成龙', {
strategy: DesensitizeStrategy.CHINESE_NAME,
}),
).toBe('成*')
expect(
desensitize('成龙', {
strategy: DesensitizeStrategy.CHINESE_NAME,
replacer: '#',
}),
).toBe('成#')
expect(
desensitize('成龙', {
strategy: DesensitizeStrategy.CHINESE_NAME,
replacer: '##&',
}),
).toBe('成##&')
expect(
desensitize('卡尔·滴滴末', {
strategy: DesensitizeStrategy.CHINESE_NAME,
}),
).toBe('卡*****')
})

test('CHINESE_ID_CARD_NUMBER', () => {
expect(
desensitize('5222404', {
strategy: DesensitizeStrategy.CHINESE_ID_CARD_NUMBER,
}),
).toBe('5****04')
})

test('CHINESE_MOBILE_PHONE_NUMBER', () => {
expect(
desensitize('1833339998', {
strategy: DesensitizeStrategy.CHINESE_MOBILE_PHONE_NUMBER,
}),
).toBe('183***9998')
})

test('EMAIL', () => {
expect(
desensitize('hell@gmail.com', {
strategy: DesensitizeStrategy.EMAIL,
}),
).toBe('h***@gmail.com')
})

test('preKeep', () => {
expect(
desensitize('hell@gmail.com', {
preKeep: 2,
}),
).toBe('he************')
expect(
desensitize('hell@gmail.com', {
preKeep: 0,
}),
).toBe('**************')
})

test('postKeep', () => {
expect(
desensitize('hell@gmail.com', {
postKeep: 2,
}),
).toBe('************om')
expect(
desensitize('hell@gmail.com', {
postKeep: 0,
}),
).toBe('**************')
})

test('preKeep & postKeep', () => {
expect(
desensitize('hell@gmail.com', {
preKeep: 1,
postKeep: 2,
}),
).toBe('h***********om')
})

test('default', () => {
expect(desensitize('hell@gmail.com')).toBe('**************')
})

test('empty', () => {
expect(desensitize('')).toBe('')
})
})
77 changes: 77 additions & 0 deletions src/utils/desensitize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export enum DesensitizeStrategy {
CHINESE_NAME = 'CHINESE_NAME',
CHINESE_ID_CARD_NUMBER = 'CHINESE_ID_CARD_NUMBER',
CHINESE_MOBILE_PHONE_NUMBER = 'CHINESE_MOBILE_PHONE_NUMBER',
EMAIL = 'EMAIL',
}

export interface DesensitizeOptions {
/**
* 脱敏策略
*/
strategy?: DesensitizeStrategy
/**
* 脱敏替换字符
*
* @default '*'
*/
replacer?: string
/**
* 前置保留字符数
*
* @default 0
*/
preKeep?: number
/**
* 后置保留字符数
*
* @default 0
*/
postKeep?: number
}

function replace(text: string, start: number, end: number, replacer: string) {
let res = text.substring(0, start)
for (let i = start; i < end; i++) {
res += replacer
}
res += text.substring(end)
return res
}

/**
* 文本脱敏。
*
* @param text 待脱敏的文本
* @param options 脱敏选项
*/
export function desensitize(
text: string,
options: DesensitizeOptions = {},
): string {
if (!text) return text
const replacer = options?.replacer ?? '*'
if (options.strategy) {
if (options.strategy === DesensitizeStrategy.CHINESE_NAME) {
return replace(text, 1, text.length, replacer)
}
if (options.strategy === DesensitizeStrategy.CHINESE_ID_CARD_NUMBER) {
return replace(text, 1, text.length - 2, replacer)
}
if (options.strategy === DesensitizeStrategy.CHINESE_MOBILE_PHONE_NUMBER) {
return replace(text, 3, text.length - 4, replacer)
}
if (options.strategy === DesensitizeStrategy.EMAIL) {
return replace(text, 1, text.indexOf('@'), replacer)
}
}
if (options.preKeep != null || options.postKeep != null) {
return replace(
text,
options.preKeep ?? 0,
text.length - (options.postKeep ?? 0),
replacer,
)
}
return replace(text, 0, text.length, replacer)
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from './createUrlQueryString'
export * from './DataPacker'
export * from './dedent'
export * from './defaultIndexTo'
export * from './desensitize'
export * from './devOrProd'
export * from './EventBus'
export * from './formatBytes'
Expand Down

0 comments on commit e5d056f

Please sign in to comment.