Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/mixin-deep-1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
champkeh committed May 8, 2021
2 parents 9a5b38a + 479793a commit c0e0187
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 32 deletions.
27 changes: 27 additions & 0 deletions @types/common/CommonUtil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,37 @@ declare function maskPhone(phone: string, start?: number, len?: number): string;
* // => '110410**********34'
*/
declare function maskIDCard(idcard: string, start?: number, len?: number): string;
/**
* 对邮箱进行去敏处理
* @param email
*/
declare function maskEMail(email: string): string;
/**
* 对姓名进行去敏处理
* @param name
*/
declare function maskName(name: string): string;
/**
* 对一个字符串进行去敏处理
* @param text 原始字符串(必填)
* @param start 开头保留多少位明文(必填)
* @param end 结尾保留多少位明文(必填)
* @param len 中间显示多少个*
*
* @example
* mask('123456', 2, 3)
* // => '12*456'
* mask('123456', 2, 3, 4)
* // => '12****456'
*/
declare function mask(text: string, start: number, end: number, len?: number): string;
declare const _default: {
phoneIsValid: typeof phoneIsValid;
maskPhone: typeof maskPhone;
idcardIsValid: typeof idcardIsValid;
maskIDCard: typeof maskIDCard;
mask: typeof mask;
maskName: typeof maskName;
maskEMail: typeof maskEMail;
};
export default _default;
4 changes: 2 additions & 2 deletions @types/string/StringUtil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ declare function swapCase(input: string): string;
* @returns {number} 统计后字符串的个数
*
* @example
* countMatches('dabddadb', 'da'); // 3
* countMatches('abcdeabcdeabcde','ab'); // 6
* countMatches('dabddadb', 'da'); // 2
* countMatches('abcdeabcdeabcde','ab'); // 3
*/
declare function countMatches(input: string, sub: string): number;
/**
Expand Down
30 changes: 18 additions & 12 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# 变更日志

## 0.5.0 / 2018-12-17
## 0.1.7 / 2020-12-22

- TS支持测试源文件,改用nyc
CommonUtil:

## 0.4.0 / 2018-11-27
- 增加 maskName 方法
- 增加 maskEMail 方法

- JS支持测试源文件,改用nyc
## 0.1.6 / 2020-12-22

## 0.3.0 / 2018-11-23
CommonUtil:

- 添加对typescript的支持
- 升级babel7
- 添加对测试覆盖率的支持
- 文档添加emoji
- 增加 mask 通用方法

## 0.1.5-rc.10 / 2019-03-22

CommonUtil:

- 增加 phoneIsValid 方法
- 增加 maskPhone 方法
- 增加 idcardIsValid 方法
- 增加 maskIDCard 方法

DateUtil:

## 0.2.0 / 2018-3-10

- 支持banner
- 添加es5-shim

## 0.1.0 / 2018-3-1

Expand Down
86 changes: 82 additions & 4 deletions doc/api.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
[maskPhone 对手机号进行脱敏处理](#2-maskPhone)<br>
[idcardIsValid 检查身份证是否合法](#3-idcardIsValid)<br>
[maskIDCard 对身份证进行脱敏处理](#4-maskIDCard)<br>
[mask](#5-mask)<br>
[mask 通用脱敏方法](#5-mask)<br>
[maskName 对姓名脱敏](#6-maskName)<br>
[maskEMail 对电子邮箱脱敏](#7-maskemail)<br>

<br>

Expand Down Expand Up @@ -387,13 +389,89 @@ maskIDCard('110410199001011234', 6, 10)
| 参数 | 类型 | 是否必传 | 默认值 | 说明 |
|:---|:----|:-----:|:------:|:---|
| text | String ||| 原始字符串 |
| start | Number ||| 开头显示明文字符数 |
| end | Number ||| 结尾显示明文字符数 |
| len | Number ||| 掩码字符长度,如果不传,则根据原始字符串长度计算剩余字符个数 |
| start | Number ||| 开头显示明文字符数(必须>=0) |
| end | Number ||| 结尾显示明文字符数(必须>=0) |
| len | Number ||| 掩码字符长度,如果不传,则根据原始字符串长度计算剩余字符个数(必须>=0) |

* 返回值<br>
**脱敏字符串**

* 举个例子

```js
mask('123456', 2, 3) // 前2后3,剩余字符用*代替
// => '12*456'
mask('123456', 2, 3, 4) // 前2后3,中间显示4个*
// => '12****456'
```

> 特别说明:无

### 6. maskName()

* 方法签名<br>
**maskName(name: string)**

* 方法详细介绍<br>
**对姓名进行去敏处理,用\*替换**

* 参数

| 参数 | 类型 | 是否必传 | 默认值 | 说明 |
|:---|:----|:-----:|:------:|:---|
| name | String ||| 姓名 |

* 返回值<br>
**脱敏字符串**

* 举个例子

```js
maskName('')
// => '李'
maskName('张三')
// => '张*'
maskName('张三丰')
// => '张*丰'
maskName('蒙奇D路飞')
// => '蒙*飞'
```

> 特别说明:
>

### 7. maskEMail()

* 方法签名<br>
**maskEMail(email: string)**

* 方法详细介绍<br>
**对电子邮箱进行去敏处理,用\*替换**

* 参数

| 参数 | 类型 | 是否必传 | 默认值 | 说明 |
|:---|:----|:-----:|:------:|:---|
| email | String ||| 电子邮箱 |

* 返回值<br>
**脱敏字符串**

* 举个例子

```js
maskEMail('123456@jz-ins.cn')
// => '12***56@jz-ins.cn'
maskEMail('123@jz-ins.cn')
// => '12***23@jz-ins.cn'
```

> 特别说明:
> 邮箱中的名字部分会采用 `mask(2, 2, 3)` 的方式进行去敏


---

Expand Down
19 changes: 7 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sn-js-utils",
"version": "0.1.5-rc.10",
"version": "0.1.7",
"description": "JS | TS 常用工具类库",
"main": "dist/index.js",
"jsnext:main": "dist/index.esm.js",
Expand Down
32 changes: 31 additions & 1 deletion src/common/CommonUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ function maskIDCard(idcard: string, start: number = 6, len: number = 8): string
return idcard.substr(0, start) + '*'.repeat(len) + idcard.substring(start + len);
}

/**
* 对邮箱进行去敏处理
* @param email
*/
function maskEMail(email: string): string {
return email && email.replace(/(.*?)(@.+)/g, (_: string, name: string, suffix: string): string => {
return mask(name, 2, 2, 3) + suffix;
});
}

