Skip to content

Commit

Permalink
Remove lodash chunk, groupBy
Browse files Browse the repository at this point in the history
(re: #6087)
  • Loading branch information
bhousel committed Mar 27, 2019
1 parent 4cc8d79 commit 3d80e65
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 82 deletions.
8 changes: 3 additions & 5 deletions modules/actions/add_member.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _groupBy from 'lodash-es/groupBy';

import { utilObjectOmit } from '../util';
import { osmJoinWays, osmWay } from '../osm';
import { utilArrayGroupBy, utilObjectOmit } from '../util';


export function actionAddMember(relationId, member, memberIndex, insertPair) {
Expand Down Expand Up @@ -63,12 +61,12 @@ export function actionAddMember(relationId, member, memberIndex, insertPair) {
graph = graph.replace(tempWay);
var tempMember = { id: tempWay.id, type: 'way', role: member.role };
var tempRelation = relation.replaceMember({id: insertPair.originalID}, tempMember, true);
groups = _groupBy(tempRelation.members, function(m) { return m.type; });
groups = utilArrayGroupBy(tempRelation.members, 'type');
groups.way = groups.way || [];

} else {
// Add the member anywhere, one time. Just push and let `osmJoinWays` decide where to put it.
groups = _groupBy(relation.members, function(m) { return m.type; });
groups = utilArrayGroupBy(relation.members, 'type');
groups.way = groups.way || [];
groups.way.push(member);
}
Expand Down
9 changes: 4 additions & 5 deletions modules/actions/join.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import _groupBy from 'lodash-es/groupBy';

import { actionDeleteWay } from './delete_way';
import { osmIsInterestingTag, osmJoinWays } from '../osm';
import { geoPathIntersections } from '../geo';
import { utilArrayIntersection } from '../util';
import { utilArrayGroupBy, utilArrayIntersection } from '../util';


// Join ways at the end node they share.
Expand All @@ -18,8 +16,9 @@ export function actionJoin(ids) {

function groupEntitiesByGeometry(graph) {
var entities = ids.map(function(id) { return graph.entity(id); });
return Object.assign({ line: [] },
_groupBy(entities, function(entity) { return entity.geometry(graph); })
return Object.assign(
{ line: [] },
utilArrayGroupBy(entities, function(entity) { return entity.geometry(graph); })
);
}

Expand Down
10 changes: 5 additions & 5 deletions modules/actions/merge.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import _groupBy from 'lodash-es/groupBy';

import { utilArrayUniq } from '../util';
import { utilArrayGroupBy, utilArrayUniq } from '../util';


export function actionMerge(ids) {

function groupEntitiesByGeometry(graph) {
var entities = ids.map(function(id) { return graph.entity(id); });
return Object.assign({ point: [], area: [], line: [], relation: [] },
_groupBy(entities, function(entity) { return entity.geometry(graph); }));
return Object.assign(
{ point: [], area: [], line: [], relation: [] },
utilArrayGroupBy(entities, function(entity) { return entity.geometry(graph); })
);
}


Expand Down
31 changes: 15 additions & 16 deletions modules/actions/merge_polygon.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import _groupBy from 'lodash-es/groupBy';

import { geoPolygonContainsPolygon } from '../geo';
import { osmJoinWays, osmRelation } from '../osm';
import { utilObjectOmit } from '../util';
import { utilArrayGroupBy, utilObjectOmit } from '../util';


export function actionMergePolygon(ids, newRelationId) {

function groupEntities(graph) {
var entities = ids.map(function (id) { return graph.entity(id); });
return Object.assign({
closedWay: [],
multipolygon: [],
other: []
}, _groupBy(entities, function(entity) {
if (entity.type === 'way' && entity.isClosed()) {
return 'closedWay';
} else if (entity.type === 'relation' && entity.isMultipolygon()) {
return 'multipolygon';
} else {
return 'other';
}
}));
var geometryGroups = utilArrayGroupBy(entities, function(entity) {
if (entity.type === 'way' && entity.isClosed()) {
return 'closedWay';
} else if (entity.type === 'relation' && entity.isMultipolygon()) {
return 'multipolygon';
} else {
return 'other';
}
});

return Object.assign(
{ closedWay: [], multipolygon: [], other: [] },
geometryGroups
);
}


Expand Down
13 changes: 7 additions & 6 deletions modules/behavior/copy.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import _groupBy from 'lodash-es/groupBy';
import _map from 'lodash-es/map';

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

import { uiCmd } from '../ui';
import { utilArrayGroupBy } from '../util';


export function behaviorCopy(context) {

function groupEntities(ids, graph) {
var entities = ids.map(function (id) { return graph.entity(id); });
return Object.assign({relation: [], way: [], node: []},
_groupBy(entities, function(entity) { return entity.type; }));
return Object.assign(
{ relation: [], way: [], node: [] },
utilArrayGroupBy(entities, 'type')
);
}


Expand All @@ -21,7 +22,7 @@ export function behaviorCopy(context) {
descendants = descendants || {};

if (entity.type === 'relation') {
children = _map(entity.members, 'id');
children = entity.members.map(function(m) { return m.id; });
} else if (entity.type === 'way') {
children = entity.nodes;
} else {
Expand Down
6 changes: 3 additions & 3 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 _groupBy from 'lodash-es/groupBy';
import _forEach from 'lodash-es/forEach';

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


export function coreHistory(context) {
Expand Down Expand Up @@ -517,7 +517,7 @@ export function coreHistory(context) {

var childNodesLoaded = function(err, result) {
if (!err) {
var visibleGroups = _groupBy(result.data, 'visible');
var visibleGroups = utilArrayGroupBy(result.data, 'visible');
var visibles = visibleGroups.true || []; // alive nodes
var invisibles = visibleGroups.false || []; // deleted nodes

Expand Down
9 changes: 5 additions & 4 deletions modules/operations/continue.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import _groupBy from 'lodash-es/groupBy';

import { t } from '../util/locale';
import { modeDrawLine } from '../modes';
import { behaviorOperation } from '../behavior';
import { utilArrayGroupBy } from '../util';


export function operationContinue(selectedIDs, context) {
var graph = context.graph();
var entities = selectedIDs.map(function(id) { return graph.entity(id); });
var geometries = Object.assign({ line: [], vertex: [] },
_groupBy(entities, function(entity) { return entity.geometry(graph); }));
var geometries = Object.assign(
{ line: [], vertex: [] },
utilArrayGroupBy(entities, function(entity) { return entity.geometry(graph); })
);
var vertex = geometries.vertex[0];


Expand Down
8 changes: 3 additions & 5 deletions modules/renderer/features.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import _groupBy from 'lodash-es/groupBy';

import { dispatch as d3_dispatch } from 'd3-dispatch';

import { osmEntity } from '../osm';
import { utilRebind } from '../util/rebind';
import { utilArrayUnion, utilQsString, utilStringQs } from '../util';
import { utilArrayGroupBy, utilArrayUnion, utilQsString, utilStringQs } from '../util';


export function rendererFeatures(context) {
Expand Down Expand Up @@ -276,8 +274,8 @@ export function rendererFeatures(context) {

features.gatherStats = function(d, resolver, dimensions) {
var needsRedraw = false;
var type = _groupBy(d, function(ent) { return ent.type; });
var entities = [].concat(type.relation || [], type.way || [], type.node || []);
var types = utilArrayGroupBy(d, 'type');
var entities = [].concat(types.relation || [], types.way || [], types.node || []);
var currHidden, geometry, matches, i, j;

for (i = 0; i < _keys.length; i++) {
Expand Down
17 changes: 10 additions & 7 deletions modules/services/osm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _chunk from 'lodash-es/chunk';
import _cloneDeep from 'lodash-es/cloneDeep';
import _forEach from 'lodash-es/forEach';
import _groupBy from 'lodash-es/groupBy';
import _throttle from 'lodash-es/throttle';

import rbush from 'rbush';
Expand All @@ -13,7 +11,11 @@ import osmAuth from 'osm-auth';
import { JXON } from '../util/jxon';
import { geoExtent, geoVecAdd } from '../geo';
import { osmEntity, osmNode, osmNote, osmRelation, osmWay } from '../osm';
import { utilArrayUniq, utilRebind, utilIdleWorker, utilTiler, utilQsString } from '../util';

import {
utilArrayChunk, utilArrayGroupBy, utilArrayUniq, utilRebind,
utilIdleWorker, utilTiler, utilQsString
} from '../util';


var tiler = utilTiler();
Expand Down Expand Up @@ -509,13 +511,14 @@ export default {
// GET /api/0.6/[nodes|ways|relations]?#parameters
loadMultiple: function(ids, callback) {
var that = this;
var groups = utilArrayGroupBy(utilArrayUniq(ids), osmEntity.id.type);

_forEach(_groupBy(utilArrayUniq(ids), osmEntity.id.type), function(v, k) {
Object.keys(groups).forEach(function(k) {
var type = k + 's'; // nodes, ways, relations
var osmIDs = v.map(function(id) { return osmEntity.id.toOSM(id); });
var osmIDs = groups[k].map(function(id) { return osmEntity.id.toOSM(id); });
var options = { skipSeen: false };

_chunk(osmIDs, 150).forEach(function(arr) {
utilArrayChunk(osmIDs, 150).forEach(function(arr) {
that.loadFromAPI(
'/api/0.6/' + type + '?' + type + '=' + arr.join(),
function(err, entities) {
Expand Down Expand Up @@ -620,7 +623,7 @@ export default {
if (!this.authenticated()) return; // require auth
}

_chunk(toLoad, 150).forEach(function(arr) {
utilArrayChunk(toLoad, 150).forEach(function(arr) {
oauth.xhr(
{ method: 'GET', path: '/api/0.6/users?users=' + arr.join() },
wrapcb(this, done, _connectionID)
Expand Down
16 changes: 6 additions & 10 deletions modules/svg/lines.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import _groupBy from 'lodash-es/groupBy';
import _flatten from 'lodash-es/flatten';
import _forOwn from 'lodash-es/forOwn';
import _map from 'lodash-es/map';

import { range as d3_range } from 'd3-array';

import {
svgMarkerSegments,
svgPath,
svgRelationMemberTags,
svgSegmentWay,
svgTagClasses
svgMarkerSegments, svgPath, svgRelationMemberTags,
svgSegmentWay, svgTagClasses
} from './index';

import { osmEntity, osmOldMultipolygonOuterMember } from '../osm';
import { utilArrayGroupBy } from '../util';
import { utilDetect } from '../util/detect';


Expand Down Expand Up @@ -190,7 +186,6 @@ export function svgLines(projection, context) {

var getPath = svgPath(projection, graph);
var ways = [];
var pathdata = {};
var onewaydata = {};
var sideddata = {};
var oldMultiPolygonOuters = {};
Expand All @@ -207,9 +202,10 @@ export function svgLines(projection, context) {
}

ways = ways.filter(getPath);
pathdata = _groupBy(ways, function(way) { return way.layer(); });
var pathdata = utilArrayGroupBy(ways, function(way) { return way.layer(); });

_forOwn(pathdata, function(v, k) {
Object.keys(pathdata).forEach(function(k) {
var v = pathdata[k];
var onewayArr = v.filter(function(d) { return d.isOneWay(); });
var onewaySegments = svgMarkerSegments(
projection, graph, 35,
Expand Down
6 changes: 2 additions & 4 deletions modules/ui/raw_membership_editor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _groupBy from 'lodash-es/groupBy';

import {
event as d3_event,
select as d3_select
Expand All @@ -19,7 +17,7 @@ import { osmEntity, osmRelation } from '../osm';
import { services } from '../services';
import { svgIcon } from '../svg';
import { uiCombobox, uiDisclosure } from './index';
import { utilDisplayName, utilNoAuto, utilHighlightEntities } from '../util';
import { utilArrayGroupBy, utilDisplayName, utilNoAuto, utilHighlightEntities } from '../util';


export function uiRawMembershipEditor(context) {
Expand Down Expand Up @@ -119,7 +117,7 @@ export function uiRawMembershipEditor(context) {
});

// Dedupe identical names by appending relation id - see #2891
var dupeGroups = Object.values(_groupBy(result, 'value'))
var dupeGroups = Object.values(utilArrayGroupBy(result, 'value'))
.filter(function(v) { return v.length > 1; });

dupeGroups.forEach(function(group) {
Expand Down
45 changes: 45 additions & 0 deletions modules/util/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,48 @@ export function utilArrayUnion(a, b) {
export function utilArrayUniq(a) {
return Array.from(new Set(a));
}

// Splits array into chunks of given chunk size
// var a = [1,2,3,4,5,6,7];
// utilArrayChunk(a, 3);
// [[1,2,3],[4,5,6],[7]];
export function utilArrayChunk(a, chunkSize) {
if (!chunkSize || chunkSize < 0) return [a.slice()];

var result = new Array(Math.ceil(a.length / chunkSize));
return Array.from(result, function(item, i) {
return a.slice(i * chunkSize, i * chunkSize + chunkSize);
});
}


// Groups the items of the Array according to the given key
// `key` can be passed as a property or as a key function
//
// var pets = [
// { type: 'Dog', name: 'Spot' },
// { type: 'Cat', name: 'Tiger' },
// { type: 'Dog', name: 'Rover' },
// { type: 'Cat', name: 'Leo' }
// ];
//
// utilArrayGroupBy(pets, 'type')
// {
// 'Dog': [{type: 'Dog', name: 'Spot'}, {type: 'Dog', name: 'Rover'}],
// 'Cat': [{type: 'Cat', name: 'Tiger'}, {type: 'Cat', name: 'Leo'}]
// }
//
// utilArrayGroupBy(pets, function(item) { return item.name.length; })
// {
// 3: [{type: 'Cat', name: 'Leo'}],
// 4: [{type: 'Dog', name: 'Spot'}],
// 5: [{type: 'Cat', name: 'Tiger'}, {type: 'Dog', name: 'Rover'}]
// }
export function utilArrayGroupBy(a, key) {
return a.reduce(function(acc, item) {
var group = (typeof key === 'function') ? key(item) : item[key];
(acc[group] = acc[group] || []).push(item);
return acc;
}, {});
}

3 changes: 3 additions & 0 deletions modules/util/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export { utilArrayChunk } from './array';
export { utilArrayDifference } from './array';
export { utilArrayGroupBy } from './array';
export { utilArrayIntersection } from './array';
export { utilArrayUnion } from './array';
export { utilArrayUniq } from './array';

export { utilAsyncMap } from './util';
export { utilCallWhenIdle } from './call_when_idle';
export { utilCleanTags } from './clean_tags';
Expand Down
Loading

0 comments on commit 3d80e65

Please sign in to comment.