Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize match currencies #345

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions packages/server/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ export function matchCurrencies(
currencies: Currency[],
patterns: string[],
): Currency[] {
const matchedCurrencies: Currency[] = [];
const patternCount = patterns.length;
const currencyCount = currencies.length;
const matchers = patterns
.filter((pattern) => pattern)
.map((pattern) => picomatch(pattern));
Comment on lines +8 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably do both filtering and mapping in one loop

Suggested change
const matchers = patterns
.filter((pattern) => pattern)
.map((pattern) => picomatch(pattern));
const matchers = patterns.reduce((filtered, pattern) => {
if (pattern) {
filtered.push(picomatch(pattern));
}
return filtered;
}, []);


for (let i = 0; i < patternCount; i += 1) {
const currentPattern = patterns[i];
if (currentPattern) {
const isMatch = picomatch(currentPattern);

for (let j = 0; j < currencyCount; j += 1) {
const currentCurrency = currencies[j];
if (currentCurrency) {
if (isMatch(currentCurrency.id)) {
matchedCurrencies.push(currentCurrency);
}
}
}
}
}
return matchedCurrencies;
return currencies.filter((currency) =>
matchers.some((matcher) => matcher(currency.id)),
);
}

export function filterAccountsForCurrencies(
Expand Down
6 changes: 3 additions & 3 deletions packages/simulator/tests/simulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ describe("Simulator", () => {
// THEN
expect(currencies).toBeDefined();
expect(currencies.length).toBe(currencyIds.length);
expect(currencies[0]?.id).toBe(currencyIds[1]);
expect(currencies[1]?.id).toBe(currencyIds[0]);
// Notice that the order of the list isn't defined by the order of the arguments in the query
expect(currencies.map(({ id }) => id)).toEqual(
expect.arrayContaining(currencyIds),
);
});

it("should throw an error if permission not granted", async () => {
Expand Down
Loading