|
| 1 | +"use strict"; |
1 | 2 | /** |
2 | 3 | * This file adapted from JSNetworkX: https://github.com/fkling/JSNetworkX |
3 | 4 | * Copyright (C) 2012 Felix Kling <felix.kling@gmx.net> |
4 | 5 | * JSNetworkX is distributed with the BSD license |
5 | 6 | */ |
6 | 7 |
|
7 | | -function isObjectLike(value) { |
8 | | - return !!value && typeof value == 'object'; |
9 | | -} |
10 | | - |
11 | | -function isBoolean(value) { |
12 | | - var boolTag = '[object Boolean]'; |
13 | | - return value === true || value === false || (isObjectLike(value) && Object.prototype.toString.call(value) == boolTag); |
14 | | -} |
15 | | - |
16 | | -function nodesAreEqual(a, b) { |
17 | | - return a === b || typeof a === 'object' && a.toString() === b.toString(); |
18 | | -} |
19 | | - |
20 | 8 | export function hasPath(G, {source, target}) { |
21 | | - try { |
22 | | - shortestPath(G, {source, target}); |
23 | | - } catch(error) { |
24 | | - if (error instanceof JSNetworkXNoPath) { |
25 | | - return false; |
26 | | - } |
27 | | - throw error; |
| 9 | + try { |
| 10 | + bidirectionalShortestPath(G, source, target) |
| 11 | + } catch(error) { |
| 12 | + if (error instanceof JSNetworkXNoPath) { |
| 13 | + return false; |
28 | 14 | } |
29 | | - return true; |
| 15 | + throw error; |
30 | 16 | } |
| 17 | + return true; |
| 18 | +} |
31 | 19 |
|
32 | | -function shortestPath(G, {source, target, weight}={}) { |
33 | | - return bidirectionalShortestPath(G, source, target); |
| 20 | +function nodesAreEqual(a, b) { |
| 21 | + return a === b || typeof a === 'object' && a.toString() === b.toString(); |
34 | 22 | } |
35 | 23 |
|
36 | 24 | function bidirectionalShortestPath(G, source, target) { |
@@ -107,7 +95,7 @@ function bidirectionalPredSucc(G, source, target) { |
107 | 95 | } |
108 | 96 | } |
109 | 97 | } |
110 | | - throw new JSNetworkXNoPath('No path between ' + source + ' and ' + target + "."); |
| 98 | + throw new JSNetworkXNoPath('No path between ' + source + ' and ' + target + '.'); |
111 | 99 | } |
112 | 100 |
|
113 | 101 | function topologicalSort(G, optNbunch) { |
@@ -389,3 +377,13 @@ class JSNetworkXNoPath extends JSNetworkXUnfeasible { |
389 | 377 | this.name = 'JSNetworkXNoPath'; |
390 | 378 | } |
391 | 379 | } |
| 380 | + |
| 381 | +// functions from LoDash, needed by functions from JSNetworkX |
| 382 | +function isObjectLike(value) { |
| 383 | + return !!value && typeof value == 'object'; |
| 384 | +} |
| 385 | + |
| 386 | +function isBoolean(value) { |
| 387 | + var boolTag = '[object Boolean]'; |
| 388 | + return value === true || value === false || (isObjectLike(value) && Object.prototype.toString.call(value) == boolTag); |
| 389 | +} |
0 commit comments