Skip to content

Commit

Permalink
feat: new function - formatterIdCard
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed May 24, 2024
1 parent 807864d commit be2d6e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pnpm add @utopia-utils/dom
* 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)
* formatterIdCard: 身份证呈格式化。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/stringFormatter.ts)

### 类型判断

Expand Down
11 changes: 10 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, formatterPhoneNumber } from './stringFormatter'
import { formatterBankCard, formatterIdCard, formatterPhoneNumber } from './stringFormatter'

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

it('formatterIdCard', () => {
expect(formatterIdCard('36072119941229004X')).toMatchInlineSnapshot(`"360721 19941229 004X"`)
expect(formatterIdCard(' fsd 36072119941229004X')).toMatchInlineSnapshot(`"360721 19941229 004X"`)
expect(formatterIdCard('')).toMatchInlineSnapshot(`""`)
expect(formatterIdCard('null')).toMatchInlineSnapshot(`""`)
// @ts-expect-error test undefined
expect(formatterIdCard(undefined)).toMatchInlineSnapshot(`""`)
})
})
25 changes: 25 additions & 0 deletions packages/core/src/stringFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,28 @@ export function formatterPhoneNumber(str: string) {
return `${$1} ${$2} ${$3}`
})
}

const ID_CARD_LENGTH = 18
/**
* Formats an ID card number by removing non-numeric characters and adding spaces for better readability.
* @param str - The ID card number to format.
* @returns The formatted ID card number.
* @example
* ```ts
* formatterIdCard('36072119941229004X') // '360721 19941229 004X'
* formatterIdCard(' fsd 36072119941229004X') // '360721 19941229 004X'
* formatterIdCard(undefined) // ''
* ```
*/
export function formatterIdCard(str: string) {
return `${str}`
.replace(/[^0-9xX]/g, '')
.substring(0, ID_CARD_LENGTH)
.replace(/(\d{6})(\d{0,8})?(\d{0,4})?/, (res, $1, $2, $3) => {
if (res.length <= 6)
return $1
if (res.length <= 14)
return `${$1} ${$2}`
return `${$1} ${$2} ${$3}`
})
}

0 comments on commit be2d6e3

Please sign in to comment.