/**
* 对姓名进行去敏处理
* @param name
*/
function maskName(name: string): string {
if (!name) {
return name;
}

if (name.length <= 2) {
return mask(name, 1, 0);
} else {
return mask(name, 1, 1, 1);
}
}

/**
* 对一个字符串进行去敏处理
* @param text 原始字符串(必填)
Expand All @@ -135,7 +161,7 @@ function maskIDCard(idcard: string, start: number = 6, len: number = 8): string
* mask('123456', 2, 3, 4)
* // => '12****456'
*/
function mask(text: string, start: number, end: number, len: number): string {
function mask(text: string, start: number, end: number, len?: number): string {
if (!text) {
return text;
}
Expand All @@ -158,10 +184,14 @@ function mask(text: string, start: number, end: number, len: number): string {
}
}



export default {
phoneIsValid,
maskPhone,
idcardIsValid,
maskIDCard,
mask,
maskName,
maskEMail,
};
28 changes: 28 additions & 0 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,32 @@ describe('测试 CommonUtil 工具类', function () {
expect(utils.CommonUtil.mask('123456', 4, 3, 6)).to.be.equal('1234******456');
});
});

describe('测试 maskName 方法', function () {
it('正常输入', function () {
expect(utils.CommonUtil.maskName('123456')).to.be.equal('1*6');
expect(utils.CommonUtil.maskName('12')).to.be.equal('1*');
expect(utils.CommonUtil.maskName('张三')).to.be.equal('张*');
expect(utils.CommonUtil.maskName('张三丰')).to.be.equal('张*丰');
expect(utils.CommonUtil.maskName('蒙奇D路飞')).to.be.equal('蒙*飞');
});
it('异常输入', function () {
expect(utils.CommonUtil.maskName('')).to.be.equal('');
expect(utils.CommonUtil.maskName('张')).to.be.equal('张');
});
});

describe('测试 maskEMail 方法', function () {
it('正常输入', function () {
expect(utils.CommonUtil.maskEMail('123456@jz-ins.cn')).to.be.equal('12***56@jz-ins.cn');
expect(utils.CommonUtil.maskEMail('12@jz-ins.cn')).to.be.equal('12***12@jz-ins.cn');
expect(utils.CommonUtil.maskEMail('张三@jz-ins.cn')).to.be.equal('张三***张三@jz-ins.cn');
expect(utils.CommonUtil.maskEMail('张三丰@jz-ins.cn')).to.be.equal('张三***三丰@jz-ins.cn');
expect(utils.CommonUtil.maskEMail('蒙奇D路飞@jz-ins.cn')).to.be.equal('蒙奇***路飞@jz-ins.cn');
});
it('异常输入', function () {
expect(utils.CommonUtil.maskEMail('')).to.be.equal('');
expect(utils.CommonUtil.maskEMail('张')).to.be.equal('张');
});
});
});

0 comments on commit c0e0187

Please sign in to comment.