Skip to content

Commit

Permalink
Merge branch 'feat/mobile'
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsn committed May 7, 2023
2 parents 7ae1b72 + c0032c6 commit 7a14526
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/format/desensitizeMobile/demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

import { desensitizeMobile } from '@nmsn/utils';

export default () => {
const source = '13112345678';
const result = desensitizeMobile(source);
return (
<div>
<p>source: {JSON.stringify(source)}</p>
<p>result: {JSON.stringify(result)}</p>
</div>
);
};
29 changes: 29 additions & 0 deletions src/format/desensitizeMobile/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: desensitizeMobile
toc: false
---

## desensitizeMobile

给手机号码脱敏

<code src="./demo.tsx"></code>

### API

```typescript
const data = desensitizeMobile(mobile: string | number);
```

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ------------ | -------- | ------ |
| mobile | 入参,电话号码 | `string \| number` | - |


### Result

| 参数 | 说明 | 类型 | 默认值 |
| ---- | -------------- | -------- | ------ |
| data | 返回的脱敏电话号 | `string` | '' |
6 changes: 6 additions & 0 deletions src/format/desensitizeMobile/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import phoneDesensitization from '.';

it('phoneDesensitization', () => {
expect(phoneDesensitization('13111111111')).toEqual('131****1111');
expect(() => phoneDesensitization('')).toThrow();
});
14 changes: 14 additions & 0 deletions src/format/desensitizeMobile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import regex from '@/regex';

const desensitizeMobile = (mobile: string | number) => {
const _phone = mobile.toString();

if (!regex.mobile.test(_phone)) {
throw new Error('No valid mobile number');
}

const reg = /(\d{3})\d{4}(\d{4})/;
return _phone.replace(reg, '$1****$2');
};

export default desensitizeMobile;
1 change: 1 addition & 0 deletions src/format/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as addThousandSeparator } from './addThousandSeparator';
export { default as arr2Obj } from './arr2Obj';
export { default as desensitizeMobile } from './desensitizeMobile';
export { default as displayWithUnit } from './displayWithUnit';
export { default as filterObjAttrs } from './filterObjAttrs';
export { default as formatDecimals2Percent } from './formatDecimals2Percent';
Expand Down

1 comment on commit 7a14526

@vercel
Copy link

@vercel vercel bot commented on 7a14526 May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

utils – ./

utils-nmsn.vercel.app
utils-git-main-nmsn.vercel.app
nmsn-utils.vercel.app

Please sign in to comment.