Remove lodash forEach

(re: 6087)
This commit is contained in:
Bryan Housel
2019-03-28 19:50:05 -04:00
parent 5a0f8b3453
commit 4a8c20c56e
12 changed files with 100 additions and 126 deletions

View File

@@ -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');
@@ -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);
@@ -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);
@@ -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);
@@ -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');
@@ -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) {
@@ -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(', ');
}
});
@@ -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) {
@@ -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);

View File

@@ -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';
@@ -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) {
@@ -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;
@@ -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;
@@ -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;
@@ -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 = {};

View File

@@ -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';
@@ -26,7 +24,7 @@ var _impOsmUrls = {
};
function abortRequest(i) {
_forEach(i, function(v) {
Object.values(i).forEach(function(v) {
if (v) {
v.abort();
}
@@ -34,10 +32,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];
}
});
@@ -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);
} else {
return [0,0];
}
}
function relativeBearing(p1, p2) {
@@ -136,7 +127,7 @@ export default {
reset: function() {
if (_erCache) {
_forEach(_erCache.inflightTile, abortRequest);
Object.values(_erCache.inflightTile).forEach(abortRequest);
}
_erCache = {
data: {},
@@ -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,

View File

@@ -1,5 +1,3 @@
import _forEach from 'lodash-es/forEach';
import rbush from 'rbush';
import { dispatch as d3_dispatch } from 'd3-dispatch';
@@ -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];
}
});
@@ -275,8 +273,9 @@ export default {
reset: function() {
if (_krCache) {
_forEach(_krCache.inflightTile, abortRequest);
Object.values(_krCache.inflightTile).forEach(abortRequest);
}
_krCache = {
data: {},
loadedTile: {},

View File

@@ -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 {
@@ -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];
}
});
@@ -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 = {

View File

@@ -1,5 +1,3 @@
import _forEach from 'lodash-es/forEach';
import { json as d3_json } from 'd3-request';
import rbush from 'rbush';
@@ -7,22 +5,22 @@ import { geoExtent } from '../geo';
import { utilQsString } from '../util';
var apibase = 'https://nominatim.openstreetmap.org/',
inflight = {},
nominatimCache;
var apibase = 'https://nominatim.openstreetmap.org/';
var _inflight = {};
var _nominatimCache;
export default {
init: function() {
inflight = {};
nominatimCache = rbush();
_inflight = {};
_nominatimCache = rbush();
},
reset: function() {
_forEach(inflight, function(req) { req.abort(); });
inflight = {};
nominatimCache = rbush();
Object.values(_inflight).forEach(function(req) { req.abort(); });
_inflight = {};
_nominatimCache = rbush();
},
@@ -40,7 +38,7 @@ export default {
reverse: function (location, callback) {
var cached = nominatimCache.search(
var cached = _nominatimCache.search(
{ minX: location[0], minY: location[1], maxX: location[0], maxY: location[1] }
);
@@ -50,10 +48,10 @@ export default {
var params = { zoom: 13, format: 'json', addressdetails: 1, lat: location[1], lon: location[0] };
var url = apibase + 'reverse?' + utilQsString(params);
if (inflight[url]) return;
if (_inflight[url]) return;
inflight[url] = d3_json(url, function(err, result) {
delete inflight[url];
_inflight[url] = d3_json(url, function(err, result) {
delete _inflight[url];
if (err) {
return callback(err);
@@ -62,7 +60,7 @@ export default {
}
var extent = geoExtent(location).padByMeters(200);
nominatimCache.insert(Object.assign(extent.bbox(), {data: result}));
_nominatimCache.insert(Object.assign(extent.bbox(), {data: result}));
callback(null, result);
});
@@ -72,10 +70,10 @@ export default {
search: function (val, callback) {
var searchVal = encodeURIComponent(val);
var url = apibase + 'search/' + searchVal + '?limit=10&format=json';
if (inflight[url]) return;
if (_inflight[url]) return;
inflight[url] = d3_json(url, function(err, result) {
delete inflight[url];
_inflight[url] = d3_json(url, function(err, result) {
delete _inflight[url];
callback(err, result);
});
}

View File

@@ -1,5 +1,3 @@
import _forEach from 'lodash-es/forEach';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { request as d3_request } from 'd3-request';
@@ -56,10 +54,10 @@ function loadTiles(which, url, projection) {
// abort inflight requests that are no longer needed
var cache = _oscCache[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];
}
});
@@ -196,12 +194,8 @@ export default {
},
reset: function() {
var cache = _oscCache;
if (cache) {
if (cache.images && cache.images.inflight) {
_forEach(cache.images.inflight, abortRequest);
}
if (_oscCache) {
Object.values(_oscCache.images.inflight).forEach(abortRequest);
}
_oscCache = {

View File

@@ -1,5 +1,4 @@
import _cloneDeep from 'lodash-es/cloneDeep';
import _forEach from 'lodash-es/forEach';
import _throttle from 'lodash-es/throttle';
import rbush from 'rbush';
@@ -62,10 +61,10 @@ function abortRequest(i) {
function abortUnwantedRequests(cache, tiles) {
_forEach(cache.inflight, function(v, k) {
Object.keys(cache.inflight).forEach(function(k) {
var wanted = tiles.find(function(tile) { return k === tile.id; });
if (!wanted) {
abortRequest(v);
abortRequest(cache.inflight[k]);
delete cache.inflight[k];
}
});
@@ -363,9 +362,9 @@ export default {
_userDetails = undefined;
_rateLimitError = undefined;
_forEach(_tileCache.inflight, abortRequest);
_forEach(_noteCache.inflight, abortRequest);
_forEach(_noteCache.inflightPost, abortRequest);
Object.values(_tileCache.inflight).forEach(abortRequest);
Object.values(_noteCache.inflight).forEach(abortRequest);
Object.values(_noteCache.inflightPost).forEach(abortRequest);
if (_changeset.inflight) abortRequest(_changeset.inflight);
_tileCache = { loaded: {}, inflight: {}, seen: {} };

View File

@@ -1,5 +1,4 @@
import _debounce from 'lodash-es/debounce';
import _forEach from 'lodash-es/forEach';
import { json as d3_json } from 'd3-request';
@@ -51,7 +50,7 @@ export default {
reset: function() {
_forEach(_inflight, function(req) { req.abort(); });
Object.values(_inflight).forEach(function(req) { req.abort(); });
_inflight = {};
},
@@ -67,7 +66,7 @@ export default {
var locale = _localeIDs[langCode];
var preferredPick, localePick;
_forEach(entity.claims[property], function(stmt) {
entity.claims[property].forEach(function(stmt) {
// If exists, use value limited to the needed language (has a qualifier P26 = locale)
// Or if not found, use the first value with the "preferred" rank
if (!preferredPick && stmt.rank === 'preferred') {
@@ -194,7 +193,7 @@ export default {
callback(d.error.messages.map(function(v) { return v.html['*']; }).join('<br>'));
} else {
var localeID = false;
_forEach(d.entities, function(res) {
Object.values(d.entities).forEach(function(res) {
if (res.missing !== '') {
// Simplify access to the localized values
res.description = localizedToString(res.descriptions, params.langCode);

View File

@@ -1,5 +1,3 @@
import _forEach from 'lodash-es/forEach';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { timer as d3_timer } from 'd3-timer';
@@ -80,10 +78,10 @@ function loadTiles(which, url, projection, margin) {
// abort inflight requests that are no longer needed
var cache = _ssCache[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];
}
});
@@ -446,12 +444,8 @@ export default {
* reset() reset the cache.
*/
reset: function () {
var cache = _ssCache;
if (cache) {
if (cache.bubbles && cache.bubbles.inflight) {
_forEach(cache.bubbles.inflight, abortRequest);
}
if (_ssCache) {
Object.values(_ssCache.bubbles.inflight).forEach(abortRequest);
}
_ssCache = {

View File

@@ -1,5 +1,4 @@
import _debounce from 'lodash-es/debounce';
import _forEach from 'lodash-es/forEach';
import { json as d3_json } from 'd3-request';
@@ -208,7 +207,7 @@ export default {
reset: function() {
_forEach(_inflight, function(req) { req.abort(); });
Object.values(_inflight).forEach(function(request) { request.abort(); });
_inflight = {};
},

View File

@@ -1,5 +1,4 @@
import _isEqual from 'lodash-es/isEqual';
import _forEach from 'lodash-es/forEach';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { request as d3_request } from 'd3-request';
@@ -140,7 +139,7 @@ export default {
for (var sourceID in _vtCache) {
var source = _vtCache[sourceID];
if (source && source.inflight) {
_forEach(source.inflight, abortRequest);
Object.values(source.inflight).forEach(abortRequest);
}
}
@@ -191,10 +190,10 @@ export default {
var tiles = tiler.getTiles(projection);
// abort inflight requests that are no longer needed
_forEach(source.inflight, function(v, k) {
Object.keys(source.inflight).forEach(function(k) {
var wanted = tiles.find(function(tile) { return k === tile.id; });
if (!wanted) {
abortRequest(v);
abortRequest(source.inflight[k]);
delete source.inflight[k];
}
});