-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new function - formatterBankCard
- Loading branch information
Showing
3 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(`""`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ') | ||
} |