diff --git a/modules/actions/add_member.js b/modules/actions/add_member.js index acf14f599..941f795ea 100644 --- a/modules/actions/add_member.js +++ b/modules/actions/add_member.js @@ -1,6 +1,6 @@ import _groupBy from 'lodash-es/groupBy'; -import _omit from 'lodash-es/omit'; +import { utilObjectOmit } from '../util'; import { osmJoinWays, osmWay } from '../osm'; @@ -135,7 +135,7 @@ export function actionAddMember(relationId, member, memberIndex, insertPair) { wayMembers.push(item.pair[0]); wayMembers.push(item.pair[1]); } else { - wayMembers.push(_omit(item, 'index')); + wayMembers.push(utilObjectOmit(item, ['index'])); } } @@ -144,7 +144,7 @@ export function actionAddMember(relationId, member, memberIndex, insertPair) { // see https://wiki.openstreetmap.org/wiki/Public_transport#Service_routes var newMembers = PTv2members.concat( (groups.node || []), wayMembers, (groups.relation || []) ); - return graph.replace(relation.update({members: newMembers})); + return graph.replace(relation.update({ members: newMembers })); // `moveMember()` changes the `members` array in place by splicing diff --git a/modules/actions/merge_polygon.js b/modules/actions/merge_polygon.js index 51186a5d2..0b2bc23f6 100644 --- a/modules/actions/merge_polygon.js +++ b/modules/actions/merge_polygon.js @@ -1,9 +1,8 @@ import _groupBy from 'lodash-es/groupBy'; -import _map from 'lodash-es/map'; -import _omit from 'lodash-es/omit'; import { geoPolygonContainsPolygon } from '../geo'; import { osmJoinWays, osmRelation } from '../osm'; +import { utilObjectOmit } from '../util'; export function actionMergePolygon(ids, newRelationId) { @@ -48,8 +47,8 @@ export function actionMergePolygon(ids, newRelationId) { return polygons.map(function(d, n) { if (i === n) return null; return geoPolygonContainsPolygon( - _map(d.nodes, 'loc'), - _map(w.nodes, 'loc') + d.nodes.map(function(n) { return n.loc; }), + w.nodes.map(function(n) { return n.loc; }) ); }); }); @@ -108,7 +107,7 @@ export function actionMergePolygon(ids, newRelationId) { return graph.replace(relation.update({ members: members, - tags: _omit(relation.tags, 'area') + tags: utilObjectOmit(relation.tags, ['area']) })); }; diff --git a/modules/behavior/hash.js b/modules/behavior/hash.js index 8a059a746..b6713328b 100644 --- a/modules/behavior/hash.js +++ b/modules/behavior/hash.js @@ -1,11 +1,10 @@ -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 { utilObjectOmit, utilQsString, utilStringQs } from '../util'; export function behaviorHash(context) { @@ -21,9 +20,9 @@ export function behaviorHash(context) { return true; // replace bogus hash } else if (s !== formatter(map).slice(1)) { // hash has changed - var mode = context.mode(), - dist = geoSphericalDistance(map.center(), [args[2], args[1]]), - maxdist = 500; + var mode = context.mode(); + var dist = geoSphericalDistance(map.center(), [args[2], args[1]]); + var maxdist = 500; // Don't allow the hash location to change too much while drawing // This can happen if the user accidently hit the back button. #3996 @@ -40,7 +39,7 @@ export function behaviorHash(context) { var center = map.center(); var zoom = map.zoom(); var precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)); - var q = _omit(utilStringQs(window.location.hash.substring(1)), + var q = utilObjectOmit(utilStringQs(window.location.hash.substring(1)), ['comment', 'source', 'hashtags', 'walkthrough'] ); var newParams = {}; diff --git a/modules/core/history.js b/modules/core/history.js index 0f330b2a7..84735e774 100644 --- a/modules/core/history.js +++ b/modules/core/history.js @@ -7,7 +7,6 @@ import _isFunction from 'lodash-es/isFunction'; import _isEmpty from 'lodash-es/isEmpty'; import _forEach from 'lodash-es/forEach'; import _map from 'lodash-es/map'; -import _omit from 'lodash-es/omit'; import _uniq from 'lodash-es/uniq'; import { dispatch as d3_dispatch } from 'd3-dispatch'; @@ -19,7 +18,7 @@ import { coreGraph } from './graph'; import { coreTree } from './tree'; import { osmEntity } from '../osm/entity'; import { uiLoading } from '../ui'; -import { utilRebind, utilSessionMutex } from '../util'; +import { utilObjectOmit, utilRebind, utilSessionMutex } from '../util'; export function coreHistory(context) { @@ -393,7 +392,7 @@ export function coreHistory(context) { function customizer(src) { - var copy = _omit(_cloneDeep(src), ['type', 'user', 'v', 'version', 'visible']); + var copy = utilObjectOmit(_cloneDeep(src), ['type', 'user', 'v', 'version', 'visible']); if (_isEmpty(copy.tags)) { delete copy.tags; } diff --git a/modules/presets/preset.js b/modules/presets/preset.js index 44ed18cd5..c6ba50744 100644 --- a/modules/presets/preset.js +++ b/modules/presets/preset.js @@ -1,8 +1,8 @@ -import _omit from 'lodash-es/omit'; import _uniq from 'lodash-es/uniq'; import { t } from '../util/locale'; import { areaKeys } from '../core/context'; +import { utilObjectOmit } from '../util'; export function presetPreset(id, preset, fields, visible, rawPresets) { @@ -183,7 +183,7 @@ export function presetPreset(id, preset, fields, visible, rawPresets) { } // Lookup documentation on OSM Wikibase... - var key = reference.key || Object.keys(_omit(preset.tags, 'name'))[0]; + var key = reference.key || Object.keys(utilObjectOmit(preset.tags, 'name'))[0]; var value = reference.value || preset.tags[key]; if (geometry === 'relation' && key === 'type') { @@ -205,7 +205,7 @@ export function presetPreset(id, preset, fields, visible, rawPresets) { preset.removeTags = preset.removeTags || preset.tags || {}; preset.unsetTags = function(tags, geometry) { - tags = _omit(tags, Object.keys(preset.removeTags)); + tags = utilObjectOmit(tags, Object.keys(preset.removeTags)); for (var f in preset.fields) { var field = preset.fields[f]; diff --git a/modules/services/taginfo.js b/modules/services/taginfo.js index 00cc6efee..ee26c0c23 100644 --- a/modules/services/taginfo.js +++ b/modules/services/taginfo.js @@ -1,10 +1,9 @@ import _debounce from 'lodash-es/debounce'; import _forEach from 'lodash-es/forEach'; -import _omit from 'lodash-es/omit'; import { json as d3_json } from 'd3-request'; -import { utilQsString } from '../util'; +import { utilObjectOmit, utilQsString } from '../util'; import { currentLocale } from '../util/locale'; @@ -65,7 +64,7 @@ function setSortMembers(params) { function clean(params) { - return _omit(params, ['geometry', 'debounce']); + return utilObjectOmit(params, ['geometry', 'debounce']); } diff --git a/modules/util/index.js b/modules/util/index.js index 181dc65ef..80bd8a252 100644 --- a/modules/util/index.js +++ b/modules/util/index.js @@ -19,6 +19,7 @@ export { utilHighlightEntities } from './util'; export { utilIdleWorker } from './idle_worker'; export { utilKeybinding } from './keybinding'; export { utilNoAuto } from './util'; +export { utilObjectOmit } from './object'; export { utilPrefixCSSProperty } from './util'; export { utilPrefixDOMProperty } from './util'; export { utilPreset } from './util'; diff --git a/modules/util/object.js b/modules/util/object.js new file mode 100644 index 000000000..fc905e81d --- /dev/null +++ b/modules/util/object.js @@ -0,0 +1,9 @@ + +export function utilObjectOmit(obj, omitKeys) { + return Object.keys(obj).reduce(function(result, key) { + if (omitKeys.indexOf(key) === -1) { + result[key] = obj[key]; // keep + } + return result; + }, {}); +} diff --git a/modules/util/util.js b/modules/util/util.js index b02b2ee08..6ab48cbbf 100644 --- a/modules/util/util.js +++ b/modules/util/util.js @@ -1,5 +1,3 @@ -import _map from 'lodash-es/map'; - import { t, textDirection } from './locale'; import { utilDetect } from './detect'; import { remove as removeDiacritics } from 'diacritics'; @@ -7,7 +5,9 @@ import { fixRTLTextForSvg, rtlRegex } from './svg_paths_rtl_fix'; export function utilTagText(entity) { - return _map(entity.tags, function(v, k) { + var obj = (entity && entity.tags) || {}; + return Object.keys(obj).map(function(k) { + var v = obj[k]; return k + '=' + v; }).join(', '); } diff --git a/test/spec/util/object.js b/test/spec/util/object.js new file mode 100644 index 000000000..2d577a8a6 --- /dev/null +++ b/test/spec/util/object.js @@ -0,0 +1,9 @@ +describe('iD.utilObjectOmit', function() { + it('omits keys', function() { + var t = { a: 1, b: 2 }; + expect(iD.utilObjectOmit(t, [])).to.eql({ a: 1, b: 2 }); + expect(iD.utilObjectOmit(t, ['a'])).to.eql({ b: 2 }); + expect(iD.utilObjectOmit(t, ['a', 'b'])).to.eql({}); + }); +}); +