diff --git a/README.md b/README.md index 43720cc..b47ce7f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 2ba2bf2..58fa71b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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' diff --git a/packages/core/src/stringFormatter.test.ts b/packages/core/src/stringFormatter.test.ts index fce22b6..1037947 100644 --- a/packages/core/src/stringFormatter.test.ts +++ b/packages/core/src/stringFormatter.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -import { formatterBankCard } from './stringFormatter' +import { formatterBankCard, formatterPhoneNumber } from './stringFormatter' describe('stringFormatter', () => { it('formatterBankCard', () => { @@ -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(`""`) + }) }) diff --git a/packages/core/src/stringFormatter.ts b/packages/core/src/stringFormatter.ts index 1236ce0..10dcd72 100644 --- a/packages/core/src/stringFormatter.ts +++ b/packages/core/src/stringFormatter.ts @@ -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}` + }) +}