Skip to content

Commit

Permalink
feat: new function - formatterBankCard
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed May 23, 2024
1 parent 7f4e6f2 commit d2aed80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from './randomString'
export * from './retry'
export * from './sleep'
export * from './sort'
export * from './stringFormatter'
export * from './union'
export * from './unique'
export * from './uniqueWith'
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/stringFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'

import { formatterBankCard } from './stringFormatter'

describe('stringFormatter', () => {
it('formatterBankCard', () => {
expect(formatterBankCard('1234567890123456')).toMatchInlineSnapshot(`"1234 5678 9012 3456"`)
expect(formatterBankCard(' 1234 56 7890 23456')).toMatchInlineSnapshot(`"1234 5678 9023 456"`)
expect(formatterBankCard('_ 3232 32432 32432 ')).toMatchInlineSnapshot(`"3232 3243 2324 32"`)
expect(formatterBankCard('_ Sdj 32 32432 jds3232ds ')).toMatchInlineSnapshot(`"3232 4323 232"`)
expect(formatterBankCard('')).toMatchInlineSnapshot(`""`)
expect(formatterBankCard('null')).toMatchInlineSnapshot(`""`)
// @ts-expect-error test undefined
expect(formatterBankCard(undefined)).toMatchInlineSnapshot(`""`)
})
})
8 changes: 8 additions & 0 deletions packages/core/src/stringFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
*/
export function formatterBankCard(str: string) {
return `${str}`.replace(/\D/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
}

0 comments on commit d2aed80

Please sign in to comment.