From 34ee77993f58ef4bc2d4ae683c387d06cab47f4c Mon Sep 17 00:00:00 2001 From: Hossein Marzban Date: Sun, 20 Dec 2020 09:48:49 +0330 Subject: [PATCH] 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 --- src/static/js/pluginfw/tsort.js | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/static/js/pluginfw/tsort.js b/src/static/js/pluginfw/tsort.js index 18e1f158a..f988de3ad 100644 --- a/src/static/js/pluginfw/tsort.js +++ b/src/static/js/pluginfw/tsort.js @@ -1,3 +1,5 @@ +'use strict'; + /** * general topological sort * from https://gist.github.com/1232505 @@ -7,7 +9,7 @@ * @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 sorted = []; // sorted list of IDs ( returned value ) const visited = {}; // hash: id of already visited node => true @@ -26,8 +28,7 @@ function tsort(edges) { nodes[from].afters.push(to); }); - // 2. topological sort - Object.keys(nodes).forEach(function visit(idstr, ancestors) { + const visit = (idstr, ancestors) => { const node = nodes[idstr]; const id = node.id; @@ -41,14 +42,17 @@ function tsort(edges) { visited[idstr] = true; 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}`); } + // if already in ancestors, a closed chain exists. + if (ancestors.indexOf(afterID) >= 0) throw new Error(`closed chain : ${afterID} is in ${id}`); visit(afterID.toString(), ancestors.map((v) => v)); // recursive call }); sorted.unshift(id); - }); + }; + + // 2. topological sort + Object.keys(nodes).forEach(visit); return sorted; } @@ -56,7 +60,7 @@ function tsort(edges) { /** * TEST **/ -function tsortTest() { +const tsortTest = () => { // example 1: success let edges = [ [1, 2], @@ -82,15 +86,13 @@ function tsortTest() { } // example 3: generate random edges - const max = 100; const - iteration = 30; - function randomInt(max) { - return Math.floor(Math.random() * max) + 1; - } + const max = 100; + const iteration = 30; + const randomInt = (max) => Math.floor(Math.random() * max) + 1; - edges = (function () { - const ret = []; let - i = 0; + edges = (() => { + const ret = []; + let i = 0; while (i++ < iteration) ret.push([randomInt(max), randomInt(max)]); return ret; })(); @@ -101,8 +103,7 @@ function tsortTest() { } catch (e) { console.log('failed', e.message); } -} - +}; // for node.js if (typeof exports === 'object' && exports === this) {