Skip to content

Commit

Permalink
Merge pull request #26 from VueBricks/master
Browse files Browse the repository at this point in the history
🚀v1.4.5 敏感信息加符号
  • Loading branch information
linyisonger authored Apr 19, 2024
2 parents 43542ef + 9c34482 commit 00b4fb1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 🚀v1.4.5 敏感信息加符号
➕: sensitivePlusSymbol 敏感信息加符号

### 🚀v1.4.4 修复身份证号码X结尾报错
🔧: 修复身份证号码X结尾报错

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@3r/tool",
"version": "1.4.4",
"version": "1.4.5",
"description": "🏃‍包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节...持续更新整合",
"main": "index.js",
"type": "module",
Expand Down
31 changes: 30 additions & 1 deletion src/lib/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,35 @@ export class Convertor {
throw Error('array length error .')
} else return [val, val, val, val]
}

/**
* 敏感信息加符号
* @param str
* @param range
* @param symbol
*/
static sensitivePlusSymbol(str: string, range: [] | [[]] | string, symbol = '*') {
let res = str.split('');
let rangeList = []
if (Array.isArray(range)) {
range = range.toString();
}
rangeList = range.split(',').map(item => +item)
// 非法参数
if (rangeList.length % 2 == 1 || rangeList.length == 0) return str;

Check failure on line 477 in src/lib/convertor.ts

View workflow job for this annotation

GitHub Actions / build

Expected '===' and instead saw '=='

Check failure on line 477 in src/lib/convertor.ts

View workflow job for this annotation

GitHub Actions / build

Expected '===' and instead saw '=='

for (let i = 0; i < str.length; i++) {
for (let j = 0; j < rangeList.length; j += 2) {
const min = rangeList[j];
const max = rangeList[j + 1];
if (min <= i && i < max) {
res[i] = symbol
}
}
}
return res.join('')
}

}

[
Expand All @@ -478,7 +507,7 @@ export class Convertor {
{ name: 'camelcaseToSnakeCase', prototype: String.prototype, type: 'property' },
{ name: 'getConstellationByDate', prototype: String.prototype, type: 'property' },
{ name: 'getConstellationByDate', prototype: Date.prototype, type: 'property' },
{ name: 'citizenIdentificationNumberParse', prototype: String.prototype, type: 'property' }
{ name: 'citizenIdentificationNumberParse', prototype: String.prototype, type: 'property' },
].forEach(item => {
Object.defineProperty(item.prototype, item.name, {
get: function () {
Expand Down
14 changes: 11 additions & 3 deletions test/convertor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let run = function () {
console.log('身份证号解析', '230504199607116664'.citizenIdentificationNumberParse);
console.log('字节转换', Convertor.byteFormat(1099511627776, 2));
console.log('四值法拆分', Convertor.fourValueSplit(1));
console.log('敏感信息加符号', Convertor.sensitivePlusSymbol('230504199607116664', '6,14'));
}
try {
describe('转换模块', function () {
Expand Down Expand Up @@ -141,17 +142,17 @@ try {
birthday: '1996/07/11',
gender: '女',
constellation: '巨蟹座',
age: 27
age: new Date().getFullYear() - 1996
})
expect('230504199607116664'.citizenIdentificationNumberParse).toEqual({
reginCode: '230504',
birthday: '1996/07/11',
gender: '女',
constellation: '巨蟹座',
age: 27
age: new Date().getFullYear() - 1996
})
expect('420503198804097532'.citizenIdentificationNumberParse).toEqual({
"age": 35,
"age": new Date().getFullYear() - 1988,
"birthday": "1988/04/09",
"constellation": "白羊座",
"gender": "男",
Expand All @@ -178,6 +179,13 @@ try {
expect(Convertor.fourValueSplit([1, 2, 3, 4])).toEqual([1, 2, 3, 4])
expect(() => Convertor.fourValueSplit([1, 2, 3, 4, 5])).toThrow()
})
it('敏感信息加符号', function () {
expect(Convertor.sensitivePlusSymbol('230504199607116664', '6,14')).toEqual('230504********6664')
expect(Convertor.sensitivePlusSymbol('18602736144', [3, 7])).toEqual('186****6144')
expect(Convertor.sensitivePlusSymbol('18602736144', [[3, 7], [8, 11]])).toEqual('186****6***')
expect(Convertor.sensitivePlusSymbol('18602736144', [[3, 7, 3]])).toEqual('18602736144')
})

})
} catch (error) {
// describe is not defined 无需理会 调用方式不一致
Expand Down

0 comments on commit 00b4fb1

Please sign in to comment.