lint: Run eslint --fix on src/

This commit is contained in:
Richard Hansen 2020-11-23 13:24:19 -05:00 committed by John McLear
parent b8d07a42eb
commit 8e5fd19db2
109 changed files with 9061 additions and 10572 deletions

View file

@ -8,27 +8,28 @@
**/
function tsort(edges) {
var nodes = {}, // hash: stringified id of the node => { id: id, afters: lisf of ids }
sorted = [], // sorted list of IDs ( returned value )
visited = {}; // hash: id of already visited node => true
const nodes = {}; // hash: stringified id of the node => { id: id, afters: lisf of ids }
const sorted = []; // sorted list of IDs ( returned value )
const visited = {}; // hash: id of already visited node => true
var Node = function(id) {
const Node = function (id) {
this.id = id;
this.afters = [];
}
};
// 1. build data structures
edges.forEach(function(v) {
var from = v[0], to = v[1];
edges.forEach((v) => {
const from = v[0]; const
to = v[1];
if (!nodes[from]) nodes[from] = new Node(from);
if (!nodes[to]) nodes[to] = new Node(to);
if (!nodes[to]) nodes[to] = new Node(to);
nodes[from].afters.push(to);
});
// 2. topological sort
Object.keys(nodes).forEach(function visit(idstr, ancestors) {
var node = nodes[idstr],
id = node.id;
const node = nodes[idstr];
const id = node.id;
// if already exists, do nothing
if (visited[idstr]) return;
@ -39,11 +40,11 @@ function tsort(edges) {
visited[idstr] = true;
node.afters.forEach(function(afterID) {
if (ancestors.indexOf(afterID) >= 0) // if already in ancestors, a closed chain exists.
throw new Error('closed chain : ' + afterID + ' is in ' + id);
node.afters.forEach((afterID) => {
if (ancestors.indexOf(afterID) >= 0) // if already in ancestors, a closed chain exists.
{ throw new Error(`closed chain : ${afterID} is in ${id}`); }
visit(afterID.toString(), ancestors.map(function(v) { return v })); // recursive call
visit(afterID.toString(), ancestors.map((v) => v)); // recursive call
});
sorted.unshift(id);
@ -56,57 +57,55 @@ function tsort(edges) {
* TEST
**/
function tsortTest() {
// example 1: success
var edges = [
let edges = [
[1, 2],
[1, 3],
[2, 4],
[3, 4]
[3, 4],
];
var sorted = tsort(edges);
let sorted = tsort(edges);
console.log(sorted);
// example 2: failure ( A > B > C > A )
edges = [
['A', 'B'],
['B', 'C'],
['C', 'A']
['C', 'A'],
];
try {
sorted = tsort(edges);
}
catch (e) {
} catch (e) {
console.log(e.message);
}
// example 3: generate random edges
var max = 100, iteration = 30;
const max = 100; const
iteration = 30;
function randomInt(max) {
return Math.floor(Math.random() * max) + 1;
}
edges = (function() {
var ret = [], i = 0;
while (i++ < iteration) ret.push( [randomInt(max), randomInt(max)] );
edges = (function () {
const ret = []; let
i = 0;
while (i++ < iteration) ret.push([randomInt(max), randomInt(max)]);
return ret;
})();
try {
sorted = tsort(edges);
console.log("succeeded", sorted);
console.log('succeeded', sorted);
} catch (e) {
console.log('failed', e.message);
}
catch (e) {
console.log("failed", e.message);
}
}
// for node.js
if (typeof exports == 'object' && exports === this) {
if (typeof exports === 'object' && exports === this) {
module.exports = tsort;
if (process.argv[1] === __filename) tsortTest();
}