Skip to content

Commit

Permalink
nested properties vor viewurl replacement (SAP#2694)
Browse files Browse the repository at this point in the history
  • Loading branch information
hardl authored May 5, 2022
1 parent e0e7918 commit 5ac37a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
23 changes: 17 additions & 6 deletions core/src/utilities/helpers/generic-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Standalone or partly-standalone methods that are used widely through the whole app and are synchronous.
import { LuigiElements, LuigiConfig } from '../../core-api';
import { replace, get } from 'lodash';

class GenericHelpersClass {
/**
Expand Down Expand Up @@ -195,12 +196,22 @@ class GenericHelpersClass {
replaceVars(inputString, params, prefix, parenthesis = true) {
let processedString = inputString;
if (params) {
Object.entries(params).forEach(entry => {
processedString = processedString.replace(
new RegExp(this.escapeRegExp((parenthesis ? '{' : '') + prefix + entry[0] + (parenthesis ? '}' : '')), 'g'),
encodeURIComponent(entry[1])
);
});
if (parenthesis) {
processedString = replace(processedString, /{([\s\S]+?)}/g, val => {
let repl = val.slice(1, -1).trim();
if (repl.indexOf(prefix) === 0) {
repl = repl.substring(prefix.length);
}
return get(params, repl, val);
});
} else {
Object.entries(params).forEach(entry => {
processedString = processedString.replace(
new RegExp(this.escapeRegExp(prefix + entry[0]), 'g'),
encodeURIComponent(entry[1])
);
});
}
}
if (parenthesis) {
processedString = processedString.replace(new RegExp('\\{' + this.escapeRegExp(prefix) + '[^\\}]+\\}', 'g'), '');
Expand Down
24 changes: 24 additions & 0 deletions core/test/utilities/helpers/generic-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ describe('Generic-helpers', () => {
assert.deepEqual(GenericHelpers.removeProperties(input, keys), expected);
});

it('replaceVars', () => {
const context = {
a: 'a_val',
b: {
c: 'c_val'
}
};
assert.equal(
GenericHelpers.replaceVars('index.html#/{context.a}', context, 'context.'),
'index.html#/a_val',
'first level context vars being interpolated'
);
assert.equal(
GenericHelpers.replaceVars('index.html#/{context.b.c}', context, 'context.'),
'index.html#/c_val',
'nested context vars being interpolated'
);
assert.equal(
GenericHelpers.replaceVars('index.html#/:a/{context.b.c}', context, ':', false),
'index.html#/a_val/{context.b.c}',
'colon prefix, no paranthesis'
);
});

describe('semverCompare', () => {
it('standard versions', () => {
const input = ['1.1.1', '0.6.4', '0.7.7', '0.7.1', '1.0.0'];
Expand Down
13 changes: 13 additions & 0 deletions core/test/utilities/helpers/routing-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ describe('Routing-helpers', () => {

expect(RoutingHelpers.substituteViewUrl(viewUrl, {})).to.equal(expected);
});

it('substituteViewUrl - substitutes nested context variable', () => {
const viewUrl = '/{i18n.currentLocale}/microfrontend.html#/:var1/{context.nested.value}/{nodeParams.param1}';
const expected = '/en/microfrontend.html#/var1_value/context_nested_value/nodeparam_value';

expect(
RoutingHelpers.substituteViewUrl(viewUrl, {
pathParams: { var1: 'var1_value' },
context: { nested: { value: 'context_nested_value' } },
nodeParams: { param1: 'nodeparam_value' }
})
).to.equal(expected);
});
});
describe('substitute search query params', () => {
afterEach(() => {
Expand Down

0 comments on commit 5ac37a6

Please sign in to comment.