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} * @returns {string}
*/ */
export function getStringWithCustomValues(str, customValueMap) { export function getStringWithCustomValues(str, customValueMap) {
let newString = str; let newString = str ?? '';
Object.keys(customValueMap).forEach((key) => { if (customValueMap != null) {
newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]); Object.keys(customValueMap).forEach((key) => {
}); newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]);
});
}
return newString; return newString;
} }

View file

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