Skip to content

Commit 38db2b2

Browse files
Merge pull request #74 from Exabyte-io/chore/SOF-6955
Chore/sof 6955
2 parents f93a0a1 + 3ce19dc commit 38db2b2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/utils/str.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export function convertArabicToRoman(num) {
9797
/**
9898
* Render name template based on config.
9999
* Use substitution map to replace a config value with a more readable variant.
100+
* NOTE: We currently iterate through the whole object. A more efficient albeit more verbose approach would be
101+
* to give the property path in the subsitution map, e.g., `{ "user.name": {"user001": "John Doe"} }`.
100102
* @param {string|undefined} template - Template for the name property
101103
* @param {Object} data - Entity config
102104
* @param {Object} substitutionMap - Maps object value to human-readable string
@@ -115,13 +117,19 @@ export function renderTextWithSubstitutes(template, data, substitutionMap = {})
115117

116118
// Helper function for recursive substitution
117119
function substituteNested(obj) {
118-
lodash.forIn(obj, (value, key) => {
120+
if (!lodash.isPlainObject(obj)) {
121+
return;
122+
}
123+
// eslint-disable-next-line no-restricted-syntax
124+
for (const [key, value] of Object.entries(obj)) {
119125
if (lodash.isPlainObject(value)) {
120126
substituteNested(value);
127+
} else if (Array.isArray(value)) {
128+
value.forEach((val) => substituteNested(val));
121129
} else if (substitutionMap[value]) {
122130
obj[key] = substitutionMap[value];
123131
}
124-
});
132+
}
125133
}
126134

127135
substituteNested(renderData);

tests/utils/str.tests.ts

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ describe("str: renderTextWithSubstitutes", () => {
6262

6363
expect(result).to.be.equal("Hello !");
6464
});
65+
66+
it("should be able to access nested elements in template notation", () => {
67+
const template = "Hello {{ user }} of {{ array[1]['planet'] }}!";
68+
const data = { user: "user001", array: [{ planet: "A" }, { planet: "B" }] };
69+
const substitutionMap = {
70+
A: "Earth",
71+
B: "Mars",
72+
};
73+
74+
const result = renderTextWithSubstitutes(template, data, substitutionMap);
75+
76+
expect(result).to.be.equal("Hello user001 of Mars!");
77+
});
6578
});
6679

6780
describe("str: findPreviousVersion", () => {

0 commit comments

Comments
 (0)