Skip to content

Commit

Permalink
Remove lodash forEach
Browse files Browse the repository at this point in the history
(re: 6087)
  • Loading branch information
bhousel committed Mar 28, 2019
1 parent 5a0f8b3 commit 4a8c20c
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 126 deletions.
28 changes: 16 additions & 12 deletions build_data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-disable no-console */
const requireESM = require('esm')(module);
const _forEach = requireESM('lodash-es/forEach').default;

const colors = require('colors/safe');
const fs = require('fs');
const glob = require('glob');
Expand Down Expand Up @@ -303,7 +300,8 @@ function generatePresets(tstrings, faIcons) {
function generateTranslations(fields, presets, tstrings) {
var translations = JSON.parse(JSON.stringify(tstrings)); // deep clone

_forEach(translations.fields, function(field, id) {
Object.keys(translations.fields).forEach(function(id) {
var field = translations.fields[id];
var f = fields[id];
var options = field.options || {};
var optkeys = Object.keys(options);
Expand All @@ -329,7 +327,8 @@ function generateTranslations(fields, presets, tstrings) {
}
});

_forEach(translations.presets, function(preset, id) {
Object.keys(translations.presets).forEach(function(id) {
var preset = translations.presets[id];
var p = presets[id];
var tags = p.tags || {};
var keys = Object.keys(tags);
Expand Down Expand Up @@ -368,7 +367,8 @@ function generateTaginfo(presets, fields) {
'tags': []
};

_forEach(presets, function(preset) {
Object.keys(presets).forEach(function(id) {
var preset = presets[id];
if (preset.suggestion) return;

var keys = Object.keys(preset.tags);
Expand Down Expand Up @@ -406,7 +406,8 @@ function generateTaginfo(presets, fields) {
coalesceTags(taginfo, tag);
});

_forEach(fields, function(field) {
Object.keys(fields).forEach(function(id) {
var field = fields[id];
var keys = field.keys || [ field.key ] || [];
var isRadio = (field.type === 'radio' || field.type === 'structureRadio');

Expand All @@ -431,7 +432,7 @@ function generateTaginfo(presets, fields) {
});
});

_forEach(deprecated, function(elem) {
deprecated.forEach(function(elem) {
var old = elem.old;
var oldKeys = Object.keys(old);
if (oldKeys.length === 1) {
Expand All @@ -455,9 +456,10 @@ function generateTaginfo(presets, fields) {
}
});

_forEach(taginfo.tags, function(elem) {
if (elem.description)
taginfo.tags.forEach(function(elem) {
if (elem.description) {
elem.description = elem.description.join(', ');
}
});


Expand Down Expand Up @@ -511,7 +513,8 @@ function generateTaginfo(presets, fields) {
}

function validateCategoryPresets(categories, presets) {
_forEach(categories, function(category) {
Object.keys(categories).forEach(function(id) {
var category = categories[id];
if (category.members) {
category.members.forEach(function(preset) {
if (presets[preset] === undefined) {
Expand Down Expand Up @@ -594,7 +597,8 @@ function validatePresetFields(presets, fields) {
}

function validateDefaults (defaults, categories, presets) {
_forEach(defaults.defaults, function (members, name) {
Object.keys(defaults.defaults).forEach(function(name) {
var members = defaults.defaults[name];
members.forEach(function (id) {
if (!presets[id] && !categories[id]) {
console.error('Unknown category or preset: ' + id + ' in default ' + name);
Expand Down
36 changes: 21 additions & 15 deletions modules/core/history.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _cloneDeep from 'lodash-es/cloneDeep';
import _cloneDeepWith from 'lodash-es/cloneDeepWith';
import _forEach from 'lodash-es/forEach';

import { dispatch as d3_dispatch } from 'd3-dispatch';
import { easeLinear as d3_easeLinear } from 'd3-ease';
Expand All @@ -11,8 +10,10 @@ import { coreGraph } from './graph';
import { coreTree } from './tree';
import { osmEntity } from '../osm/entity';
import { uiLoading } from '../ui';
import { utilArrayDifference, utilArrayGroupBy, utilArrayUnion,
utilObjectOmit, utilRebind, utilSessionMutex } from '../util';
import {
utilArrayDifference, utilArrayGroupBy, utilArrayUnion,
utilObjectOmit, utilRebind, utilSessionMutex
} from '../util';


export function coreHistory(context) {
Expand Down Expand Up @@ -355,13 +356,14 @@ export function coreHistory(context) {
var baseEntities = {};

// clone base entities..
_forEach(graph.base().entities, function(entity) {
Object.values(graph.base().entities).forEach(function(entity) {
var copy = _cloneDeepWith(entity, customizer);
baseEntities[copy.id] = copy;
});

// replace base entities with head entities..
_forEach(graph.entities, function(entity, id) {
Object.keys(graph.entities).forEach(function(id) {
var entity = graph.entities[id];
if (entity) {
var copy = _cloneDeepWith(entity, customizer);
baseEntities[copy.id] = copy;
Expand All @@ -371,7 +373,7 @@ export function coreHistory(context) {
});

// swap temporary for permanent ids..
_forEach(baseEntities, function(entity) {
Object.values(baseEntities).forEach(function(entity) {
if (Array.isArray(entity.nodes)) {
entity.nodes = entity.nodes.map(function(node) {
return permIds[node] || node;
Expand Down Expand Up @@ -423,7 +425,8 @@ export function coreHistory(context) {
var modified = [];
var deleted = [];

_forEach(i.graph.entities, function(entity, id) {
Object.keys(i.graph.entities).forEach(function(id) {
var entity = i.graph.entities[id];
if (entity) {
var key = osmEntity.key(entity);
allEntities[key] = entity;
Expand All @@ -439,18 +442,21 @@ export function coreHistory(context) {
}
if (entity && entity.nodes) {
// get originals of pre-existing child nodes
_forEach(entity.nodes, function(nodeId) {
if (nodeId in base.graph.entities) {
baseEntities[nodeId] = base.graph.entities[nodeId];
entity.nodes.forEach(function(nodeID) {
if (nodeID in base.graph.entities) {
baseEntities[nodeID] = base.graph.entities[nodeID];
}
});
}
// get originals of parent entities too
_forEach(base.graph._parentWays[id], function(parentId) {
if (parentId in base.graph.entities) {
baseEntities[parentId] = base.graph.entities[parentId];
}
});
var baseParents = base.graph._parentWays[id];
if (baseParents) {
baseParents.forEach(function(parentID) {
if (parentID in base.graph.entities) {
baseEntities[parentID] = base.graph.entities[parentID];
}
});
}
});

var x = {};
Expand Down
37 changes: 16 additions & 21 deletions modules/services/improveOSM.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import _forEach from 'lodash-es/forEach';

import rbush from 'rbush';

import { dispatch as d3_dispatch } from 'd3-dispatch';
import { json as d3_json } from 'd3-request';
import { request as d3_request } from 'd3-request';

import { geoExtent, geoVecAdd } from '../geo';
import { geoExtent, geoVecAdd, geoVecScale } from '../geo';
import { qaError } from '../osm';
import { services } from './index';
import { t } from '../util/locale';
Expand All @@ -26,18 +24,18 @@ var _impOsmUrls = {
};

function abortRequest(i) {
_forEach(i, function(v) {
Object.values(i).forEach(function(v) {
if (v) {
v.abort();
}
});
}

function abortUnwantedRequests(cache, tiles) {
_forEach(cache.inflightTile, function(v, k) {
Object.keys(cache.inflightTile).forEach(function(k) {
var wanted = tiles.find(function(tile) { return k === tile.id; });
if (!wanted) {
abortRequest(v);
abortRequest(cache.inflightTile[k]);
delete cache.inflightTile[k];
}
});
Expand Down Expand Up @@ -69,18 +67,11 @@ function linkEntity(d) {
}

function pointAverage(points) {
var x = 0;
var y = 0;

_forEach(points, function(v) {
x += v.lon;
y += v.lat;
});

x /= points.length;
y /= points.length;

return [x, y];
if (points.length) {
return geoVecScale(points.reduce(geoVecAdd, [0,0]), 1 / points.length);

This comment has been minimized.

Copy link
@kymckay

kymckay Mar 29, 2019

Collaborator

TIL about this handy reduce method. That said, I think this may be a breaking change as points is an array of objects with keys lat and lon which I presume geoVecAdd won't handle.

This comment has been minimized.

Copy link
@bhousel

bhousel Mar 29, 2019

Author Member

Thanks for catching that @SilentSpike !
I changed it to this:

function pointAverage(points) {
    if (points.length) {
        var sum = points.reduce(function(acc, point) {
            return geoVecAdd(acc, [point.lon, point.lat]);
        }, [0,0]);
        return geoVecScale(sum, 1 / points.length);
    } else {
        return [0,0];
    }
}

Yeah! reduce is pretty great for taking an array of stuff and turning it into a single value

} else {
return [0,0];
}
}

function relativeBearing(p1, p2) {
Expand Down Expand Up @@ -136,7 +127,7 @@ export default {

reset: function() {
if (_erCache) {
_forEach(_erCache.inflightTile, abortRequest);
Object.values(_erCache.inflightTile).forEach(abortRequest);
}
_erCache = {
data: {},
Expand Down Expand Up @@ -173,10 +164,14 @@ export default {
// 3 separate requests to store for each tile
var requests = {};

_forEach(_impOsmUrls, function(v, k) {
Object.keys(_impOsmUrls).forEach(function(k) {
var v = _impOsmUrls[k];
// We exclude WATER from missing geometry as it doesn't seem useful
// We use most confident one-way and turn restrictions only, still have false positives
var kParams = Object.assign({}, params, (k === 'mr') ? { type: 'PARKING,ROAD,BOTH,PATH' } : { confidenceLevel: 'C1' });
var kParams = Object.assign({},
params,
(k === 'mr') ? { type: 'PARKING,ROAD,BOTH,PATH' } : { confidenceLevel: 'C1' }
);
var url = v + '/search?' + utilQsString(kParams);

requests[k] = d3_json(url,
Expand Down
9 changes: 4 additions & 5 deletions modules/services/keepRight.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _forEach from 'lodash-es/forEach';

import rbush from 'rbush';

import { dispatch as d3_dispatch } from 'd3-dispatch';
Expand Down Expand Up @@ -39,10 +37,10 @@ function abortRequest(i) {
}

function abortUnwantedRequests(cache, tiles) {
_forEach(cache.inflightTile, function(v, k) {
Object.keys(cache.inflightTile).forEach(function(k) {
var wanted = tiles.find(function(tile) { return k === tile.id; });
if (!wanted) {
abortRequest(v);
abortRequest(cache.inflightTile[k]);
delete cache.inflightTile[k];
}
});
Expand Down Expand Up @@ -275,8 +273,9 @@ export default {

reset: function() {
if (_krCache) {
_forEach(_krCache.inflightTile, abortRequest);
Object.values(_krCache.inflightTile).forEach(abortRequest);
}

_krCache = {
data: {},
loadedTile: {},
Expand Down
26 changes: 7 additions & 19 deletions modules/services/mapillary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* global Mapillary:false */
import _forEach from 'lodash-es/forEach';

import { dispatch as d3_dispatch } from 'd3-dispatch';
import { request as d3_request } from 'd3-request';
import {
Expand Down Expand Up @@ -51,10 +49,10 @@ function loadTiles(which, url, projection) {

// abort inflight requests that are no longer needed
var cache = _mlyCache[which];
_forEach(cache.inflight, function(v, k) {
Object.keys(cache.inflight).forEach(function(k) {
var wanted = tiles.find(function(tile) { return k.indexOf(tile.id + ',') === 0; });
if (!wanted) {
abortRequest(v);
abortRequest(cache.inflight[k]);
delete cache.inflight[k];
}
});
Expand Down Expand Up @@ -244,21 +242,11 @@ export default {
},

reset: function() {
var cache = _mlyCache;

if (cache) {
if (cache.images && cache.images.inflight) {
_forEach(cache.images.inflight, abortRequest);
}
if (cache.image_detections && cache.image_detections.inflight) {
_forEach(cache.image_detections.inflight, abortRequest);
}
if (cache.map_features && cache.map_features.inflight) {
_forEach(cache.map_features.inflight, abortRequest);
}
if (cache.sequences && cache.sequences.inflight) {
_forEach(cache.sequences.inflight, abortRequest);
}
if (_mlyCache) {
Object.values(_mlyCache.images.inflight).forEach(abortRequest);
Object.values(_mlyCache.image_detections.inflight).forEach(abortRequest);
Object.values(_mlyCache.map_features.inflight).forEach(abortRequest);
Object.values(_mlyCache.sequences.inflight).forEach(abortRequest);
}

_mlyCache = {
Expand Down
Loading

0 comments on commit 4a8c20c

Please sign in to comment.