Finishing getStringWithCustomValues method

This commit is contained in:
Michaela Brydon 2024-01-03 19:09:19 -05:00
parent 0de9309cc2
commit 3920969c48
2 changed files with 16 additions and 12 deletions

View file

@ -9,10 +9,12 @@ import { useMainStore } from '@/store';
* @returns {string}
*/
export function getStringWithCustomValues(str, customValueMap) {
let newString = str;
Object.keys(customValueMap).forEach((key) => {
newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]);
});
let newString = str ?? '';
if (customValueMap != null) {
Object.keys(customValueMap).forEach((key) => {
newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]);
});
}
return newString;
}

View file

@ -2,14 +2,16 @@ import { getStringWithCustomValues } from '@/helpers/cms-content-helper.js';
describe('getStringWithCustomValues', () => {
test.each([
['Hello, {custom:name}!', { name: 'John' }, 'Hello, John!']// ,
// ['{custom:greeting}, {custom:name}!', { greeting: 'Hi', name: 'John' }, 'Hi, John!'],
// ['{custom:greeting}, {custom:name}!', { greeting: 'Hi' }, 'Hi, {custom:name}!'],
// ['Hello, {custom:name}!', {}, 'Hello, {custom:name}!'],
// ['Hello, world!', { name: 'John' }, 'Hello, world!'],
// ['', { name: 'John' }, ''],
// ['Hello, {custom:name}!', null, 'Hello, {custom:name}!'],
// [null, { name: 'John' }, null]
['Hello, {custom:name}!', { name: 'John' }, 'Hello, John!'],
['{custom:greeting}, {custom:name}!', { greeting: 'Hi', name: 'John' }, 'Hi, John!'],
['{custom:greeting}, {custom:name}!', { greeting: 'Hi' }, 'Hi, {custom:name}!'],
['Hello, {custom:name}!', {}, 'Hello, {custom:name}!'],
['Hello, world!', { name: 'John' }, 'Hello, world!'],
['', { name: 'John' }, ''],
['Hello, {custom:name}!', null, 'Hello, {custom:name}!'],
['Hello, {custom:name}!', undefined, 'Hello, {custom:name}!'],
[null, { name: 'John' }, ''],
[undefined, { name: 'John' }, '']
])('getStringWithCustomValues(%s, %o) should return %s', (str, customValueMap, expected) => {
expect(getStringWithCustomValues(str, customValueMap)).toBe(expected);
});