Skip to content

Commit

Permalink
feat: new function - formatterPhoneNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed May 24, 2024
1 parent d2aed80 commit 807864d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pnpm add @utopia-utils/dom
* yuanToFen: 人民币:元转分。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/yuanToFen.ts)
* fenToYuan: 人民币:分转元。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/fenToYuan.ts)
* yuanFormat: 人民币格式化(单位默认是分,会进行分转元再格式化)。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/yuanFormat.ts)
* formatterBankCard: 银行卡号格式化。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts)
* formatterPhoneNumber: 手机号格式化。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts)

### 类型判断

```bash
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export * from './pipe'
export * from './randomInt'
export * from './randomString'
export * from './retry'
export * from './sleep'
export * from './sleep'mch
export * from './sort'
export * from './stringFormatter'
export * from './union'
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/stringFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { formatterBankCard } from './stringFormatter'
import { formatterBankCard, formatterPhoneNumber } from './stringFormatter'

describe('stringFormatter', () => {
it('formatterBankCard', () => {
Expand All @@ -13,4 +13,14 @@ describe('stringFormatter', () => {
// @ts-expect-error test undefined
expect(formatterBankCard(undefined)).toMatchInlineSnapshot(`""`)
})

it('formatterPhoneNumber', () => {
expect(formatterPhoneNumber('12345678901')).toMatchInlineSnapshot(`"123 4567 8901"`)
expect(formatterPhoneNumber('123456789012345678901')).toMatchInlineSnapshot(`"123 4567 8901"`)
expect(formatterPhoneNumber(' SD 123 4567 8901 23fdss ')).toMatchInlineSnapshot(`"123 4567 8901"`)
expect(formatterPhoneNumber('')).toMatchInlineSnapshot(`""`)
expect(formatterPhoneNumber('null')).toMatchInlineSnapshot(`""`)
// @ts-expect-error test undefined
expect(formatterPhoneNumber(undefined)).toMatchInlineSnapshot(`""`)
})
})
32 changes: 32 additions & 0 deletions packages/core/src/stringFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,39 @@
* Formats a bank card number by removing non-digit characters and adding spaces every four digits.
* @param str - The bank card number to be formatted.
* @returns The formatted bank card number.
* @example
* ```ts
* formatterBankCard('1234567890123456') // '1234 5678 9012 3456'
* formatterBankCard('_ 3232 32432 32432 ') // '3232 3243 2324 32'
* formatterBankCard(undefined) // ''
* ```
*/
export function formatterBankCard(str: string) {
return `${str}`.replace(/\D/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
}

const PHONE_LENGTH = 11
/**
* Formats a phone number string.
*
* @param str - The phone number string to format.
* @returns The formatted phone number string.
* @example
* ```ts
* formatterPhoneNumber('12345678901') // '123 4567 8901'
* formatterPhoneNumber(' asd 123456789012345678901') // '123 4567 8901'
* formatterPhoneNumber(undefined) // ''
* ```
*/
export function formatterPhoneNumber(str: string) {
return `${str}`
.replace(/\D/g, '')
.substring(0, PHONE_LENGTH)
.replace(/(\d{3})(\d{0,4})?(\d{0,4})?/, (res, $1, $2, $3) => {
if (res.length <= 3)
return $1
if (res.length <= 7)
return `${$1} ${$2}`
return `${$1} ${$2} ${$3}`
})
}

0 comments on commit 807864d

Please sign in to comment.