Skip to content

Commit

Permalink
Add browser-polyfills, remove lodash assign, compact, values
Browse files Browse the repository at this point in the history
(re: #6087)
  • Loading branch information
bhousel committed Mar 22, 2019
1 parent 547267d commit 11bfeaa
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 132 deletions.
18 changes: 14 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,21 @@ you're going to update the presets, [review the Presets README](/data/presets/RE

## JavaScript

iD uses ES5 syntax throughout the code, with one exception:
[ES6 modules](http://exploringjs.com/es6/ch_modules.html) using `import` and
iD uses ES5 syntax throughout the code, with the following exceptions:
- [ES6 modules](http://exploringjs.com/es6/ch_modules.html) using `import` and
`export` constructs. These are processed by [Rollup.js](https://rollupjs.org/guide/en)
and not present in the distributed iD bundle.
- Polyfills provided by [browser-polyfills](https://github.com/tiagomapmarques/browser-polyfills#what-does-it-have)
- `Promise`
- `fetch`
- `Map`
- `Set`
- `Array.find`
- `Array.findIndex`
- `Array.from`
- `Object.values`
- `Object.assign`
- `requestAnimationFrame (rAF)`

ES5 syntax is required for:
* IE11, which about 15-20% of our users still use
Expand All @@ -307,8 +318,7 @@ only one difference: **4 space soft tabs always for JavaScript, not 2.**
No aligned `=`, no aligned arguments, spaces are either indents or the 1
space between expressions. No hard tabs, ever.

JavaScript code should pass through [ESLint](http://eslint.org/) with no
warnings.
JavaScript code should pass through [ESLint](http://eslint.org/) with no warnings.


## HTML
Expand Down
4 changes: 1 addition & 3 deletions data/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _values from 'lodash-es/values';

export { wikipedia as dataWikipedia } from 'wmf-sitematrix';

export { dataAddressFormats } from './address-formats.json';
Expand Down Expand Up @@ -29,7 +27,7 @@ import whichPolygon from 'which-polygon';


// index the osm-community-index
var ociFeatureCollection = _values(ociFeatures).map(function(feature) {
var ociFeatureCollection = Object.values(ociFeatures).map(function(feature) {
// workaround for which-polygon: only supports `properties`, not `id`
// https://github.com/mapbox/which-polygon/pull/6
feature.properties = {
Expand Down
9 changes: 2 additions & 7 deletions modules/behavior/hash.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import _assign from 'lodash-es/assign';
import _omit from 'lodash-es/omit';
import _throttle from 'lodash-es/throttle';

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

import { geoSphericalDistance } from '../geo';
import { modeBrowse } from '../modes';

import {
utilQsString,
utilStringQs
} from '../util';
import { utilQsString, utilStringQs } from '../util';


export function behaviorHash(context) {
Expand Down Expand Up @@ -62,7 +57,7 @@ export function behaviorHash(context) {
'/' + center[1].toFixed(precision) +
'/' + center[0].toFixed(precision);

return '#' + utilQsString(_assign(q, newParams), true);
return '#' + utilQsString(Object.assign(q, newParams), true);
};


Expand Down
3 changes: 1 addition & 2 deletions modules/core/difference.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _difference from 'lodash-es/difference';
import _each from 'lodash-es/each';
import _isEqual from 'lodash-es/isEqual';
import _values from 'lodash-es/values';


/*
Expand Down Expand Up @@ -167,7 +166,7 @@ export function coreDifference(base, head) {
}
}

return _values(relevant);
return Object.values(relevant);
};


Expand Down
7 changes: 3 additions & 4 deletions modules/core/graph.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _assign from 'lodash-es/assign';
import _difference from 'lodash-es/difference';
import _includes from 'lodash-es/includes';
import _without from 'lodash-es/without';
Expand All @@ -12,9 +11,9 @@ export function coreGraph(other, mutable) {

if (other instanceof coreGraph) {
var base = other.base();
this.entities = _assign(Object.create(base.entities), other.entities);
this._parentWays = _assign(Object.create(base.parentWays), other._parentWays);
this._parentRels = _assign(Object.create(base.parentRels), other._parentRels);
this.entities = Object.assign(Object.create(base.entities), other.entities);
this._parentWays = Object.assign(Object.create(base.parentWays), other._parentWays);
this._parentRels = Object.assign(Object.create(base.parentRels), other._parentRels);

} else {
this.entities = Object.create({});
Expand Down
11 changes: 3 additions & 8 deletions modules/core/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import _forEach from 'lodash-es/forEach';
import _map from 'lodash-es/map';
import _omit from 'lodash-es/omit';
import _reject from 'lodash-es/reject';
import _values from 'lodash-es/values';
import _without from 'lodash-es/without';
import _uniq from 'lodash-es/uniq';

Expand All @@ -23,11 +22,7 @@ import { coreGraph } from './graph';
import { coreTree } from './tree';
import { osmEntity } from '../osm/entity';
import { uiLoading } from '../ui';

import {
utilRebind,
utilSessionMutex
} from '../util';
import { utilRebind, utilSessionMutex } from '../util';


export function coreHistory(context) {
Expand Down Expand Up @@ -472,8 +467,8 @@ export function coreHistory(context) {

return JSON.stringify({
version: 3,
entities: _values(allEntities),
baseEntities: _values(baseEntities),
entities: Object.values(allEntities),
baseEntities: Object.values(baseEntities),
stack: s,
nextIDs: osmEntity.id.next,
index: _index
Expand Down
1 change: 1 addition & 0 deletions modules/id.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import 'browser-polyfills';
import * as iD from './index';
window.iD = iD;
3 changes: 1 addition & 2 deletions modules/modes/select.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _intersection from 'lodash-es/intersection';
import _map from 'lodash-es/map';
import _uniq from 'lodash-es/uniq';
import _values from 'lodash-es/values';
import _without from 'lodash-es/without';

import {
Expand Down Expand Up @@ -241,7 +240,7 @@ export function modeSelect(context, selectedIDs) {

context.features().forceVisible(selectedIDs);

var operations = _without(_values(Operations), Operations.operationDelete)
var operations = _without(Object.values(Operations), Operations.operationDelete)
.map(function(o) { return o(selectedIDs, context); })
.filter(function(o) { return o.available(); });

Expand Down
6 changes: 2 additions & 4 deletions modules/osm/changeset.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import _compact from 'lodash-es/compact';
import _extend from 'lodash-es/extend';
import _filter from 'lodash-es/filter';
import _find from 'lodash-es/find';
import _map from 'lodash-es/map';
import _values from 'lodash-es/values';

import { osmEntity } from './entity';
import { geoExtent } from '../geo';
Expand Down Expand Up @@ -106,7 +104,7 @@ _extend(osmChangeset.prototype, {

while (processing.length > 0) {
var next = processing[0],
deps = _filter(_compact(next.member.map(resolve)), isNew);
deps = _filter(next.member.map(resolve).filter(Boolean), isNew);
if (deps.length === 0) {
sorted[next['@id']] = next;
processing.shift();
Expand All @@ -116,7 +114,7 @@ _extend(osmChangeset.prototype, {
}
}

changes.relation = _values(sorted);
changes.relation = Object.values(sorted);
return changes;
}

Expand Down
3 changes: 1 addition & 2 deletions modules/presets/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _find from 'lodash-es/find';
import _findIndex from 'lodash-es/findIndex';
import _some from 'lodash-es/some';
import _uniq from 'lodash-es/uniq';
import _values from 'lodash-es/values';
import _without from 'lodash-es/without';

import { utilEditDistance } from '../util/index';
Expand Down Expand Up @@ -108,7 +107,7 @@ export function presetCollection(collection) {
// matches value to preset.tags values
var leading_tag_values = searchable
.filter(function(a) {
return _some(_without(_values(a.tags || {}), '*'), leading);
return _some(_without(Object.values(a.tags || {}), '*'), leading);
});

var leading_suggestions = suggestions
Expand Down
6 changes: 2 additions & 4 deletions modules/renderer/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _compact from 'lodash-es/compact';
import _map from 'lodash-es/map';
import _throttle from 'lodash-es/throttle';
import _values from 'lodash-es/values';

import { set as d3_set } from 'd3-collection';
import { dispatch as d3_dispatch } from 'd3-dispatch';
Expand Down Expand Up @@ -207,7 +205,7 @@ export function rendererMap(context) {
}
}
});
var data = _values(selectedAndParents);
var data = Object.values(selectedAndParents);
var filter = function(d) { return d.id in selectedAndParents; };

data = context.features().filter(data, graph);
Expand Down Expand Up @@ -279,7 +277,7 @@ export function rendererMap(context) {

if (difference) {
var complete = difference.complete(map.extent());
data = _compact(_values(complete));
data = Object.values(complete).filter(Boolean);
filter = function(d) { return d.id in complete; };
features.clear(data);

Expand Down
3 changes: 1 addition & 2 deletions modules/services/nominatim.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _assign from 'lodash-es/assign';
import _forEach from 'lodash-es/forEach';

import { json as d3_json } from 'd3-request';
Expand Down Expand Up @@ -63,7 +62,7 @@ export default {
}

var extent = geoExtent(location).padByMeters(200);
nominatimCache.insert(_assign(extent.bbox(), {data: result}));
nominatimCache.insert(Object.assign(extent.bbox(), {data: result}));

callback(null, result);
});
Expand Down
3 changes: 1 addition & 2 deletions modules/svg/areas.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _map from 'lodash-es/map';
import _values from 'lodash-es/values';

import { bisector as d3_bisector } from 'd3-array';

Expand Down Expand Up @@ -214,7 +213,7 @@ export function svgAreas(projection, context) {
}
}

areas = _values(areas).filter(function hasPath(a) { return path(a.entity); });
areas = Object.values(areas).filter(function hasPath(a) { return path(a.entity); });
areas.sort(function areaSort(a, b) { return b.area - a.area; });
areas = _map(areas, 'entity');

Expand Down
4 changes: 1 addition & 3 deletions modules/svg/debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _values from 'lodash-es/values';

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

import { data, dataImperial, dataDriveLeft } from '../../data';
Expand Down Expand Up @@ -94,7 +92,7 @@ export function svgDebug(projection, context) {


var community = layer.selectAll('path.debug-community')
.data(showsCommunity ? _values(data.community.features) : []);
.data(showsCommunity ? Object.values(data.community.features) : []);

community.exit()
.remove();
Expand Down
19 changes: 4 additions & 15 deletions modules/svg/midpoints.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import _values from 'lodash-es/values';

import {
svgPointTransform,
svgTagClasses
} from './index';

import {
geoAngle,
geoLineIntersection,
geoVecInterp,
geoVecLength
} from '../geo';
import { svgPointTransform, svgTagClasses } from './index';
import { geoAngle, geoLineIntersection, geoVecInterp, geoVecLength } from '../geo';


export function svgMidpoints(projection, context) {
Expand Down Expand Up @@ -131,7 +120,7 @@ export function svgMidpoints(projection, context) {

var groups = drawLayer.selectAll('.midpoint')
.filter(midpointFilter)
.data(_values(midpoints), function(d) { return d.id; });
.data(Object.values(midpoints), function(d) { return d.id; });

groups.exit()
.remove();
Expand Down Expand Up @@ -170,7 +159,7 @@ export function svgMidpoints(projection, context) {

// Draw touch targets..
touchLayer
.call(drawTargets, graph, _values(midpoints), midpointFilter);
.call(drawTargets, graph, Object.values(midpoints), midpointFilter);
}

return drawMidpoints;
Expand Down
9 changes: 3 additions & 6 deletions modules/svg/vertices.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import _assign from 'lodash-es/assign';
import _values from 'lodash-es/values';

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

import { geoScaleToZoom } from '../geo';
Expand Down Expand Up @@ -368,7 +365,7 @@ export function svgVertices(projection, context) {
hovered: _currHover // hovered + siblings of hovered (render only in draw modes)
};

var all = _assign({}, (isMoving ? _currHover : {}), _currSelected, _currPersistent);
var all = Object.assign({}, (isMoving ? _currHover : {}), _currSelected, _currPersistent);

// Draw the vertices..
// The filter function controls the scope of what objects d3 will touch (exit/enter/update)
Expand Down Expand Up @@ -406,7 +403,7 @@ export function svgVertices(projection, context) {

// note that drawVertices will add `_currSelected` automatically if needed..
var filter = function(d) { return d.id in _prevSelected; };
drawVertices(selection, graph, _values(_prevSelected), filter, extent, false);
drawVertices(selection, graph, Object.values(_prevSelected), filter, extent, false);
};


Expand All @@ -429,7 +426,7 @@ export function svgVertices(projection, context) {

// note that drawVertices will add `_currHover` automatically if needed..
var filter = function(d) { return d.id in _prevHover; };
drawVertices(selection, graph, _values(_prevHover), filter, extent, false);
drawVertices(selection, graph, Object.values(_prevHover), filter, extent, false);
};

return drawVertices;
Expand Down
21 changes: 9 additions & 12 deletions modules/ui/feature_info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import _compact from 'lodash-es/compact';
import _map from 'lodash-es/map';

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

import { t } from '../util/locale';
Expand All @@ -10,15 +7,15 @@ import { tooltip } from '../util/tooltip';

export function uiFeatureInfo(context) {
function update(selection) {
var features = context.features(),
stats = features.stats(),
count = 0,
hiddenList = _compact(_map(features.hidden(), function(k) {
if (stats[k]) {
count += stats[k];
return String(stats[k]) + ' ' + t('feature.' + k + '.description');
}
}));
var features = context.features();
var stats = features.stats();
var count = 0;
var hiddenList = features.hidden().map(function(k) {
if (stats[k]) {
count += stats[k];
return String(stats[k]) + ' ' + t('feature.' + k + '.description');
}
}).filter(Boolean);

selection.html('');

Expand Down
Loading

0 comments on commit 11bfeaa

Please sign in to comment.