feat(frontend): add card-to-iban interface
This commit is contained in:
23
frontend/app/utils/card.test.ts
Normal file
23
frontend/app/utils/card.test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { formatCard, normalizeCard, validCard } from './card'
|
||||
|
||||
describe('card utilities', () => {
|
||||
it('normalizes Persian and Arabic digits with common separators', () => {
|
||||
expect(normalizeCard('۶۰۳۷ ٩٩۷۵-۱۲۳۴ ۵۶۷۸')).toBe('6037997512345678')
|
||||
})
|
||||
|
||||
it('keeps unexpected characters so validation can report them', () => {
|
||||
expect(normalizeCard('6037 ABC 1234')).toBe('6037ABC1234')
|
||||
})
|
||||
|
||||
it('accepts only sixteen ASCII digits', () => {
|
||||
expect(validCard('6037997512345678')).toBe(true)
|
||||
expect(validCard('603799751234567')).toBe(false)
|
||||
expect(validCard('603799ABC2345678')).toBe(false)
|
||||
})
|
||||
|
||||
it('groups a normalized card for display', () => {
|
||||
expect(formatCard('6037997512345678')).toBe('6037 9975 1234 5678')
|
||||
})
|
||||
})
|
||||
23
frontend/app/utils/card.ts
Normal file
23
frontend/app/utils/card.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
const persianDigits = '۰۱۲۳۴۵۶۷۸۹'
|
||||
const arabicDigits = '٠١٢٣٤٥٦٧٨٩'
|
||||
|
||||
export function normalizeCard(value: string): string {
|
||||
return value
|
||||
.replace(/[۰-۹]/g, (digit) => String(persianDigits.indexOf(digit)))
|
||||
.replace(/[٠-٩]/g, (digit) => String(arabicDigits.indexOf(digit)))
|
||||
.replace(/[\s-]/g, '')
|
||||
}
|
||||
|
||||
export function validCard(value: string): boolean {
|
||||
return /^\d{16}$/.test(value)
|
||||
}
|
||||
|
||||
export function formatCard(value: string): string {
|
||||
const digits = normalizeCard(value).replace(/\D/g, '').slice(0, 16)
|
||||
|
||||
return digits.replace(/(\d{4})(?=\d)/g, '$1 ')
|
||||
}
|
||||
|
||||
export function formatIBAN(value: string): string {
|
||||
return value.replace(/(.{4})(?=.)/g, '$1 ')
|
||||
}
|
||||
Reference in New Issue
Block a user