From 76e0d13729397c93ac0d0c99f2d43b574f5519f2 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sat, 23 Mar 2019 01:22:56 -0400 Subject: [PATCH] Remove lodash every and filter (re: #6087) --- build_data.js | 3 +-- modules/actions/move.js | 6 ++---- modules/core/history.js | 4 ++-- modules/core/validator.js | 17 ++++++++--------- modules/geo/geom.js | 4 +--- modules/modes/save.js | 12 ++++++++---- modules/operations/disconnect.js | 7 +++---- modules/operations/split.js | 9 ++++----- modules/osm/changeset.js | 3 +-- modules/osm/entity.js | 5 ++--- modules/services/maprules.js | 3 +-- modules/svg/lines.js | 5 ++--- modules/ui/fields/combo.js | 3 +-- modules/ui/panels/history.js | 5 ++--- modules/ui/panels/measurement.js | 9 ++++----- modules/ui/raw_membership_editor.js | 7 ++----- modules/ui/success.js | 4 +--- 17 files changed, 45 insertions(+), 61 deletions(-) diff --git a/build_data.js b/build_data.js index 8369dd944..b7374586a 100644 --- a/build_data.js +++ b/build_data.js @@ -5,7 +5,6 @@ const _forEach = requireESM('lodash-es/forEach').default; const _isEmpty = requireESM('lodash-es/isEmpty').default; const _merge = requireESM('lodash-es/merge').default; const _toPairs = requireESM('lodash-es/toPairs').default; -const _filter = requireESM('lodash-es/filter').default; const colors = require('colors/safe'); const fs = require('fs'); @@ -561,7 +560,7 @@ function validatePresetFields(presets, fields) { if (fieldCount > maxFieldsBeforeWarning) { // Fields with `prerequisiteTag` probably won't show up initially, // so don't count them against the limits. - var fieldsWithoutPrerequisites = _filter(preset.fields, function(fieldID) { + var fieldsWithoutPrerequisites = preset.fields.filter(function(fieldID) { if (fields[fieldID] && fields[fieldID].prerequisiteTag) { return false; } diff --git a/modules/actions/move.js b/modules/actions/move.js index 0dc8a716d..0cfa61eb4 100644 --- a/modules/actions/move.js +++ b/modules/actions/move.js @@ -1,5 +1,3 @@ -import _every from 'lodash-es/every'; -import _filter from 'lodash-es/filter'; import _intersection from 'lodash-es/intersection'; import _isEqual from 'lodash-es/isEqual'; import _map from 'lodash-es/map'; @@ -34,7 +32,7 @@ export function actionMove(moveIds, tryDelta, projection, cache) { if (parents.length < 3) return true; // Restrict movement of a vertex where >2 ways meet, unless all parentWays are moving too.. - var parentsMoving = _every(parents, function(id) { return cache.moving[id]; }); + var parentsMoving = parents.every(function(id) { return cache.moving[id]; }); if (!parentsMoving) delete cache.moving[nodeId]; return parentsMoving; @@ -118,7 +116,7 @@ export function actionMove(moveIds, tryDelta, projection, cache) { cacheEntities(moveIds); cacheIntersections(cache.ways); - cache.nodes = _filter(cache.nodes, canMove); + cache.nodes = cache.nodes.filter(canMove); cache.ok = true; } diff --git a/modules/core/history.js b/modules/core/history.js index 7ba8f8465..3308753c3 100644 --- a/modules/core/history.js +++ b/modules/core/history.js @@ -1,7 +1,6 @@ import _cloneDeep from 'lodash-es/cloneDeep'; import _cloneDeepWith from 'lodash-es/cloneDeepWith'; import _difference from 'lodash-es/difference'; -import _filter from 'lodash-es/filter'; import _flatten from 'lodash-es/flatten'; import _groupBy from 'lodash-es/groupBy'; import _isFunction from 'lodash-es/isFunction'; @@ -502,7 +501,8 @@ export function coreHistory(context) { // childnodes that would normally have been downloaded with it.. #2142 if (loadChildNodes) { var osm = context.connection(); - var nodes = _flatten(_uniq(_map(_filter(baseEntities, { type: 'way' }), 'nodes'))); + var baseWays = baseEntities.filter(function(e) { return e.type === 'way'; }); + var nodes = _flatten(_uniq(_map(baseWays, 'nodes'))); var missing = _reject(nodes, function(n) { return _stack[0].graph.hasEntity(n); }); if (!_isEmpty(missing) && osm) { diff --git a/modules/core/validator.js b/modules/core/validator.js index 0ee56b69f..fbd5de71a 100644 --- a/modules/core/validator.js +++ b/modules/core/validator.js @@ -1,5 +1,3 @@ -import _isFunction from 'lodash-es/isFunction'; -import _filter from 'lodash-es/filter'; import _flatten from 'lodash-es/flatten'; import _flattenDeep from 'lodash-es/flattenDeep'; import _uniq from 'lodash-es/uniq'; @@ -18,14 +16,15 @@ export function coreValidator(context) { var self = {}; var _issues = []; var _issuesByEntityID = {}; - var _disabledValidations = {}; - var validations = _filter(Validations, _isFunction).reduce(function(obj, validation) { - var func = validation(); - obj[func.type] = func; - return obj; - }, {}); + var validations = {}; + Object.values(Validations).forEach(function(validation) { + if (typeof validation === 'function') { + var fn = validation(); + validations[fn.type] = fn; + } + }); var entityValidationIDs = []; var changesValidationIDs = []; @@ -121,7 +120,7 @@ export function coreValidator(context) { } runValidation('missing_role'); - + if (entity.type === 'relation') { if (!runValidation('old_multipolygon')) { // don't flag missing tags if they are on the outer way diff --git a/modules/geo/geom.js b/modules/geo/geom.js index 0bb100e3d..7c5fb789f 100644 --- a/modules/geo/geom.js +++ b/modules/geo/geom.js @@ -1,5 +1,3 @@ -import _every from 'lodash-es/every'; - import { geoVecAngle, geoVecCross, @@ -257,7 +255,7 @@ export function geoPointInPolygon(point, polygon) { export function geoPolygonContainsPolygon(outer, inner) { - return _every(inner, function(point) { + return inner.every(function(point) { return geoPointInPolygon(point, outer); }); } diff --git a/modules/modes/save.js b/modules/modes/save.js index de31e86c0..fc9c5d5ca 100644 --- a/modules/modes/save.js +++ b/modules/modes/save.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _map from 'lodash-es/map'; import _reduce from 'lodash-es/reduce'; import _union from 'lodash-es/union'; @@ -132,7 +131,10 @@ export function modeSave(context) { // Do the full (slow) conflict check.. } else { - var modified = _filter(history.difference().summary(), { changeType: 'modified' }); + var summary = history.difference().summary(); + var modified = summary.filter(function(item) { + return item.changeType === 'modified'; + }); _toCheck = _map(_map(modified, 'entity'), 'id'); _toLoad = withChildNodes(_toCheck, localGraph); _loaded = {}; @@ -156,8 +158,10 @@ export function modeSave(context) { var entity = graph.entity(id); if (entity.type === 'way') { try { - var children = graph.childNodes(entity); - result.push.apply(result, _map(_filter(children, 'version'), 'id')); + var children = graph.childNodes(entity) + .filter(function(child) { return child.version !== undefined; }); + + result.push.apply(result, _map(children, 'id')); } catch (err) { /* eslint-disable no-console */ if (typeof console !== 'undefined') console.error(err); diff --git a/modules/operations/disconnect.js b/modules/operations/disconnect.js index 21aa783be..117d30b6a 100644 --- a/modules/operations/disconnect.js +++ b/modules/operations/disconnect.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _without from 'lodash-es/without'; import { t } from '../util/locale'; @@ -7,12 +6,12 @@ import { behaviorOperation } from '../behavior/index'; export function operationDisconnect(selectedIDs, context) { - var vertices = _filter(selectedIDs, function(entityId) { + var vertices = selectedIDs.filter(function(entityId) { return context.geometry(entityId) === 'vertex'; }); - var entityId = vertices[0], - action = actionDisconnect(entityId); + var entityId = vertices[0]; + var action = actionDisconnect(entityId); if (selectedIDs.length > 1) { action.limitWays(_without(selectedIDs, entityId)); diff --git a/modules/operations/split.js b/modules/operations/split.js index b3318836b..f35b99e4c 100644 --- a/modules/operations/split.js +++ b/modules/operations/split.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _without from 'lodash-es/without'; import { t } from '../util/locale'; @@ -8,13 +7,13 @@ import { modeSelect } from '../modes/index'; export function operationSplit(selectedIDs, context) { - var vertices = _filter(selectedIDs, function(entityId) { + var vertices = selectedIDs.filter(function(entityId) { return context.geometry(entityId) === 'vertex'; }); - var entityId = vertices[0], - action = actionSplit(entityId), - ways = []; + var entityId = vertices[0]; + var action = actionSplit(entityId); + var ways = []; if (vertices.length === 1) { if (selectedIDs.length > 1) { diff --git a/modules/osm/changeset.js b/modules/osm/changeset.js index 1ba39d5bc..122a31d05 100644 --- a/modules/osm/changeset.js +++ b/modules/osm/changeset.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _map from 'lodash-es/map'; import { osmEntity } from './entity'; @@ -102,7 +101,7 @@ Object.assign(osmChangeset.prototype, { while (processing.length > 0) { var next = processing[0], - deps = _filter(next.member.map(resolve).filter(Boolean), isNew); + deps = next.member.map(resolve).filter(Boolean).filter(isNew); if (deps.length === 0) { sorted[next['@id']] = next; processing.shift(); diff --git a/modules/osm/entity.js b/modules/osm/entity.js index b157fb8ae..f120d6af0 100644 --- a/modules/osm/entity.js +++ b/modules/osm/entity.js @@ -1,4 +1,3 @@ -import _every from 'lodash-es/every'; import _union from 'lodash-es/union'; import _without from 'lodash-es/without'; @@ -171,7 +170,7 @@ osmEntity.prototype = { var deprecated = []; dataDeprecated.forEach(function(d) { - var matchesDeprecatedTags = _every(Object.keys(d.old), function(key) { + var matchesDeprecatedTags = Object.keys(d.old).every(function(key) { if (!tags[key]) return false; if (d.old[key] === '*') return true; @@ -183,7 +182,7 @@ osmEntity.prototype = { } else { if (tags[key] === d.old[key]) { if (d.old[key] === d.replace[key]) { - return !_every(Object.keys(d.replace), function(key) { + return !Object.keys(d.replace).every(function(key) { return tags[key] === d.replace[key]; }); } else { diff --git a/modules/services/maprules.js b/modules/services/maprules.js index dd8bb0961..de176d36c 100644 --- a/modules/services/maprules.js +++ b/modules/services/maprules.js @@ -1,7 +1,6 @@ import _isMatch from 'lodash-es/isMatch'; import _intersection from 'lodash-es/intersection'; import _reduce from 'lodash-es/reduce'; -import _every from 'lodash-es/every'; import { areaKeys } from '../core/context'; import { @@ -201,7 +200,7 @@ export default { checks: this.filterRuleChecks(selector), // true if all conditions for a tag error are true.. matches: function(entity) { - return _every(this.checks, function(check) { + return this.checks.every(function(check) { return check(entity.tags); }); }, diff --git a/modules/svg/lines.js b/modules/svg/lines.js index 500b02fb7..dffd8a353 100644 --- a/modules/svg/lines.js +++ b/modules/svg/lines.js @@ -1,5 +1,4 @@ import _groupBy from 'lodash-es/groupBy'; -import _filter from 'lodash-es/filter'; import _flatten from 'lodash-es/flatten'; import _forOwn from 'lodash-es/forOwn'; import _map from 'lodash-es/map'; @@ -211,7 +210,7 @@ export function svgLines(projection, context) { pathdata = _groupBy(ways, function(way) { return way.layer(); }); _forOwn(pathdata, function(v, k) { - var onewayArr = _filter(v, function(d) { return d.isOneWay(); }); + var onewayArr = v.filter(function(d) { return d.isOneWay(); }); var onewaySegments = svgMarkerSegments( projection, graph, 35, function shouldReverse(entity) { return entity.tags.oneway === '-1'; }, @@ -221,7 +220,7 @@ export function svgLines(projection, context) { ); onewaydata[k] = _flatten(_map(onewayArr, onewaySegments)); - var sidedArr = _filter(v, function(d) { return d.isSided(); }); + var sidedArr = v.filter(function(d) { return d.isSided(); }); var sidedSegments = svgMarkerSegments( projection, graph, 30, function shouldReverse() { return false; }, diff --git a/modules/ui/fields/combo.js b/modules/ui/fields/combo.js index c39831358..e6d2668a7 100644 --- a/modules/ui/fields/combo.js +++ b/modules/ui/fields/combo.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _map from 'lodash-es/map'; import _reject from 'lodash-es/reject'; import _remove from 'lodash-es/remove'; @@ -185,7 +184,7 @@ export function uiFieldCombo(field, context) { taginfo[fn](params, function(err, data) { if (err) return; if (hasCountryPrefix) { - data = _filter(data, function(d) { + data = data.filter(function(d) { return d.value.toLowerCase().indexOf(_country + ':') === 0; }); } diff --git a/modules/ui/panels/history.js b/modules/ui/panels/history.js index 133955577..e92ed68cb 100644 --- a/modules/ui/panels/history.js +++ b/modules/ui/panels/history.js @@ -1,5 +1,3 @@ -import _filter from 'lodash-es/filter'; - import { t } from '../../util/locale'; import { svgIcon } from '../../svg'; import { utilDetect } from '../../util/detect'; @@ -104,7 +102,8 @@ export function uiPanelHistory(context) { selected = [ t('note.note') + ' ' + selectedNoteID ]; note = osm.getNote(selectedNoteID); } else { // selected 1..n entities - selected = _filter(context.selectedIDs(), function(e) { return context.hasEntity(e); }); + selected = context.selectedIDs() + .filter(function(e) { return context.hasEntity(e); }); if (selected.length) { entity = context.entity(selected[0]); } diff --git a/modules/ui/panels/measurement.js b/modules/ui/panels/measurement.js index aabdf7641..bdb65f682 100644 --- a/modules/ui/panels/measurement.js +++ b/modules/ui/panels/measurement.js @@ -1,5 +1,3 @@ -import _filter from 'lodash-es/filter'; - import { event as d3_event } from 'd3-selection'; import { @@ -15,8 +13,8 @@ import { services } from '../../services'; export function uiPanelMeasurement(context) { - var locale = utilDetect().locale, - isImperial = (locale.toLowerCase() === 'en-us'); + var locale = utilDetect().locale; + var isImperial = (locale.toLowerCase() === 'en-us'); function radiansToMeters(r) { @@ -65,7 +63,8 @@ export function uiPanelMeasurement(context) { } else { // selected 1..n entities var extent = geoExtent(); - selected = _filter(context.selectedIDs(), function(e) { return context.hasEntity(e); }); + selected = context.selectedIDs() + .filter(function(e) { return context.hasEntity(e); }); if (selected.length) { for (var i = 0; i < selected.length; i++) { entity = context.entity(selected[i]); diff --git a/modules/ui/raw_membership_editor.js b/modules/ui/raw_membership_editor.js index a60172147..0bc898c16 100644 --- a/modules/ui/raw_membership_editor.js +++ b/modules/ui/raw_membership_editor.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _groupBy from 'lodash-es/groupBy'; import { @@ -120,10 +119,8 @@ export function uiRawMembershipEditor(context) { }); // Dedupe identical names by appending relation id - see #2891 - var dupeGroups = _filter( - _groupBy(result, 'value'), - function(v) { return v.length > 1; } - ); + var dupeGroups = _groupBy(result, 'value') + .filter(function(v) { return v.length > 1; }); dupeGroups.forEach(function(group) { group.forEach(function(obj) { diff --git a/modules/ui/success.js b/modules/ui/success.js index 276a7152d..7b424016a 100644 --- a/modules/ui/success.js +++ b/modules/ui/success.js @@ -1,5 +1,3 @@ -import _filter from 'lodash-es/filter'; - import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select } from 'd3-selection'; @@ -132,7 +130,7 @@ export function uiSuccess(context) { var matchIDs = matchFeatures.map(function(feature) { return feature.id; }); // Gather community resources that are either global or match a polygon. - var matchResources = _filter(data.community.resources, function(v) { + var matchResources = data.community.resources.filter(function(v) { return !v.featureId || matchIDs.indexOf(v.featureId) !== -1; });