From 97dc659b0ff1feb8d9f8efdc00babd76f063d4fb Mon Sep 17 00:00:00 2001 From: J Guthrie Date: Sun, 24 Feb 2019 02:19:29 +0000 Subject: [PATCH] Fixed linting - moved function out of loop --- modules/actions/straighten.js | 21 +++++++++++++-------- modules/operations/straighten.js | 5 +++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/modules/actions/straighten.js b/modules/actions/straighten.js index c09187375..6207b316b 100644 --- a/modules/actions/straighten.js +++ b/modules/actions/straighten.js @@ -43,10 +43,10 @@ export function actionStraighten(selectedIDs, projection) { // and need to be removed so currNode _difference calculation below works) // i.e. ["n-1", "n-1", "n-2"] => ["n-2"] startNodes = _filter(startNodes, function(n) { - return startNodes.indexOf(n) == startNodes.lastIndexOf(n); + return startNodes.indexOf(n) === startNodes.lastIndexOf(n); }); endNodes = _filter(endNodes, function(n) { - return endNodes.indexOf(n) == endNodes.lastIndexOf(n); + return endNodes.indexOf(n) === endNodes.lastIndexOf(n); }); // Choose the initial endpoint to start from @@ -54,15 +54,20 @@ export function actionStraighten(selectedIDs, projection) { nextWay = []; nodes = []; + // Create nested function outside of loop to avoid "function in loop" lint error + var getNextWay = function(currNode, remainingWays) { + return _filter(remainingWays, function(way) { + return way[0] === currNode || way[way.length-1] === currNode; + })[0]; + }; + // Add nodes to end of nodes array, until all ways are added while (remainingWays.length) { - nextWay = _filter(remainingWays, function(way) { - return way[0] == currNode || way[way.length-1] == currNode; - })[0]; + nextWay = getNextWay(currNode, remainingWays); remainingWays = _difference(remainingWays, [nextWay]); - if (nextWay[0] != currNode) { + if (nextWay[0] !== currNode) { nextWay.reverse(); } nodes = nodes.concat(nextWay); @@ -71,7 +76,7 @@ export function actionStraighten(selectedIDs, projection) { } // If user selected 2 nodes to straighten between, then slice nodes array to those nodes - if (selectedNodes.length == 2) { + if (selectedNodes.length === 2) { var startNodeIdx = nodes.indexOf(selectedNodes[0]), endNodeIdx = nodes.indexOf(selectedNodes[1]), sortedStartEnd = [startNodeIdx, endNodeIdx]; @@ -84,7 +89,7 @@ export function actionStraighten(selectedIDs, projection) { } return nodes.map(function(n) { return graph.entity(n); }); - }; + } var action = function(graph, t) { if (t === null || !isFinite(t)) t = 1; diff --git a/modules/operations/straighten.js b/modules/operations/straighten.js index 3323539ee..72dc3d829 100644 --- a/modules/operations/straighten.js +++ b/modules/operations/straighten.js @@ -1,6 +1,7 @@ import _uniq from 'lodash-es/uniq'; import _difference from 'lodash-es/difference'; import _includes from 'lodash-es/includes'; +import _filter from 'lodash-es/filter'; import { t } from '../util/locale'; import { actionStraighten } from '../actions/index'; @@ -44,10 +45,10 @@ export function operationStraighten(selectedIDs, context) { // Remove duplicate end/startNodes (duplicate nodes cannot be at the line end) startNodes = _filter(startNodes, function(n) { - return startNodes.indexOf(n) == startNodes.lastIndexOf(n); + return startNodes.indexOf(n) === startNodes.lastIndexOf(n); }); endNodes = _filter(endNodes, function(n) { - return endNodes.indexOf(n) == endNodes.lastIndexOf(n); + return endNodes.indexOf(n) === endNodes.lastIndexOf(n); }); // Return false if line is only 2 nodes long