diff --git a/src/core/graph.js b/src/core/graph.js index 1107272..c27a6aa 100644 --- a/src/core/graph.js +++ b/src/core/graph.js @@ -614,6 +614,25 @@ export function edgeToString(edge: Edge): string { return `{address: ${address}, src: ${src}, dst: ${dst}}`; } +/** + * Convert an edge to an object whose fields are human-readable strings. + * This is useful for storing edges in human-readable formats that + * should not include NUL characters, such as Jest snapshots. + */ +export function edgeToStrings( + edge: Edge +): {| + +address: string, + +src: string, + +dst: string, +|} { + return { + address: EdgeAddress.toString(edge.address), + src: NodeAddress.toString(edge.src), + dst: NodeAddress.toString(edge.dst), + }; +} + export function edgeToParts( edge: Edge ): {|+addressParts: string[], +srcParts: string[], +dstParts: string[]|} { diff --git a/src/core/graph.test.js b/src/core/graph.test.js index 56371dd..f55cc95 100644 --- a/src/core/graph.test.js +++ b/src/core/graph.test.js @@ -14,6 +14,7 @@ import { Graph, NodeAddress, edgeToString, + edgeToStrings, edgeToParts, } from "./graph"; import {advancedGraph} from "./graphTestUtil"; @@ -1391,6 +1392,22 @@ describe("core/graph", () => { }); }); + describe("edgeToStrings", () => { + it("works", () => { + const edge = { + address: EdgeAddress.fromParts(["one", "two"]), + dst: NodeAddress.fromParts(["five", "six"]), + src: NodeAddress.fromParts(["three", "four"]), + }; + const expected = { + address: 'EdgeAddress["one","two"]', + src: 'NodeAddress["three","four"]', + dst: 'NodeAddress["five","six"]', + }; + expect(edgeToStrings(edge)).toEqual(expected); + }); + }); + describe("edgeToParts", () => { it("works", () => { const edge = {