Skip to content

Commit

Permalink
Refactor into reusable function.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdobeLinhart committed Sep 19, 2024
1 parent 4e52235 commit 3bd6955
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
25 changes: 14 additions & 11 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ export const checkCustomPlaceholders = (content) => {
return newContent;

Check warning on line 164 in libs/features/personalization/personalization.js

View check run for this annotation

Codecov / codecov/patch

libs/features/personalization/personalization.js#L152-L164

Added lines #L152 - L164 were not covered by tests
};

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

export const createContent = (el, content, manifestId, targetManifestId, action, modifiers) => {
if (action === 'replace') {
addIds(el, manifestId, targetManifestId);
Expand All @@ -174,7 +185,8 @@ export const createContent = (el, content, manifestId, targetManifestId, action,
return el;
}
if (getSelectorType(content) !== 'fragment') {
const newContent = checkCustomPlaceholders(content);
const config = getConfig();
const newContent = replacePlaceholders(content, config.placeholders);

if (action === 'replace') {
el.innerHTML = newContent;
Expand Down Expand Up @@ -946,16 +958,7 @@ export function handleFragmentCommand(command, a) {
return false;
}

function replacePlaceholders(value, placeholders) {
let val = value;
const matches = val.match(/{{(.*?)}}/g);
if (!matches) return val;
matches.forEach((match) => {
const key = match.replace(/{{|}}/g, '').trim();
if (placeholders[key]) val = val.replace(match, placeholders[key]);
});
return val;
}


export function parseNestedPlaceholders({ placeholders }) {
if (!placeholders) return;
Expand Down
36 changes: 0 additions & 36 deletions test/features/personalization/placeholderPerformance.test.js

This file was deleted.

0 comments on commit 3bd6955

Please sign in to comment.