Lint: pluginfw tsort.js (#4576)

* lint: pluginfw tsort.js

* Don't comment out the `console.log()` call

Disabling the log message is out of scope for the pull request.

* Put const and let on separate lines

* Convert `tsort` from function to arrow function

ESLint doesn't complain about this due to a bug in
prefer-arrow/prefer-arrow-functions rule:
https://github.com/TristonJ/eslint-plugin-prefer-arrow/issues/24

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
pull/4596/head
Hossein Marzban 2020-12-20 09:48:49 +03:30 committed by GitHub
parent 040057239e
commit 34ee77993f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 17 deletions

View File

@ -1,3 +1,5 @@
'use strict';
/** /**
* general topological sort * general topological sort
* from https://gist.github.com/1232505 * from https://gist.github.com/1232505
@ -7,7 +9,7 @@
* @returns Array : topological sorted list of IDs * @returns Array : topological sorted list of IDs
**/ **/
function tsort(edges) { const tsort = (edges) => {
const nodes = {}; // hash: stringified id of the node => { id: id, afters: lisf of ids } const nodes = {}; // hash: stringified id of the node => { id: id, afters: lisf of ids }
const sorted = []; // sorted list of IDs ( returned value ) const sorted = []; // sorted list of IDs ( returned value )
const visited = {}; // hash: id of already visited node => true const visited = {}; // hash: id of already visited node => true
@ -26,8 +28,7 @@ function tsort(edges) {
nodes[from].afters.push(to); nodes[from].afters.push(to);
}); });
// 2. topological sort const visit = (idstr, ancestors) => {
Object.keys(nodes).forEach(function visit(idstr, ancestors) {
const node = nodes[idstr]; const node = nodes[idstr];
const id = node.id; const id = node.id;
@ -41,14 +42,17 @@ function tsort(edges) {
visited[idstr] = true; visited[idstr] = true;
node.afters.forEach((afterID) => { node.afters.forEach((afterID) => {
if (ancestors.indexOf(afterID) >= 0) // if already in ancestors, a closed chain exists. // if already in ancestors, a closed chain exists.
{ throw new Error(`closed chain : ${afterID} is in ${id}`); } if (ancestors.indexOf(afterID) >= 0) throw new Error(`closed chain : ${afterID} is in ${id}`);
visit(afterID.toString(), ancestors.map((v) => v)); // recursive call visit(afterID.toString(), ancestors.map((v) => v)); // recursive call
}); });
sorted.unshift(id); sorted.unshift(id);
}); };
// 2. topological sort
Object.keys(nodes).forEach(visit);
return sorted; return sorted;
} }
@ -56,7 +60,7 @@ function tsort(edges) {
/** /**
* TEST * TEST
**/ **/
function tsortTest() { const tsortTest = () => {
// example 1: success // example 1: success
let edges = [ let edges = [
[1, 2], [1, 2],
@ -82,15 +86,13 @@ function tsortTest() {
} }
// example 3: generate random edges // example 3: generate random edges
const max = 100; const const max = 100;
iteration = 30; const iteration = 30;
function randomInt(max) { const randomInt = (max) => Math.floor(Math.random() * max) + 1;
return Math.floor(Math.random() * max) + 1;
}
edges = (function () { edges = (() => {
const ret = []; let const ret = [];
i = 0; let i = 0;
while (i++ < iteration) ret.push([randomInt(max), randomInt(max)]); while (i++ < iteration) ret.push([randomInt(max), randomInt(max)]);
return ret; return ret;
})(); })();
@ -101,8 +103,7 @@ function tsortTest() {
} catch (e) { } catch (e) {
console.log('failed', e.message); console.log('failed', e.message);
} }
} };
// for node.js // for node.js
if (typeof exports === 'object' && exports === this) { if (typeof exports === 'object' && exports === this) {