Skip to content

Commit

Permalink
fix(exchange): optional whitespaces in exchange query detection
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski committed Oct 25, 2024
1 parent 438fac7 commit 4e573da
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib/functions/query/gadgets/exchange.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCurrencyCode } from '$lib/functions/gadgets/exchange';

const regex =
/^(?<amount>\d+(\.\d+)?) (?<from>[a-zA-Z]{3}|[a-zA-Z ]+) (?:to|in) (?<to>[a-zA-Z]{3}|[a-zA-Z ]+)$/;
/^(?<amount>\d+(\.\d+)?)(?:\s*)(?<from>[a-zA-Z]{3}|[a-zA-Z ]+)(?:\s*)(?:to|in)(?:\s*)(?<to>[a-zA-Z]{3}|[a-zA-Z ]+)$/;

/**
* Check if query is an exchange gadget query
Expand All @@ -14,8 +14,8 @@ export function exchangery(query) {
const match = query.match(regex);
if (match === null || match.groups === undefined) return false;

const fromCode = getCurrencyCode(match.groups['from']);
const toCode = getCurrencyCode(match.groups['to']);
const fromCode = getCurrencyCode(match.groups.from);
const toCode = getCurrencyCode(match.groups.to);
return fromCode !== undefined && toCode !== undefined;
}

Expand All @@ -37,9 +37,9 @@ export function extractExchangeQuery(query) {
throw new Error('Invalid exchange query');
}

const from = getCurrencyCode(match.groups['from']);
const to = getCurrencyCode(match.groups['to']);
const amount = parseFloat(match.groups['amount']);
const from = getCurrencyCode(match.groups.from);
const to = getCurrencyCode(match.groups.to);
const amount = Number.parseFloat(match.groups.amount);
if (from === undefined || to === undefined) {
throw new Error('Invalid exchange query');
}
Expand Down

0 comments on commit 4e573da

Please sign in to comment.