-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
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
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('[email protected]', { | ||
strategy: DesensitizeStrategy.EMAIL, | ||
}), | ||
).toBe('h***@gmail.com') | ||
}) | ||
|
||
test('preKeep', () => { | ||
expect( | ||
desensitize('[email protected]', { | ||
preKeep: 2, | ||
}), | ||
).toBe('he************') | ||
expect( | ||
desensitize('[email protected]', { | ||
preKeep: 0, | ||
}), | ||
).toBe('**************') | ||
}) | ||
|
||
test('postKeep', () => { | ||
expect( | ||
desensitize('[email protected]', { | ||
postKeep: 2, | ||
}), | ||
).toBe('************om') | ||
expect( | ||
desensitize('[email protected]', { | ||
postKeep: 0, | ||
}), | ||
).toBe('**************') | ||
}) | ||
|
||
test('preKeep & postKeep', () => { | ||
expect( | ||
desensitize('[email protected]', { | ||
preKeep: 1, | ||
postKeep: 2, | ||
}), | ||
).toBe('h***********om') | ||
}) | ||
|
||
test('default', () => { | ||
expect(desensitize('[email protected]')).toBe('**************') | ||
}) | ||
|
||
test('empty', () => { | ||
expect(desensitize('')).toBe('') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -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) | ||
} |
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