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

MWPW-158674 [MEP] Support nested placholders in MEP placeholders #2908

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,21 @@ export function handleFragmentCommand(command, a) {
return false;
}

export function parseNestedPlaceholders({ placeholders }) {
if (!placeholders) return;
Object.entries(placeholders).forEach(([key, value]) => {
const matches = value.match(/{{(.*?)}}/g);
if (matches) {
matches.forEach((match) => {
const foundKey = match.replace(/{{|}}/g, '').trim();
if (placeholders[foundKey]) {
placeholders[key] = placeholders[key].replace(match, placeholders[foundKey]);
}
});
}
});
}

export async function applyPers(manifests, postLCP = false) {
if (!manifests?.length) return;
let experiments = manifests;
Expand All @@ -935,6 +950,7 @@ export async function applyPers(manifests, postLCP = false) {
}

experiments = cleanAndSortManifestList(experiments);
parseNestedPlaceholders(config);

let results = [];

Expand Down
19 changes: 19 additions & 0 deletions test/features/personalization/parseNestedPlaceholders.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from '@esm-bundle/chai';
import { parseNestedPlaceholders } from '../../../libs/features/personalization/personalization.js';

const config = {
placeholders: {
'promo-product-name': 'CC All Apps',
'promo-header': 'Buy now and save {{promo-discount}}% off {{promo-product-name}}.',
'promo-discount': '50',
'promo-description': 'For just {{promo-price}}, get 20+...',
'promo-price': 'US$49.99',
},
};
describe('test different values', () => {
it('should update placeholders', () => {
parseNestedPlaceholders(config);
expect(config.placeholders['promo-header']).to.equal('Buy now and save 50% off CC All Apps.');
expect(config.placeholders['promo-description']).to.equal('For just US$49.99, get 20+...');
});
});
Loading