@atlaspack/graph 3.2.1-dev.3567 → 3.3.1-canary.3630

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # @atlaspack/graph
2
+
3
+ ## 3.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#335](https://github.com/atlassian-labs/atlaspack/pull/335) [`b4dbd4d`](https://github.com/atlassian-labs/atlaspack/commit/b4dbd4d5b23d1b7aa3fcdf59cc7bc8bedd3a59cf) Thanks [@yamadapc](https://github.com/yamadapc)! - Initial changeset release
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`b4dbd4d`](https://github.com/atlassian-labs/atlaspack/commit/b4dbd4d5b23d1b7aa3fcdf59cc7bc8bedd3a59cf)]:
12
+ - @atlaspack/feature-flags@2.13.0
@@ -268,7 +268,7 @@ class AdjacencyList {
268
268
  * Returns `true` if the edge was added,
269
269
  * or `false` if the edge already exists.
270
270
  */
271
- addEdge(from, to, type = 1) {
271
+ addEdge(from, to, type = _Graph.NULL_EDGE_TYPE) {
272
272
  (0, _assert().default)(from < this.#nodes.nextId, `Node ${from} does not exist.`);
273
273
  (0, _assert().default)(to < this.#nodes.nextId, `Node ${to} does not exist.`);
274
274
  (0, _assert().default)(type > 0, `Unsupported edge type ${type}`);
@@ -316,7 +316,7 @@ class AdjacencyList {
316
316
  /**
317
317
  * Check if the graph has an edge connecting the `from` and `to` nodes.
318
318
  */
319
- hasEdge(from, to, type = 1) {
319
+ hasEdge(from, to, type = _Graph.NULL_EDGE_TYPE) {
320
320
  let hasEdge = type => {
321
321
  let hash = this.#edges.hash(from, to, type);
322
322
  return this.#edges.addressOf(hash, from, to, type) !== null;
@@ -335,7 +335,7 @@ class AdjacencyList {
335
335
  *
336
336
  * This method will increment the edge delete count.
337
337
  */
338
- removeEdge(from, to, type = 1) {
338
+ removeEdge(from, to, type = _Graph.NULL_EDGE_TYPE) {
339
339
  let hash = this.#edges.hash(from, to, type);
340
340
  let edge = this.#edges.addressOf(hash, from, to, type);
341
341
 
@@ -429,7 +429,7 @@ class AdjacencyList {
429
429
  * If `type` is an array, return nodes connected by edges of any of those types.
430
430
  * If `type` is `AllEdgeTypes` (`-1`), return nodes connected by edges of any type.
431
431
  */
432
- getNodeIdsConnectedFrom(from, type = 1) {
432
+ getNodeIdsConnectedFrom(from, type = _Graph.NULL_EDGE_TYPE) {
433
433
  let matches = node => type === _Graph.ALL_EDGE_TYPES || (Array.isArray(type) ? type.includes(this.#nodes.typeOf(node)) : type === this.#nodes.typeOf(node));
434
434
  let nodes = [];
435
435
  let seen = new Set();
@@ -467,7 +467,7 @@ class AdjacencyList {
467
467
  node = this.#nodes.next(node);
468
468
  }
469
469
  }
470
- forEachNodeIdConnectedTo(to, fn, type = 1) {
470
+ forEachNodeIdConnectedTo(to, fn, type = _Graph.NULL_EDGE_TYPE) {
471
471
  const matches = node => type === _Graph.ALL_EDGE_TYPES || type === this.#nodes.typeOf(node);
472
472
  let node = this.#nodes.head(to);
473
473
  while (node !== null) {
@@ -492,7 +492,7 @@ class AdjacencyList {
492
492
  * If `type` is an array, return nodes connected by edges of any of those types.
493
493
  * If `type` is `AllEdgeTypes` (`-1`), return nodes connected by edges of any type.
494
494
  */
495
- getNodeIdsConnectedTo(to, type = 1) {
495
+ getNodeIdsConnectedTo(to, type = _Graph.NULL_EDGE_TYPE) {
496
496
  let matches = node => type === _Graph.ALL_EDGE_TYPES || (Array.isArray(type) ? type.includes(this.#nodes.typeOf(node)) : type === this.#nodes.typeOf(node));
497
497
  let nodes = [];
498
498
  let seen = new Set();
@@ -63,6 +63,9 @@ class ContentGraph extends _Graph.default {
63
63
  return super.getNode(nodeId);
64
64
  }
65
65
  }
66
+ getContentKeyByNodeId(nodeId) {
67
+ return (0, _nullthrows().default)(this._nodeIdToContentKey.get(nodeId), `Expected node id ${nodeId} to exist`);
68
+ }
66
69
  getNodeIdByContentKey(contentKey) {
67
70
  return (0, _nullthrows().default)(this._contentKeyToNodeId.get(contentKey), `Expected content key ${contentKey} to exist`);
68
71
  }
package/lib/Graph.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports.ALL_EDGE_TYPES = void 0;
6
+ exports.default = exports.NULL_EDGE_TYPE = exports.ALL_EDGE_TYPES = void 0;
7
7
  exports.mapVisitor = mapVisitor;
8
8
  var _types = require("./types");
9
9
  var _AdjacencyList = _interopRequireDefault(require("./AdjacencyList"));
@@ -16,6 +16,7 @@ function _nullthrows() {
16
16
  return data;
17
17
  }
18
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+ const NULL_EDGE_TYPE = exports.NULL_EDGE_TYPE = 1;
19
20
  const ALL_EDGE_TYPES = exports.ALL_EDGE_TYPES = -1;
20
21
 
21
22
  /**
@@ -70,7 +71,7 @@ class Graph {
70
71
  getNode(id) {
71
72
  return this.nodes[id];
72
73
  }
73
- addEdge(from, to, type = 1) {
74
+ addEdge(from, to, type = NULL_EDGE_TYPE) {
74
75
  if (Number(type) === 0) {
75
76
  throw new Error(`Edge type "${type}" not allowed`);
76
77
  }
@@ -82,25 +83,25 @@ class Graph {
82
83
  }
83
84
  return this.adjacencyList.addEdge(from, to, type);
84
85
  }
85
- hasEdge(from, to, type = 1) {
86
+ hasEdge(from, to, type = NULL_EDGE_TYPE) {
86
87
  return this.adjacencyList.hasEdge(from, to, type);
87
88
  }
88
- forEachNodeIdConnectedTo(to, fn, type = 1) {
89
+ forEachNodeIdConnectedTo(to, fn, type = NULL_EDGE_TYPE) {
89
90
  this._assertHasNodeId(to);
90
91
  this.adjacencyList.forEachNodeIdConnectedTo(to, fn, type);
91
92
  }
92
- forEachNodeIdConnectedFrom(from, fn, type = 1) {
93
+ forEachNodeIdConnectedFrom(from, fn, type = NULL_EDGE_TYPE) {
93
94
  this._assertHasNodeId(from);
94
95
  this.adjacencyList.forEachNodeIdConnectedFromReverse(from, id => {
95
96
  fn(id);
96
97
  return false;
97
98
  }, type);
98
99
  }
99
- getNodeIdsConnectedTo(nodeId, type = 1) {
100
+ getNodeIdsConnectedTo(nodeId, type = NULL_EDGE_TYPE) {
100
101
  this._assertHasNodeId(nodeId);
101
102
  return this.adjacencyList.getNodeIdsConnectedTo(nodeId, type);
102
103
  }
103
- getNodeIdsConnectedFrom(nodeId, type = 1) {
104
+ getNodeIdsConnectedFrom(nodeId, type = NULL_EDGE_TYPE) {
104
105
  this._assertHasNodeId(nodeId);
105
106
  return this.adjacencyList.getNodeIdsConnectedFrom(nodeId, type);
106
107
  }
@@ -127,7 +128,7 @@ class Graph {
127
128
  }
128
129
  this.nodes[nodeId] = null;
129
130
  }
130
- removeEdges(nodeId, type = 1) {
131
+ removeEdges(nodeId, type = NULL_EDGE_TYPE) {
131
132
  if (!this.hasNode(nodeId)) {
132
133
  return;
133
134
  }
@@ -135,7 +136,7 @@ class Graph {
135
136
  this._removeEdge(nodeId, to, type);
136
137
  }
137
138
  }
138
- removeEdge(from, to, type = 1, removeOrphans = true) {
139
+ removeEdge(from, to, type = NULL_EDGE_TYPE, removeOrphans = true) {
139
140
  if (!this.adjacencyList.hasEdge(from, to, type)) {
140
141
  throw new Error(`Edge from ${(0, _types.fromNodeId)(from)} to ${(0, _types.fromNodeId)(to)} not found!`);
141
142
  }
@@ -143,7 +144,7 @@ class Graph {
143
144
  }
144
145
 
145
146
  // Removes edge and node the edge is to if the node is orphaned
146
- _removeEdge(from, to, type = 1, removeOrphans = true) {
147
+ _removeEdge(from, to, type = NULL_EDGE_TYPE, removeOrphans = true) {
147
148
  if (!this.adjacencyList.hasEdge(from, to, type)) {
148
149
  return;
149
150
  }
@@ -183,7 +184,7 @@ class Graph {
183
184
  }
184
185
 
185
186
  // Update a node's downstream nodes making sure to prune any orphaned branches
186
- replaceNodeIdsConnectedTo(fromNodeId, toNodeIds, replaceFilter, type = 1) {
187
+ replaceNodeIdsConnectedTo(fromNodeId, toNodeIds, replaceFilter, type = NULL_EDGE_TYPE, removeOrphans = true) {
187
188
  this._assertHasNodeId(fromNodeId);
188
189
  let outboundEdges = this.getNodeIdsConnectedFrom(fromNodeId, type);
189
190
  let childrenToRemove = new Set(replaceFilter ? outboundEdges.filter(toNodeId => replaceFilter(toNodeId)) : outboundEdges);
@@ -194,10 +195,10 @@ class Graph {
194
195
  }
195
196
  }
196
197
  for (let child of childrenToRemove) {
197
- this._removeEdge(fromNodeId, child, type);
198
+ this._removeEdge(fromNodeId, child, type, removeOrphans);
198
199
  }
199
200
  }
200
- traverse(visit, startNodeId, type = 1) {
201
+ traverse(visit, startNodeId, type = NULL_EDGE_TYPE) {
201
202
  let enter = typeof visit === 'function' ? visit : visit.enter;
202
203
  if (type === ALL_EDGE_TYPES && enter && (typeof visit === 'function' || !visit.exit)) {
203
204
  return this.dfsFast(enter, startNodeId);
@@ -212,7 +213,7 @@ class Graph {
212
213
  filteredTraverse(filter, visit, startNodeId, type) {
213
214
  return this.traverse(mapVisitor(filter, visit), startNodeId, type);
214
215
  }
215
- traverseAncestors(startNodeId, visit, type = 1) {
216
+ traverseAncestors(startNodeId, visit, type = NULL_EDGE_TYPE) {
216
217
  return this.dfs({
217
218
  visit,
218
219
  startNodeId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/graph",
3
- "version": "3.2.1-dev.3567+c06f4487e",
3
+ "version": "3.3.1-canary.3630+10a6f37ef",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "publishConfig": {
@@ -16,8 +16,8 @@
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/feature-flags": "2.12.1-dev.3567+c06f4487e",
19
+ "@atlaspack/feature-flags": "2.13.1-canary.3630+10a6f37ef",
20
20
  "nullthrows": "^1.1.1"
21
21
  },
22
- "gitHead": "c06f4487ebddc632957147c8b585a97e149062a1"
22
+ "gitHead": "10a6f37ef063d0227ebb26310383e899dbd9b1e6"
23
23
  }
@@ -3,7 +3,12 @@ import assert from 'assert';
3
3
  import nullthrows from 'nullthrows';
4
4
  import {SharedBuffer} from './shared-buffer';
5
5
  import {fromNodeId, toNodeId} from './types';
6
- import {ALL_EDGE_TYPES, type NullEdgeType, type AllEdgeTypes} from './Graph';
6
+ import {
7
+ ALL_EDGE_TYPES,
8
+ NULL_EDGE_TYPE,
9
+ type NullEdgeType,
10
+ type AllEdgeTypes,
11
+ } from './Graph';
7
12
  import type {NodeId} from './types';
8
13
 
9
14
  /** The address of the node in the nodes map. */
@@ -116,7 +121,7 @@ const MAX_LINK_TRIES: 3 = 3;
116
121
  * `getInboundEdgesByType` methods.
117
122
  *
118
123
  */
119
- export default class AdjacencyList<TEdgeType: number = 1> {
124
+ export default class AdjacencyList<TEdgeType: number = NullEdgeType> {
120
125
  #nodes: NodeTypeMap<TEdgeType | NullEdgeType>;
121
126
  #edges: EdgeTypeMap<TEdgeType | NullEdgeType>;
122
127
 
@@ -363,7 +368,7 @@ export default class AdjacencyList<TEdgeType: number = 1> {
363
368
  addEdge(
364
369
  from: NodeId,
365
370
  to: NodeId,
366
- type: TEdgeType | NullEdgeType = 1,
371
+ type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
367
372
  ): boolean {
368
373
  assert(from < this.#nodes.nextId, `Node ${from} does not exist.`);
369
374
  assert(to < this.#nodes.nextId, `Node ${to} does not exist.`);
@@ -433,7 +438,10 @@ export default class AdjacencyList<TEdgeType: number = 1> {
433
438
  hasEdge(
434
439
  from: NodeId,
435
440
  to: NodeId,
436
- type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
441
+ type:
442
+ | TEdgeType
443
+ | NullEdgeType
444
+ | Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
437
445
  ): boolean {
438
446
  let hasEdge = (type: TEdgeType | NullEdgeType) => {
439
447
  let hash = this.#edges.hash(from, to, type);
@@ -458,7 +466,7 @@ export default class AdjacencyList<TEdgeType: number = 1> {
458
466
  removeEdge(
459
467
  from: NodeId,
460
468
  to: NodeId,
461
- type: TEdgeType | NullEdgeType = 1,
469
+ type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
462
470
  ): void {
463
471
  let hash = this.#edges.hash(from, to, type);
464
472
  let edge = this.#edges.addressOf(hash, from, to, type);
@@ -568,7 +576,7 @@ export default class AdjacencyList<TEdgeType: number = 1> {
568
576
  | AllEdgeTypes
569
577
  | TEdgeType
570
578
  | NullEdgeType
571
- | Array<TEdgeType | NullEdgeType> = 1,
579
+ | Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
572
580
  ): NodeId[] {
573
581
  let matches = (node) =>
574
582
  type === ALL_EDGE_TYPES ||
@@ -622,7 +630,7 @@ export default class AdjacencyList<TEdgeType: number = 1> {
622
630
  forEachNodeIdConnectedTo(
623
631
  to: NodeId,
624
632
  fn: (nodeId: NodeId) => boolean | void,
625
- type: AllEdgeTypes | TEdgeType | NullEdgeType = 1,
633
+ type: AllEdgeTypes | TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
626
634
  ) {
627
635
  const matches = (node) =>
628
636
  type === ALL_EDGE_TYPES || type === this.#nodes.typeOf(node);
@@ -656,7 +664,7 @@ export default class AdjacencyList<TEdgeType: number = 1> {
656
664
  | AllEdgeTypes
657
665
  | TEdgeType
658
666
  | NullEdgeType
659
- | Array<TEdgeType | NullEdgeType> = 1,
667
+ | Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
660
668
  ): NodeId[] {
661
669
  let matches = (node) =>
662
670
  type === ALL_EDGE_TYPES ||
@@ -75,6 +75,13 @@ export default class ContentGraph<TNode, TEdgeType: number = 1> extends Graph<
75
75
  }
76
76
  }
77
77
 
78
+ getContentKeyByNodeId(nodeId: NodeId): ContentKey {
79
+ return nullthrows(
80
+ this._nodeIdToContentKey.get(nodeId),
81
+ `Expected node id ${nodeId} to exist`,
82
+ );
83
+ }
84
+
78
85
  getNodeIdByContentKey(contentKey: ContentKey): NodeId {
79
86
  return nullthrows(
80
87
  this._contentKeyToNodeId.get(contentKey),
package/src/Graph.js CHANGED
@@ -13,14 +13,15 @@ import {BitSet} from './BitSet';
13
13
  import nullthrows from 'nullthrows';
14
14
 
15
15
  export type NullEdgeType = 1;
16
- export type GraphOpts<TNode, TEdgeType: number = 1> = {|
16
+ export const NULL_EDGE_TYPE: NullEdgeType = 1;
17
+ export type GraphOpts<TNode, TEdgeType: number = NullEdgeType> = {|
17
18
  nodes?: Array<TNode | null>,
18
19
  adjacencyList?: SerializedAdjacencyList<TEdgeType>,
19
20
  rootNodeId?: ?NodeId,
20
21
  initialCapacity?: number,
21
22
  |};
22
23
 
23
- export type SerializedGraph<TNode, TEdgeType: number = 1> = {|
24
+ export type SerializedGraph<TNode, TEdgeType: number = NullEdgeType> = {|
24
25
  nodes: Array<TNode | null>,
25
26
  adjacencyList: SerializedAdjacencyList<TEdgeType>,
26
27
  rootNodeId: ?NodeId,
@@ -74,7 +75,7 @@ export type DFSParams<TContext> = {|
74
75
  startNodeId?: ?NodeId,
75
76
  |};
76
77
 
77
- export default class Graph<TNode, TEdgeType: number = 1> {
78
+ export default class Graph<TNode, TEdgeType: number = NullEdgeType> {
78
79
  nodes: Array<TNode | null>;
79
80
  adjacencyList: AdjacencyList<TEdgeType>;
80
81
  rootNodeId: ?NodeId;
@@ -138,7 +139,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
138
139
  addEdge(
139
140
  from: NodeId,
140
141
  to: NodeId,
141
- type: TEdgeType | NullEdgeType = 1,
142
+ type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
142
143
  ): boolean {
143
144
  if (Number(type) === 0) {
144
145
  throw new Error(`Edge type "${type}" not allowed`);
@@ -158,7 +159,10 @@ export default class Graph<TNode, TEdgeType: number = 1> {
158
159
  hasEdge(
159
160
  from: NodeId,
160
161
  to: NodeId,
161
- type?: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
162
+ type?:
163
+ | TEdgeType
164
+ | NullEdgeType
165
+ | Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
162
166
  ): boolean {
163
167
  return this.adjacencyList.hasEdge(from, to, type);
164
168
  }
@@ -166,7 +170,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
166
170
  forEachNodeIdConnectedTo(
167
171
  to: NodeId,
168
172
  fn: (nodeId: NodeId) => boolean | void,
169
- type: AllEdgeTypes | TEdgeType | NullEdgeType = 1,
173
+ type: AllEdgeTypes | TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
170
174
  ) {
171
175
  this._assertHasNodeId(to);
172
176
 
@@ -176,7 +180,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
176
180
  forEachNodeIdConnectedFrom(
177
181
  from: NodeId,
178
182
  fn: (nodeId: NodeId) => void,
179
- type: AllEdgeTypes | TEdgeType | NullEdgeType = 1,
183
+ type: AllEdgeTypes | TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
180
184
  ) {
181
185
  this._assertHasNodeId(from);
182
186
 
@@ -196,7 +200,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
196
200
  | TEdgeType
197
201
  | NullEdgeType
198
202
  | Array<TEdgeType | NullEdgeType>
199
- | AllEdgeTypes = 1,
203
+ | AllEdgeTypes = NULL_EDGE_TYPE,
200
204
  ): Array<NodeId> {
201
205
  this._assertHasNodeId(nodeId);
202
206
 
@@ -209,7 +213,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
209
213
  | TEdgeType
210
214
  | NullEdgeType
211
215
  | Array<TEdgeType | NullEdgeType>
212
- | AllEdgeTypes = 1,
216
+ | AllEdgeTypes = NULL_EDGE_TYPE,
213
217
  ): Array<NodeId> {
214
218
  this._assertHasNodeId(nodeId);
215
219
 
@@ -240,7 +244,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
240
244
  this.nodes[nodeId] = null;
241
245
  }
242
246
 
243
- removeEdges(nodeId: NodeId, type: TEdgeType | NullEdgeType = 1) {
247
+ removeEdges(nodeId: NodeId, type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE) {
244
248
  if (!this.hasNode(nodeId)) {
245
249
  return;
246
250
  }
@@ -253,7 +257,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
253
257
  removeEdge(
254
258
  from: NodeId,
255
259
  to: NodeId,
256
- type: TEdgeType | NullEdgeType = 1,
260
+ type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
257
261
  removeOrphans: boolean = true,
258
262
  ) {
259
263
  if (!this.adjacencyList.hasEdge(from, to, type)) {
@@ -269,7 +273,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
269
273
  _removeEdge(
270
274
  from: NodeId,
271
275
  to: NodeId,
272
- type: TEdgeType | NullEdgeType = 1,
276
+ type: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
273
277
  removeOrphans: boolean = true,
274
278
  ) {
275
279
  if (!this.adjacencyList.hasEdge(from, to, type)) {
@@ -325,7 +329,8 @@ export default class Graph<TNode, TEdgeType: number = 1> {
325
329
  fromNodeId: NodeId,
326
330
  toNodeIds: $ReadOnlyArray<NodeId>,
327
331
  replaceFilter?: null | ((NodeId) => boolean),
328
- type?: TEdgeType | NullEdgeType = 1,
332
+ type?: TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
333
+ removeOrphans: boolean = true,
329
334
  ): void {
330
335
  this._assertHasNodeId(fromNodeId);
331
336
 
@@ -344,7 +349,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
344
349
  }
345
350
 
346
351
  for (let child of childrenToRemove) {
347
- this._removeEdge(fromNodeId, child, type);
352
+ this._removeEdge(fromNodeId, child, type, removeOrphans);
348
353
  }
349
354
  }
350
355
 
@@ -355,7 +360,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
355
360
  | TEdgeType
356
361
  | NullEdgeType
357
362
  | Array<TEdgeType | NullEdgeType>
358
- | AllEdgeTypes = 1,
363
+ | AllEdgeTypes = NULL_EDGE_TYPE,
359
364
  ): ?TContext {
360
365
  let enter = typeof visit === 'function' ? visit : visit.enter;
361
366
  if (
@@ -389,7 +394,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
389
394
  | TEdgeType
390
395
  | NullEdgeType
391
396
  | Array<TEdgeType | NullEdgeType>
392
- | AllEdgeTypes = 1,
397
+ | AllEdgeTypes = NULL_EDGE_TYPE,
393
398
  ): ?TContext {
394
399
  return this.dfs({
395
400
  visit,
@@ -39,4 +39,22 @@ describe('ContentGraph', () => {
39
39
 
40
40
  assert(!graph.hasContentKey('contentKey'));
41
41
  });
42
+
43
+ describe('getContentKeyByNodeId', () => {
44
+ it('returns the content key for a node', () => {
45
+ const graph = new ContentGraph();
46
+
47
+ const node1 = graph.addNodeByContentKey('node1', {});
48
+ assert.equal(graph.getContentKeyByNodeId(node1), 'node1');
49
+ const node2 = graph.addNodeByContentKey('node2', {});
50
+ assert.equal(graph.getContentKeyByNodeId(node2), 'node2');
51
+ });
52
+
53
+ it('throws if the node does not have a content key', () => {
54
+ const graph = new ContentGraph();
55
+
56
+ const node1 = graph.addNode({});
57
+ assert.throws(() => graph.getContentKeyByNodeId(node1));
58
+ });
59
+ });
42
60
  });
@@ -37,13 +37,6 @@ describe('Graph', () => {
37
37
  }, /Does not have node/);
38
38
  });
39
39
 
40
- it("errors if replaceNodeIdsConnectedTo is called with a node that doesn't belong", () => {
41
- let graph = new Graph();
42
- assert.throws(() => {
43
- graph.replaceNodeIdsConnectedTo(toNodeId(-1), []);
44
- }, /Does not have node/);
45
- });
46
-
47
40
  it("errors when adding an edge to a node that doesn't exist", () => {
48
41
  let graph = new Graph();
49
42
  let node = graph.addNode({});
@@ -274,26 +267,63 @@ describe('Graph', () => {
274
267
  }
275
268
  });
276
269
 
277
- it("replaceNodeIdsConnectedTo should update a node's downstream nodes", () => {
278
- let graph = new Graph();
279
- let nodeA = graph.addNode('a');
280
- graph.setRootNodeId(nodeA);
281
- let nodeB = graph.addNode('b');
282
- let nodeC = graph.addNode('c');
283
- graph.addEdge(nodeA, nodeB);
284
- graph.addEdge(nodeA, nodeC);
270
+ describe('replaceNodeIdsConnectedTo', () => {
271
+ it("errors if replaceNodeIdsConnectedTo is called with a node that doesn't belong", () => {
272
+ let graph = new Graph();
273
+ assert.throws(() => {
274
+ graph.replaceNodeIdsConnectedTo(toNodeId(-1), []);
275
+ }, /Does not have node/);
276
+ });
285
277
 
286
- let nodeD = graph.addNode('d');
287
- graph.replaceNodeIdsConnectedTo(nodeA, [nodeB, nodeD]);
278
+ it("replaceNodeIdsConnectedTo should update a node's downstream nodes", () => {
279
+ let graph = new Graph();
280
+ let nodeA = graph.addNode('a');
281
+ graph.setRootNodeId(nodeA);
282
+ let nodeB = graph.addNode('b');
283
+ let nodeC = graph.addNode('c');
284
+ graph.addEdge(nodeA, nodeB);
285
+ graph.addEdge(nodeA, nodeC);
286
+
287
+ let nodeD = graph.addNode('d');
288
+ graph.replaceNodeIdsConnectedTo(nodeA, [nodeB, nodeD]);
289
+
290
+ assert(graph.hasNode(nodeA));
291
+ assert(graph.hasNode(nodeB));
292
+ assert(!graph.hasNode(nodeC)); // orphan removed
293
+ assert(graph.hasNode(nodeD));
294
+ assert.deepEqual(Array.from(graph.getAllEdges()), [
295
+ {from: nodeA, to: nodeB, type: 1},
296
+ {from: nodeA, to: nodeD, type: 1},
297
+ ]);
298
+ });
288
299
 
289
- assert(graph.hasNode(nodeA));
290
- assert(graph.hasNode(nodeB));
291
- assert(!graph.hasNode(nodeC));
292
- assert(graph.hasNode(nodeD));
293
- assert.deepEqual(Array.from(graph.getAllEdges()), [
294
- {from: nodeA, to: nodeB, type: 1},
295
- {from: nodeA, to: nodeD, type: 1},
296
- ]);
300
+ it('replaceNodeIdsConnectedTo does not remove orphan nodes if flag is false', () => {
301
+ let graph = new Graph();
302
+ let nodeA = graph.addNode('a');
303
+ graph.setRootNodeId(nodeA);
304
+ let nodeB = graph.addNode('b');
305
+ let nodeC = graph.addNode('c');
306
+ graph.addEdge(nodeA, nodeB);
307
+ graph.addEdge(nodeA, nodeC);
308
+
309
+ let nodeD = graph.addNode('d');
310
+ graph.replaceNodeIdsConnectedTo(
311
+ nodeA,
312
+ [nodeB, nodeD],
313
+ () => true,
314
+ 1,
315
+ false,
316
+ );
317
+
318
+ assert(graph.hasNode(nodeA));
319
+ assert(graph.hasNode(nodeB));
320
+ assert(graph.hasNode(nodeC)); // orphan kept
321
+ assert(graph.hasNode(nodeD));
322
+ assert.deepEqual(Array.from(graph.getAllEdges()), [
323
+ {from: nodeA, to: nodeB, type: 1},
324
+ {from: nodeA, to: nodeD, type: 1},
325
+ ]);
326
+ });
297
327
  });
298
328
 
299
329
  it('traverses along edge types if a filter is given', () => {