Skip to content

Commit 3d383f1

Browse files
authored
fix: only replace the last occurrence of decimal point (#35)
Fixes #32 (again)
1 parent 6caacc9 commit 3d383f1

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/lib/CurrencyInput.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
// Don't format if the user is typing a `currencyDecimal` point
5252
if (event.key === currencyDecimal) return;
5353
54-
// Always convert _the opposite_ decimal key press to the `currencyDecimal` point
54+
// Pressing `.` when the decimal point is `,` gets replaced with `,`
5555
if (isDecimalComma && event.key === '.')
56-
formattedValue = formattedValue.replace('.', currencyDecimal);
56+
formattedValue = formattedValue.replace(/\.([^.]*)$/, currencyDecimal + '$1'); // Only replace the last occurence
57+
58+
// Pressing `,` when the decimal point is `.` gets replaced with `.`
5759
if (!isDecimalComma && event.key === ',')
58-
formattedValue = formattedValue.replace(',', currencyDecimal);
60+
formattedValue = formattedValue.replace(/\,([^,]*)$/, currencyDecimal + '$1'); // Only replace the last occurence
5961
6062
// Don't format if `formattedValue` is ['$', '-$', "-"]
6163
const ignoreSymbols = [currencySymbol, `-${currencySymbol}`, '-'];

tests/svelte-currency-input.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ test.describe('CurrencyInput', () => {
231231
await page.goto('/');
232232

233233
// Pressing `.` when the decimal point is `,` gets converted to `,`
234-
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
235-
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
236-
await colonFormattedInput.focus();
234+
const euroFormattedInput = page.locator('.currencyInput__formatted[name="formatted-euro"]');
235+
const euroUnformattedInput = page.locator('.currencyInput__unformatted[name=euro]');
236+
await euroFormattedInput.focus();
237237
await selectAll(page);
238238
await page.keyboard.press('Backspace');
239-
await page.keyboard.type('-69.42');
240-
await expect(colonFormattedInput).toHaveValue('-₡69,42');
241-
await expect(colonUnformattedInput).toHaveValue('-69.42');
239+
await page.keyboard.type('-42069.69');
240+
await expect(euroFormattedInput).toHaveValue('-42.069,69 €');
241+
await expect(euroUnformattedInput).toHaveValue('-42069.69');
242242

243243
// Pressing `,` when the decimal point is `.` gets converted to `.`
244244
const bitcoinUnformattedInput = page.locator('.currencyInput__unformatted[name=bitcoin]');
@@ -248,9 +248,9 @@ test.describe('CurrencyInput', () => {
248248
await bitcoinFormattedInput.focus();
249249
await selectAll(page);
250250
await page.keyboard.press('Backspace');
251-
await page.keyboard.type('69,42');
252-
await expect(bitcoinFormattedInput).toHaveValue('฿69.42');
253-
await expect(bitcoinUnformattedInput).toHaveValue('69.42');
251+
await page.keyboard.type('42069,69');
252+
await expect(bitcoinFormattedInput).toHaveValue('฿42,069.69');
253+
await expect(bitcoinUnformattedInput).toHaveValue('42069.69');
254254
});
255255

256256
test.skip('Updating chained inputs have the correct behavior', async () => {

0 commit comments

Comments
 (0)