Skip to content

Commit

Permalink
Return multiple format output as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Sep 24, 2024
1 parent 33d039a commit de27efd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
10 changes: 5 additions & 5 deletions packages/viz/src/viz.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class Viz {
}

render(input, options = {}) {
let formats;
let format;

if (options.format === void 0) {
formats = ["dot"];
format = "dot";
} else {
formats = [options.format];
format = options.format;
}

let result = this.wrapper.renderInput(input, formats, { engine: "dot", ...options });
let result = this.wrapper.renderInput(input, [format], { engine: "dot", ...options });

if (result.status === "success") {
result.output = result.output[0];
result.output = result.output[format];
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions packages/viz/src/wrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Wrapper {
};
}

let output = [];
let output = {};

for (let format of formats) {
resultPointer = this.module.ccall("viz_render", "number", ["number", "number", "string"], [contextPointer, graphPointer, format]);
Expand All @@ -88,7 +88,7 @@ class Wrapper {
errors: this.#parseErrorMessages()
};
} else {
output.push(this.module.UTF8ToString(resultPointer));
output[format] = this.module.UTF8ToString(resultPointer);

this.module.ccall("free", "number", ["number"], [resultPointer]);
resultPointer = 0;
Expand Down
25 changes: 12 additions & 13 deletions packages/viz/test/render-formats.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe("Viz", function() {

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"<map id=\"a\" name=\"a\">\n</map>\n"
],
output: {
dot: "graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
cmapx: "<map id=\"a\" name=\"a\">\n</map>\n"
},
errors: []
});
});
Expand All @@ -27,10 +27,9 @@ describe("Viz", function() {

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n"
],
output: {
dot: "graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n"
},
errors: []
});
});
Expand All @@ -40,7 +39,7 @@ describe("Viz", function() {

assert.deepStrictEqual(result, {
status: "success",
output: [],
output: {},
errors: []
});
});
Expand All @@ -50,10 +49,10 @@ describe("Viz", function() {

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"<map id=\"a\" name=\"a\">\n</map>\n"
],
output: {
dot: "graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
cmapx: "<map id=\"a\" name=\"a\">\n</map>\n"
},
errors: []
});
});
Expand Down

0 comments on commit de27efd

Please sign in to comment.