From dfaa7d9764215999406673ab59db9ceabc4c239e Mon Sep 17 00:00:00 2001 From: William Chargin Date: Wed, 6 Jun 2018 11:54:20 -0700 Subject: [PATCH] Pull `Direction` values into an enum (#353) Summary: This saves clients from having to pollute their global namespace with `IN` and `OUT` (and, soon, `ANY`). Calls now look like: ```js myNode.neighbors({direction: Direction.OUT}); ``` Callers who prefer to pollute their namespaces are of course still welcome to do so, at the cost of one `const {IN, OUT} = Direction`. Test Plan: New unit tests for frozenness included. wchargin-branch: direction-enum --- src/v3/core/graph.js | 12 ++++++++---- src/v3/core/graph.test.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/v3/core/graph.js b/src/v3/core/graph.js index 5a234cc..dad0dd9 100644 --- a/src/v3/core/graph.js +++ b/src/v3/core/graph.js @@ -14,11 +14,15 @@ export type Edge = {| |}; export type Neighbor = {|+node: NodeAddress, +edge: Edge|}; -export opaque type Direction = Symbol; -export const IN: Direction = Symbol("IN"); -export const OUT: Direction = Symbol("OUT"); + +export opaque type DirectionT = Symbol; +export const Direction: {|+IN: DirectionT, +OUT: DirectionT|} = Object.freeze({ + IN: Symbol("IN"), + OUT: Symbol("OUT"), +}); + export type NeighborsOptions = {| - +direction: ?Direction, + +direction: ?DirectionT, +nodePrefix: ?NodeAddress, +edgePrefix: ?EdgeAddress, |}; diff --git a/src/v3/core/graph.test.js b/src/v3/core/graph.test.js index f2a8c46..eabcf13 100644 --- a/src/v3/core/graph.test.js +++ b/src/v3/core/graph.test.js @@ -1,6 +1,6 @@ // @flow -import {Address, Graph, edgeToString} from "./graph"; +import {Address, Direction, Graph, edgeToString} from "./graph"; import type {NodeAddress, EdgeAddress} from "./graph"; describe("core/graph", () => { @@ -30,6 +30,15 @@ describe("core/graph", () => { }); }); + describe("Direction values", () => { + it("are read-only", () => { + expect(() => { + // $ExpectFlowError + Direction.IN = Direction.OUT; + }).toThrow(/read.only property/); + }); + }); + describe("Graph class", () => { it("can be constructed", () => { const x = new Graph();