mirror of
https://github.com/sky-systems/sky_phone.git
synced 2026-09-04 09:13:24 +00:00
FIX - polish banking interactions and transaction details
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
normalizeBankingAmountInput,
|
||||
parseBankingAmount,
|
||||
} from '@/utils/bankingAmount'
|
||||
|
||||
describe('banking amount input', () => {
|
||||
it('uses a decimal comma and strips native number-input noise', () => {
|
||||
expect(normalizeBankingAmountInput('1.')).toBe('1,')
|
||||
expect(normalizeBankingAmountInput('12.00')).toBe('12,00')
|
||||
expect(normalizeBankingAmountInput('EUR 12,00')).toBe('12,00')
|
||||
expect(normalizeBankingAmountInput('12,0,0')).toBe('12,00')
|
||||
})
|
||||
|
||||
it('keeps the server-authoritative whole-money contract', () => {
|
||||
expect(parseBankingAmount('125')).toBe(125)
|
||||
expect(parseBankingAmount('125,00')).toBe(125)
|
||||
expect(parseBankingAmount('125,50')).toBeNull()
|
||||
expect(parseBankingAmount('0')).toBeNull()
|
||||
expect(parseBankingAmount('')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
export function normalizeBankingAmountInput(value: string): string {
|
||||
const normalizedSeparators = value.replace(/\./g, ',')
|
||||
const digitsAndSeparators = normalizedSeparators.replace(/[^\d,]/g, '')
|
||||
const separatorIndex = digitsAndSeparators.indexOf(',')
|
||||
|
||||
if (separatorIndex < 0) return digitsAndSeparators
|
||||
|
||||
const whole = digitsAndSeparators.slice(0, separatorIndex)
|
||||
const decimal = digitsAndSeparators
|
||||
.slice(separatorIndex + 1)
|
||||
.replace(/,/g, '')
|
||||
.slice(0, 2)
|
||||
|
||||
return `${whole},${decimal}`
|
||||
}
|
||||
|
||||
export function parseBankingAmount(value: string): number | null {
|
||||
if (!/^\d+(?:,\d{1,2})?$/.test(value)) return null
|
||||
|
||||
const amount = Number(value.replace(',', '.'))
|
||||
if (!Number.isSafeInteger(amount) || amount <= 0) return null
|
||||
|
||||
return amount
|
||||
}
|
||||
Reference in New Issue
Block a user