Skip to content

Commit 2a91356

Browse files
authored
Merge pull request #40 from datavis-tech/var-to-let-const
Adopt let and const
2 parents a6104da + 34a6e56 commit 2a91356

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ function Graph(serialized) {
129129
}
130130
// Depth First Search algorithm, inspired by
131131
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
132-
// This variant includes an additional option
133-
// `includeSourceNodes` to specify whether to include or
134-
// exclude the source nodes from the result (true by default).
132+
// The additional option `includeSourceNodes` specifies whether to
133+
// include or exclude the source nodes from the result (true by default).
135134
// If `sourceNodes` is not specified, all nodes in the graph
136135
// are used as source nodes.
137136
function depthFirstSearch(sourceNodes, includeSourceNodes) {
@@ -273,13 +272,18 @@ function Graph(serialized) {
273272
function dijkstra() {
274273
initializeSingleSource();
275274
initializePriorityQueue();
276-
while (!priorityQueueEmpty()) {
275+
var _loop_1 = function () {
277276
var u = extractMin();
278277
if (u === null)
279-
return;
278+
return { value: void 0 };
280279
adjacent(u).forEach(function (v) {
281280
relax(u, v);
282281
});
282+
};
283+
while (!priorityQueueEmpty()) {
284+
var state_1 = _loop_1();
285+
if (typeof state_1 === "object")
286+
return state_1.value;
283287
}
284288
}
285289
// Assembles the shortest path by traversing the

index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ function Graph(serialized?: Serialized) {
3232
// The adjacency list of the graph.
3333
// Keys are node ids.
3434
// Values are adjacent node id arrays.
35-
var edges: Record<NodeId, NodeId[]> = {};
35+
const edges: Record<NodeId, NodeId[]> = {};
3636

3737
// The weights of edges.
3838
// Keys are string encodings of edges.
3939
// Values are weights (numbers).
40-
var edgeWeights: Record<EncodedEdge, EdgeWeight> = {};
40+
const edgeWeights: Record<EncodedEdge, EdgeWeight> = {};
4141

4242
// If a serialized graph was passed into the constructor, deserialize it.
4343
if (serialized) {
@@ -138,7 +138,7 @@ function Graph(serialized?: Serialized) {
138138
// Computes the indegree for the given node.
139139
// Not very efficient, costs O(E) where E = number of edges.
140140
function indegree(node: NodeId) {
141-
var degree = 0;
141+
let degree = 0;
142142
function check(v: NodeId) {
143143
if (v === node) {
144144
degree++;
@@ -157,9 +157,8 @@ function Graph(serialized?: Serialized) {
157157

158158
// Depth First Search algorithm, inspired by
159159
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
160-
// This variant includes an additional option
161-
// `includeSourceNodes` to specify whether to include or
162-
// exclude the source nodes from the result (true by default).
160+
// The additional option `includeSourceNodes` specifies whether to
161+
// include or exclude the source nodes from the result (true by default).
163162
// If `sourceNodes` is not specified, all nodes in the graph
164163
// are used as source nodes.
165164
function depthFirstSearch(
@@ -174,8 +173,8 @@ function Graph(serialized?: Serialized) {
174173
includeSourceNodes = true;
175174
}
176175

177-
var visited: Record<NodeId, boolean> = {};
178-
var nodeList: NodeId[] = [];
176+
const visited: Record<NodeId, boolean> = {};
177+
const nodeList: NodeId[] = [];
179178

180179
function DFSVisit(node: NodeId) {
181180
if (!visited[node]) {
@@ -203,8 +202,8 @@ function Graph(serialized?: Serialized) {
203202
// Inspired by https://github.com/relaxedws/lca/blob/master/src/LowestCommonAncestor.php code
204203
// but uses depth search instead of breadth. Also uses some optimizations
205204
function lowestCommonAncestors(node1: NodeId, node2: NodeId) {
206-
var node1Ancestors: NodeId[] = [];
207-
var lcas: NodeId[] = [];
205+
const node1Ancestors: NodeId[] = [];
206+
const lcas: NodeId[] = [];
208207

209208
function CA1Visit(
210209
visited: Record<NodeId, boolean>,
@@ -297,8 +296,8 @@ function Graph(serialized?: Serialized) {
297296

298297
// Linear search to extract (find and remove) min from q.
299298
function extractMin(): NodeId | null {
300-
var min = Infinity;
301-
var minNode;
299+
let min = Infinity;
300+
let minNode;
302301
Object.keys(q).forEach(function(node) {
303302
if (d[node] < min) {
304303
min = d[node];
@@ -315,7 +314,7 @@ function Graph(serialized?: Serialized) {
315314
}
316315

317316
function relax(u: NodeId, v: NodeId) {
318-
var w = getEdgeWeight(u, v);
317+
const w = getEdgeWeight(u, v);
319318
if (d[v] > d[u] + w) {
320319
d[v] = d[u] + w;
321320
p[v] = u;
@@ -326,7 +325,7 @@ function Graph(serialized?: Serialized) {
326325
initializeSingleSource();
327326
initializePriorityQueue();
328327
while (!priorityQueueEmpty()) {
329-
var u = extractMin();
328+
const u = extractMin();
330329
if (u === null) return;
331330
adjacent(u).forEach(function(v) {
332331
relax(u as string, v);
@@ -337,9 +336,9 @@ function Graph(serialized?: Serialized) {
337336
// Assembles the shortest path by traversing the
338337
// predecessor subgraph from destination to source.
339338
function path() {
340-
var nodeList: NodeId[] & { weight?: EdgeWeight } = [];
341-
var weight = 0;
342-
var node = destination;
339+
const nodeList: NodeId[] & { weight?: EdgeWeight } = [];
340+
let weight = 0;
341+
let node = destination;
343342
while (p[node]) {
344343
nodeList.push(node);
345344
weight += getEdgeWeight(p[node], node);
@@ -361,15 +360,15 @@ function Graph(serialized?: Serialized) {
361360

362361
// Serializes the graph.
363362
function serialize() {
364-
var serialized: Serialized = {
363+
const serialized: Serialized = {
365364
nodes: nodes().map(function(id) {
366365
return { id: id };
367366
}),
368367
links: []
369368
};
370369

371370
serialized.nodes.forEach(function(node) {
372-
var source = node.id;
371+
const source = node.id;
373372
adjacent(source).forEach(function(target) {
374373
serialized.links.push({
375374
source: source,

0 commit comments

Comments
 (0)