From c083d1d0af955490765eb5d0747ebb369d651d2c Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Mon, 11 Feb 2019 14:50:28 -0500 Subject: [PATCH] More nitpicky code formatting issues - no multiline var declarations - avoid lodash unless necessary - linefeeds for readability --- modules/core/validator.js | 121 +++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/modules/core/validator.js b/modules/core/validator.js index c0ea3049e..47c92120e 100644 --- a/modules/core/validator.js +++ b/modules/core/validator.js @@ -1,5 +1,3 @@ -import { dispatch as d3_dispatch } from 'd3-dispatch'; - import _isFunction from 'lodash-es/isFunction'; import _map from 'lodash-es/map'; import _filter from 'lodash-es/filter'; @@ -7,16 +5,19 @@ import _flatten from 'lodash-es/flatten'; import _flattenDeep from 'lodash-es/flattenDeep'; import _uniq from 'lodash-es/uniq'; import _uniqWith from 'lodash-es/uniqWith'; -import { osmEntity } from '../osm'; +import { dispatch as d3_dispatch } from 'd3-dispatch'; + +import { osmEntity } from '../osm'; import { utilRebind } from '../util/rebind'; import * as Validations from '../validations/index'; + export function coreValidator(context) { - var dispatch = d3_dispatch('reload'), - self = {}, - issues = [], - issuesByEntityId = {}; + var dispatch = d3_dispatch('reload'); + var self = {}; + var _issues = []; + var _issuesByEntityID = {}; var validations = _filter(Validations, _isFunction).reduce(function(obj, validation) { var func = validation(); @@ -24,15 +25,15 @@ export function coreValidator(context) { return obj; }, {}); - var entityValidationIds = [], - changesValidationIds = []; + var entityValidationIDs = []; + var changesValidationIDs = []; for (var key in validations) { var validation = validations[key]; if (validation.inputType && validation.inputType === 'changes') { - changesValidationIds.push(key); + changesValidationIDs.push(key); } else { - entityValidationIds.push(key); + entityValidationIDs.push(key); } } @@ -50,43 +51,40 @@ export function coreValidator(context) { };*/ self.getIssues = function() { - return issues; + return _issues; }; self.getWarnings = function() { - return issues.filter(function(issue) { - return issue.severity === 'warning'; - }); + return _issues.filter(function(d) { return d.severity === 'warning'; }); }; + self.getErrors = function() { - return issues.filter(function(issue) { - return issue.severity === 'error'; - }); + return _issues.filter(function(d) { return d.severity === 'error'; }); }; self.getIssuesForEntityWithID = function(entityID) { - if (!context.hasEntity(entityID)) { - return []; - } - if (!issuesByEntityId[entityID]) { + if (!context.hasEntity(entityID)) return []; + + if (!_issuesByEntityID[entityID]) { var entity = context.entity(entityID); - issuesByEntityId[entityID] = validateEntity(entity); + _issuesByEntityID[entityID] = validateEntity(entity); } - return issuesByEntityId[entityID]; + return _issuesByEntityID[entityID]; }; + function validateEntity(entity) { - var issues = []; + var _issues = []; var ranValidations = []; + // runs validation and appends resulting issues, returning true if validation passed function runValidation(type) { // only run each validation once if (ranValidations.indexOf(type) === -1) { var fn = validations[type]; var typeIssues = fn(entity, context); - issues = issues.concat(typeIssues); - // mark this validation as having run - ranValidations.push(type); + _issues = _issues.concat(typeIssues); + ranValidations.push(type); // mark this validation as having run return typeIssues.length === 0; } return true; @@ -100,9 +98,9 @@ export function coreValidator(context) { } // other validations require feature to be tagged - if (!runValidation('missing_tag')) return issues; - if (entity.type === 'way') { + if (!runValidation('missing_tag')) return _issues; + if (entity.type === 'way') { runValidation('crossing_ways'); // only check for disconnected way if no almost junctions @@ -114,37 +112,37 @@ export function coreValidator(context) { runValidation('tag_suggests_area'); } + // run all validations not yet run manually - entityValidationIds.forEach(function(id) { + entityValidationIDs.forEach(function(id) { runValidation(id); }); - return issues; + + return _issues; } + self.validate = function() { - // clear cached issues - issuesByEntityId = {}; - issues = []; + _issuesByEntityID = {}; // clear cached + _issues = []; var history = context.history(); var changes = history.changes(); var entitiesToCheck = changes.created.concat(changes.modified); var graph = history.graph(); - issues = _flatten(_map(changesValidationIds, function(ruleId) { - var validation = validations[ruleId]; + _issues = _flatten(_map(changesValidationIDs, function(ruleID) { + var validation = validations[ruleID]; return validation(changes, context); })); entitiesToCheck = _uniq(_flattenDeep(_map(entitiesToCheck, function(entity) { var entities = [entity]; - if (entity.type === 'node') { - // validate ways if their nodes have changed + if (entity.type === 'node') { // validate ways if their nodes have changed entities = entities.concat(graph.parentWays(entity)); } entities = _map(entities, function(entity) { - if (entity.type !== 'relation') { - // validate relations if their geometries have changed + if (entity.type !== 'relation') { // validate relations if their geometries have changed return [entity].concat(graph.parentRelations(entity)); } return entity; @@ -155,40 +153,40 @@ export function coreValidator(context) { for (var entityIndex in entitiesToCheck) { var entity = entitiesToCheck[entityIndex]; var entityIssues = validateEntity(entity); - issuesByEntityId[entity.id] = entityIssues; - issues = issues.concat(entityIssues); + _issuesByEntityID[entity.id] = entityIssues; + _issues = _issues.concat(entityIssues); } - issues = _uniqWith(issues, function(issue1, issue2) { + + _issues = _uniqWith(_issues, function(issue1, issue2) { return issue1.id() === issue2.id(); }); - dispatch.call('reload', self, issues); + + dispatch.call('reload', self, _issues); }; return utilRebind(self, dispatch, 'on'); } + export function validationIssue(attrs) { // A unique, deterministic string hash. // Issues with identical id values are considered identical. - this.id = function () { + this.id = function() { var id = this.type; - if (this.hash) { - // subclasses can pass in their own differentiator + if (this.hash) { // subclasses can pass in their own differentiator id += this.hash; } - // issue subclasses set the entity order but it must be deterministic - var entityKeys = _map(this.entities, function(entity) { - // use the key since it factors in the entity's local version - return osmEntity.key(entity); - }); // factor in the entities this issue is for + // (sort them so the id is deterministic) + var entityKeys = this.entities.map(osmEntity.key); id += entityKeys.sort().join(); + + // factor in coordinates since two separate issues can have an + // idential type and entities, e.g. in crossing_ways if (this.coordinates) { - // factor in coordinates since two separate issues can have an - // idential type and entities, e.g. in crossing_ways id += this.coordinates.join(); } return id; @@ -198,12 +196,11 @@ export function validationIssue(attrs) { this.severity = attrs.severity; this.message = attrs.message; this.tooltip = attrs.tooltip; - this.entities = attrs.entities; // expect an array of entities + this.entities = attrs.entities; // expect an array of entities this.coordinates = attrs.coordinates; // expect a [lon, lat] array - this.info = attrs.info; // an object containing arbitrary extra information - this.fixes = attrs.fixes; // expect an array of functions for possible fixes - - this.hash = attrs.hash; // an optional string to further differentiate the issue + this.info = attrs.info; // an object containing arbitrary extra information + this.fixes = attrs.fixes; // expect an array of functions for possible fixes + this.hash = attrs.hash; // an optional string to further differentiate the issue this.loc = function() { if (this.coordinates && Array.isArray(this.coordinates) && this.coordinates.length === 2) { @@ -217,15 +214,15 @@ export function validationIssue(attrs) { }; if (this.fixes) { - for (var i=0; i