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
This commit is contained in:
William Chargin 2018-06-19 15:48:04 -07:00 committed by GitHub
parent ea74955a66
commit aac2fc6792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -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};
}

View File

@ -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);
});
});
});