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
This commit is contained in:
William Chargin 2018-06-06 11:54:20 -07:00 committed by GitHub
parent 4a06485a99
commit dfaa7d9764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -14,11 +14,15 @@ export type Edge = {|
|}; |};
export type Neighbor = {|+node: NodeAddress, +edge: Edge|}; export type Neighbor = {|+node: NodeAddress, +edge: Edge|};
export opaque type Direction = Symbol;
export const IN: Direction = Symbol("IN"); export opaque type DirectionT = Symbol;
export const OUT: Direction = Symbol("OUT"); export const Direction: {|+IN: DirectionT, +OUT: DirectionT|} = Object.freeze({
IN: Symbol("IN"),
OUT: Symbol("OUT"),
});
export type NeighborsOptions = {| export type NeighborsOptions = {|
+direction: ?Direction, +direction: ?DirectionT,
+nodePrefix: ?NodeAddress, +nodePrefix: ?NodeAddress,
+edgePrefix: ?EdgeAddress, +edgePrefix: ?EdgeAddress,
|}; |};

View File

@ -1,6 +1,6 @@
// @flow // @flow
import {Address, Graph, edgeToString} from "./graph"; import {Address, Direction, Graph, edgeToString} from "./graph";
import type {NodeAddress, EdgeAddress} from "./graph"; import type {NodeAddress, EdgeAddress} from "./graph";
describe("core/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", () => { describe("Graph class", () => {
it("can be constructed", () => { it("can be constructed", () => {
const x = new Graph(); const x = new Graph();