From aac2fc6792f4530929e58f812a1d262b85f6f7d9 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Tue, 19 Jun 2018 15:48:04 -0700 Subject: [PATCH] Add `edgeToParts` convenience export from `Graph` (#398) Summary: We have `edgeToString`, which formats edges as nicely human-readable strings. However, these strings have some quotes in them, and so when they are themselves stringified (e.g., as part of a Jest snapshot), they become much harder to read. We thus introduce `edgeToParts` to make our snapshots more readable. Test Plan: Unit tests added; run `yarn travis`. wchargin-branch: add-edgeToParts --- src/v3/core/graph.js | 9 +++++++++ src/v3/core/graph.test.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/v3/core/graph.js b/src/v3/core/graph.js index f38c344..1735ff2 100644 --- a/src/v3/core/graph.js +++ b/src/v3/core/graph.js @@ -588,3 +588,12 @@ export function edgeToString(edge: Edge): string { const dst = NodeAddress.toString(edge.dst); return `{address: ${address}, src: ${src}, dst: ${dst}}`; } + +export function edgeToParts( + edge: Edge +): {|+addressParts: string[], +srcParts: string[], +dstParts: string[]|} { + const addressParts = EdgeAddress.toParts(edge.address); + const srcParts = NodeAddress.toParts(edge.src); + const dstParts = NodeAddress.toParts(edge.dst); + return {addressParts, srcParts, dstParts}; +} diff --git a/src/v3/core/graph.test.js b/src/v3/core/graph.test.js index 15fc997..886f627 100644 --- a/src/v3/core/graph.test.js +++ b/src/v3/core/graph.test.js @@ -14,6 +14,7 @@ import { Graph, NodeAddress, edgeToString, + edgeToParts, } from "./graph"; describe("core/graph", () => { @@ -1467,4 +1468,20 @@ describe("core/graph", () => { expect(edgeToString(edge)).toEqual(expected); }); }); + + describe("edgeToParts", () => { + it("works", () => { + const edge = { + address: EdgeAddress.fromParts(["one", "two"]), + dst: NodeAddress.fromParts(["five", "six"]), + src: NodeAddress.fromParts(["three", "four"]), + }; + const expected = { + addressParts: ["one", "two"], + srcParts: ["three", "four"], + dstParts: ["five", "six"], + }; + expect(edgeToParts(edge)).toEqual(expected); + }); + }); });