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

Add support for multiple QUERY patterns in Value utility #464

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions libs/acf/util/src/lib/value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ describe('getValue', () => {
const result = await Value.getValue('<sessionCount>');
expect(result).toBe('10');
});

it('should handle multiple QUERY patterns correctly', async () => {
const searchParams = new URLSearchParams();
searchParams.set('param1', 'value1');
searchParams.set('param2', 'value2');
const originalSearch = window.location.search;
window.history.replaceState({}, '', `${window.location.pathname}?${searchParams.toString()}`);

const result = await Value.getValue('<query::param1> and <query::param2>');
expect(result).toBe('value1 and value2');

window.history.replaceState({}, '', `${window.location.pathname}${originalSearch}`);
});
});
12 changes: 12 additions & 0 deletions libs/acf/util/src/lib/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

export const VALUE_MATCHER = {
QUERY_PARAM: /^Query::/i,
QUERY: /<query::(.*?)>/gi,
API: /^Api::/i,
RANDOM: /<random(.+?)(\/[a-z]+)?>/gi,
BATCH_REPEAT: /<batchRepeat>/,
Expand Down Expand Up @@ -47,6 +48,14 @@
return value;
};

const getMultiQueryParam = (value: string) => {
value = value.replace(VALUE_MATCHER.QUERY, (_, key) => {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.get(key) || key;
});
Fixed Show fixed Hide fixed
return value;
};

const getApiValue = (value: string): string => {
const [, key] = value.split('::');
const apiValue = window.__api?.[key];
Expand Down Expand Up @@ -76,6 +85,9 @@
if (VALUE_MATCHER.QUERY_PARAM.test(value)) {
value = getQueryParam(value);
}
if (VALUE_MATCHER.QUERY.test(value)) {
Dismissed Show dismissed Hide dismissed
value = getMultiQueryParam(value);
}
if (VALUE_MATCHER.BATCH_REPEAT.test(value)) {
value = getBatchRepeat(value);
}
Expand Down
Loading