Skip to content

Commit 7adf418

Browse files
authored
feat: fixed tm to td conversion (#172) (#173)
* feat:fixed tm to td conversion Signed-off-by: Rohith <[email protected]> * feat:fixed tm to td conversion Signed-off-by: Rohith <[email protected]> * committing changes * modified according to pr_review * modified test * test: cover both partial and full deletion of @type array * test: verify @type filtering for both string and array inputs --------- Signed-off-by: Rohith <[email protected]>
1 parent d9e1c0b commit 7adf418

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/services/operations.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,22 +401,43 @@ describe("processConversionTMtoTD", () => {
401401
});
402402

403403
test("removes TM-specific fields", () => {
404-
const tmContent = `{
404+
const stringTypeContent = `{
405405
"@type": "tm:ThingModel",
406406
"tm:required": ["#properties/prop1"],
407407
"properties": { "prop1": {} }
408408
}`;
409409

410-
const result = processConversionTMtoTD(
411-
tmContent,
410+
const stringResult = processConversionTMtoTD(
411+
stringTypeContent,
412412
{},
413413
["prop1"],
414414
[],
415415
[],
416416
""
417417
);
418418

419-
expect(result).not.toHaveProperty("tm:required");
419+
expect(stringResult).not.toHaveProperty("tm:required");
420+
expect(stringResult).not.toHaveProperty("@type");
421+
422+
const arrayTypeContent = `{
423+
"@type": ["tm:ThingModel","example_key:example_val"],
424+
"tm:required": ["#properties/prop1"],
425+
"properties": { "prop1": {} }
426+
}`;
427+
428+
const arrayResult = processConversionTMtoTD(
429+
arrayTypeContent,
430+
{},
431+
["prop1"],
432+
[],
433+
[],
434+
""
435+
);
436+
437+
expect(Array.isArray(arrayResult["@type"])).toBe(true);
438+
expect(arrayResult["@type"]).toContain("example_key:example_val");
439+
expect(arrayResult["@type"]).not.toContain("tm:ThingModel");
440+
expect(arrayResult).not.toHaveProperty("tm:required");
420441
});
421442

422443
test("handles complex TM to TD conversion", () => {

src/services/operations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,19 @@ export function processConversionTMtoTD(
134134
};
135135
}
136136

137-
delete parsed["@type"];
137+
if (parsed["@type"]) {
138+
if (Array.isArray(parsed["@type"])) {
139+
parsed["@type"] = parsed["@type"].filter(
140+
(x: String) => x != "tm:ThingModel"
141+
);
142+
if (parsed["@type"].length == 0) {
143+
delete parsed["@type"];
144+
}
145+
} else {
146+
delete parsed["@type"];
147+
}
148+
}
138149
delete parsed["tm:required"];
139-
140150
return parsed;
141151
} catch (error) {
142152
console.error("Error processing TM:", error);

0 commit comments

Comments
 (0)