Skip to content

Commit

Permalink
Remove rest of the lodash iterators: map, reduce, forEach
Browse files Browse the repository at this point in the history
(re: #6087)
  • Loading branch information
bhousel committed Mar 29, 2019
1 parent 0d79e3e commit d5abe46
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 176 deletions.
29 changes: 11 additions & 18 deletions modules/actions/move.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import _isEqual from 'lodash-es/isEqual';
import _map from 'lodash-es/map';

import {
geoAngle,
geoChooseEdge,
geoPathIntersections,
geoPathLength,
geoVecAdd,
geoVecEqual,
geoVecInterp,
geoVecSubtract
geoAngle, geoChooseEdge, geoPathIntersections, geoPathLength,
geoVecAdd, geoVecEqual, geoVecInterp, geoVecSubtract
} from '../geo';

import { osmNode } from '../osm';
Expand All @@ -18,21 +11,21 @@ import { utilArrayIntersection } from '../util';

// https://github.com/openstreetmap/josm/blob/mirror/src/org/openstreetmap/josm/command/MoveCommand.java
// https://github.com/openstreetmap/potlatch2/blob/master/net/systemeD/halcyon/connection/actions/MoveNodeAction.as
export function actionMove(moveIds, tryDelta, projection, cache) {
export function actionMove(moveIDs, tryDelta, projection, cache) {
var _delta = tryDelta;

function setupCache(graph) {
function canMove(nodeId) {
function canMove(nodeID) {
// Allow movement of any node that is in the selectedIDs list..
if (moveIds.indexOf(nodeId) !== -1) return true;
if (moveIDs.indexOf(nodeID) !== -1) return true;

// Allow movement of a vertex where 2 ways meet..
var parents = _map(graph.parentWays(graph.entity(nodeId)), 'id');
var parents = graph.parentWays(graph.entity(nodeID));
if (parents.length < 3) return true;

// Restrict movement of a vertex where >2 ways meet, unless all parentWays are moving too..
var parentsMoving = parents.every(function(id) { return cache.moving[id]; });
if (!parentsMoving) delete cache.moving[nodeId];
var parentsMoving = parents.every(function(way) { return cache.moving[way.id]; });
if (!parentsMoving) delete cache.moving[nodeID];

return parentsMoving;
}
Expand Down Expand Up @@ -113,7 +106,7 @@ export function actionMove(moveIds, tryDelta, projection, cache) {
cache.nodes = [];
cache.ways = [];

cacheEntities(moveIds);
cacheEntities(moveIDs);
cacheIntersections(cache.ways);
cache.nodes = cache.nodes.filter(canMove);

Expand Down Expand Up @@ -331,9 +324,9 @@ export function actionMove(moveIds, tryDelta, projection, cache) {
var start = projection(node.loc);
var end = geoVecAdd(start, _delta);
var movedNodes = graph.childNodes(graph.entity(obj.movedId));
var movedPath = _map(_map(movedNodes, 'loc'), moveNode);
var movedPath = movedNodes.map(function(n) { return moveNode(n.loc); });
var unmovedNodes = graph.childNodes(graph.entity(obj.unmovedId));
var unmovedPath = _map(_map(unmovedNodes, 'loc'), projection);
var unmovedPath = unmovedNodes.map(function(n) { return projection(n.loc); });
var hits = geoPathIntersections(movedPath, unmovedPath);

for (var j = 0; i < hits.length; i++) {
Expand Down
22 changes: 8 additions & 14 deletions modules/behavior/lasso.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import _map from 'lodash-es/map';

import {
event as d3_event,
select as d3_select
} from 'd3-selection';

import {
geoExtent,
geoPointInPolygon
} from '../geo';
import { event as d3_event, select as d3_select } from 'd3-selection';

import { geoExtent, geoPointInPolygon } from '../geo';
import { modeSelect } from '../modes';
import { uiLasso } from '../ui';

Expand Down Expand Up @@ -47,7 +38,8 @@ export function behaviorLasso(context) {
function normalize(a, b) {
return [
[Math.min(a[0], b[0]), Math.min(a[1], b[1])],
[Math.max(a[0], b[0]), Math.max(a[1], b[1])]];
[Math.max(a[0], b[0]), Math.max(a[1], b[1])]
];
}


Expand All @@ -58,11 +50,13 @@ export function behaviorLasso(context) {
var bounds = lasso.extent().map(context.projection.invert);
var extent = geoExtent(normalize(bounds[0], bounds[1]));

return _map(context.intersects(extent).filter(function(entity) {
var intersects = context.intersects(extent).filter(function(entity) {
return entity.type === 'node' &&
geoPointInPolygon(context.projection(entity.loc), lasso.coordinates) &&
!context.features().isHidden(entity, graph, entity.geometry(graph));
}), 'id');
});

return intersects.map(function(entity) { return entity.id; });
}


Expand Down
1 change: 0 additions & 1 deletion modules/core/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { utilDetect } from '../util/detect';
import { utilCallWhenIdle, utilKeybinding, utilRebind, utilStringQs } from '../util';



export var areaKeys = {};

export function setAreaKeys(value) {
Expand Down
24 changes: 10 additions & 14 deletions modules/core/tree.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import _map from 'lodash-es/map';
import rbush from 'rbush';

import { coreDifference } from './difference';


export function coreTree(head) {
var rtree = rbush(),
bboxes = {},
tree = {};
var rtree = rbush();
var bboxes = {};
var tree = {};


function entityBBox(entity) {
Expand Down Expand Up @@ -44,9 +43,7 @@ export function coreTree(head) {

for (var i = 0; i < entities.length; i++) {
var entity = entities[i];

if (!entity.visible)
continue;
if (!entity.visible) continue;

if (head.entities.hasOwnProperty(entity.id) || bboxes[entity.id]) {
if (!force) {
Expand All @@ -60,16 +57,16 @@ export function coreTree(head) {
updateParents(entity, insertions, {});
}

rtree.load(_map(insertions, entityBBox));
rtree.load(Object.values(insertions).map(entityBBox));

return tree;
};


tree.intersects = function(extent, graph) {
if (graph !== head) {
var diff = coreDifference(head, graph),
insertions = {};
var diff = coreDifference(head, graph);
var insertions = {};

head = graph;

Expand All @@ -88,12 +85,11 @@ export function coreTree(head) {
insertions[entity.id] = entity;
});

rtree.load(_map(insertions, entityBBox));
rtree.load(Object.values(insertions).map(entityBBox));
}

return rtree.search(extent.bbox()).map(function(bbox) {
return head.entity(bbox.id);
});
return rtree.search(extent.bbox())
.map(function(bbox) { return head.entity(bbox.id); });
};


Expand Down
34 changes: 12 additions & 22 deletions modules/modes/save.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import _map from 'lodash-es/map';
import _reduce from 'lodash-es/reduce';

import {
event as d3_event,
select as d3_select
} from 'd3-selection';

import { event as d3_event, select as d3_select } from 'd3-selection';
import { t } from '../util/locale';

import { actionDiscardTags, actionMergeRemoteChanges, actionNoop, actionRevert } from '../actions';
Expand Down Expand Up @@ -132,22 +125,19 @@ export function modeSave(context) {


function withChildNodes(ids, graph) {
return utilArrayUniq(_reduce(ids, function(result, id) {
var s = new Set(ids);
ids.forEach(function(id) {
var entity = graph.entity(id);
if (entity.type === 'way') {
try {
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);
/* eslint-enable no-console */
if (entity.type !== 'way') return;

graph.childNodes(entity).forEach(function(child) {
if (child.version !== undefined) {
s.add(child.id);
}
}
return result;
}, ids.slice())); // shallow copy
});
});

return Array.from(s);
}


Expand Down
8 changes: 3 additions & 5 deletions modules/osm/changeset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _map from 'lodash-es/map';

import { osmEntity } from './entity';
import { geoExtent } from '../geo';

Expand Down Expand Up @@ -36,9 +34,9 @@ Object.assign(osmChangeset.prototype, {
return {
osm: {
changeset: {
tag: _map(this.tags, function(value, key) {
return { '@k': key, '@v': value };
}),
tag: Object.keys(this.tags).map(function(k) {
return { '@k': k, '@v': this.tags[k] };
}, this),
'@version': 0.6,
'@generator': 'iD'
}
Expand Down
8 changes: 3 additions & 5 deletions modules/osm/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _map from 'lodash-es/map';

import { osmEntity } from './entity';
import { geoAngle, geoExtent } from '../geo';
import { utilArrayUniq } from '../util';
Expand Down Expand Up @@ -221,9 +219,9 @@ Object.assign(osmNode.prototype, {
'@lon': this.loc[0],
'@lat': this.loc[1],
'@version': (this.version || 0),
tag: _map(this.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
tag: Object.keys(this.tags).map(function(k) {
return { keyAttributes: { k: k, v: this.tags[k] } };
}, this)
}
};
if (changeset_id) r.node['@changeset'] = changeset_id;
Expand Down
36 changes: 20 additions & 16 deletions modules/osm/relation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _map from 'lodash-es/map';

import { geoArea as d3_geoArea } from 'd3-geo';

import { osmEntity } from './entity';
Expand Down Expand Up @@ -173,8 +171,7 @@ Object.assign(osmRelation.prototype, {
// By default, adding a duplicate member (by id and role) is prevented.
// Return an updated relation.
replaceMember: function(needle, replacement, keepDuplicates) {
if (!this.memberById(needle.id))
return this;
if (!this.memberById(needle.id)) return this;

var members = [];

Expand All @@ -183,11 +180,11 @@ Object.assign(osmRelation.prototype, {
if (member.id !== needle.id) {
members.push(member);
} else if (keepDuplicates || !this.memberByIdAndRole(replacement.id, member.role)) {
members.push({id: replacement.id, type: replacement.type, role: member.role});
members.push({ id: replacement.id, type: replacement.type, role: member.role });
}
}

return this.update({members: members});
return this.update({ members: members });
},


Expand All @@ -196,21 +193,23 @@ Object.assign(osmRelation.prototype, {
relation: {
'@id': this.osmId(),
'@version': this.version || 0,
member: _map(this.members, function(member) {
member: this.members.map(function(member) {
return {
keyAttributes: {
type: member.type,
role: member.role,
ref: osmEntity.id.toOSM(member.id)
}
};
}),
tag: _map(this.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}, this),
tag: Object.keys(this.tags).map(function(k) {
return { keyAttributes: { k: k, v: this.tags[k] } };
}, this)
}
};
if (changeset_id) r.relation['@changeset'] = changeset_id;
if (changeset_id) {
r.relation['@changeset'] = changeset_id;
}
return r;
},

Expand Down Expand Up @@ -299,8 +298,12 @@ Object.assign(osmRelation.prototype, {
outers = osmJoinWays(outers, resolver);
inners = osmJoinWays(inners, resolver);

outers = outers.map(function(outer) { return _map(outer.nodes, 'loc'); });
inners = inners.map(function(inner) { return _map(inner.nodes, 'loc'); });
outers = outers.map(function(outer) {
return outer.nodes.map(function(node) { return node.loc; });
});
inners = inners.map(function(inner) {
return inner.nodes.map(function(node) { return node.loc; });
});

var result = outers.map(function(o) {
// Heuristic for detecting counterclockwise winding order. Assumes
Expand Down Expand Up @@ -332,10 +335,11 @@ Object.assign(osmRelation.prototype, {
}

var o = findOuter(inners[i]);
if (o !== undefined)
if (o !== undefined) {
result[o].push(inners[i]);
else
} else {
result.push([inners[i]]); // Invalid geometry
}
}

return result;
Expand Down
10 changes: 4 additions & 6 deletions modules/osm/way.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _map from 'lodash-es/map';

import { geoArea as d3_geoArea } from 'd3-geo';

import { geoExtent, geoVecCross } from '../geo';
Expand Down Expand Up @@ -437,10 +435,10 @@ Object.assign(osmWay.prototype, {
'@version': this.version || 0,
nd: this.nodes.map(function(id) {
return { keyAttributes: { ref: osmEntity.id.toOSM(id) } };
}),
tag: _map(this.tags, function(v, k) {
return { keyAttributes: { k: k, v: v } };
})
}, this),
tag: Object.keys(this.tags).map(function(k) {
return { keyAttributes: { k: k, v: this.tags[k] } };
}, this)
}
};
if (changeset_id) {
Expand Down
Loading

0 comments on commit d5abe46

Please sign in to comment.