@atlaspack/graph 3.2.1-dev.3566 → 3.3.0

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 = ALL_EDGE_TYPES) {
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 = ALL_EDGE_TYPES) {
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.3566+facdfb05f",
3
+ "version": "3.3.0",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "publishConfig": {
@@ -16,8 +16,7 @@
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/feature-flags": "2.12.1-dev.3566+facdfb05f",
19
+ "@atlaspack/feature-flags": "2.13.0",
20
20
  "nullthrows": "^1.1.1"
21
- },
22
- "gitHead": "facdfb05f693e50037a82a4afa101adf093fd8c9"
21
+ }
23
22
  }
@@ -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 = ALL_EDGE_TYPES,
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 = ALL_EDGE_TYPES,
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', () => {
package/LICENSE DELETED
@@ -1,201 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright (c) 2024 Atlassian US., Inc.
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.