Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit db308c9

Browse files
hlomziknick-skriabinmakseq
authored
Fix parseValue for "title: $title" cases and missed data (#380)
* TDD: failing test for value parsing * [fix] bullet-proof parseValue: all vars; fallback * [fix] get strings and structures from parseValue Co-authored-by: Nick Skriabin <[email protected]> Co-authored-by: Max Tkachenko <[email protected]>
1 parent 303eca9 commit db308c9

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/utils/__tests__/data.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ describe("parseValue", () => {
137137
expect(parseValue("$num2str", data)).toEqual("123");
138138
});
139139

140+
test("Text with one variable", () => {
141+
expect(parseValue("URL: $url", data)).toEqual("URL: https://labelstud.io");
142+
});
143+
140144
test("Text with variables", () => {
141145
expect(parseValue("URL of $name is $url", data)).toEqual("URL of Label Studio is https://labelstud.io");
142146
});

src/utils/data.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import get from "lodash.get";
44
/**
55
* Simple way to retrieve linked data in `value` param from task
66
* Works only for prefixed values ($image); non-prefixed values left as is
7+
* It's possible to add some text which will be left untouched; that's useful for
8+
* visual Text tags to display some additional info ("Title: $title")
79
* @param {string} value param
810
* @param {object} task
911
*/
@@ -12,18 +14,12 @@ export const parseValue = (value, task) => {
1214

1315
if (!value) return "";
1416

15-
if (value.match(reVar)?.length === 1) {
16-
return get(task, value.substr(1));
17+
// value can refer to structures, not only texts, so just replace wouldn't be enough
18+
if (value.match(reVar)?.[0] === value) {
19+
return get(task, value.substr(1)) ?? "";
1720
}
1821

19-
// case for visual Text tags to display some additional info ("Title: $title")
20-
return value.replace(reVar, (v) => {
21-
if (v && v.startsWith('$')) {
22-
return get(task, v.substr(1));
23-
}
24-
25-
return v;
26-
});
22+
return value.replace(reVar, (v) => get(task, v.substr(1) ?? ""));
2723
};
2824

2925
/**

0 commit comments

Comments
 (0)