addTags() -> setTags() / removeTags() -> unsetTags()

This avoids overriding the `addTags` and `removeTags` objects with functions
This commit is contained in:
Bryan Housel
2018-11-21 12:59:43 -05:00
parent ed1ffed095
commit ea9e3008a5
4 changed files with 36 additions and 36 deletions

View File

@@ -1,11 +1,11 @@
export function actionChangePreset(entityId, oldPreset, newPreset) {
return function(graph) {
var entity = graph.entity(entityId),
geometry = entity.geometry(graph),
tags = entity.tags;
export function actionChangePreset(entityID, oldPreset, newPreset) {
return function action(graph) {
var entity = graph.entity(entityID);
var geometry = entity.geometry(graph);
var tags = entity.tags;
if (oldPreset) tags = oldPreset.removeTags(tags, geometry);
if (newPreset) tags = newPreset.applyTags(tags, geometry);
if (oldPreset) tags = oldPreset.unsetTags(tags, geometry);
if (newPreset) tags = newPreset.setTags(tags, geometry);
return graph.replace(entity.update({tags: tags}));
};

View File

@@ -1,5 +1,4 @@
import _clone from 'lodash-es/clone';
import _keys from 'lodash-es/keys';
import _omit from 'lodash-es/omit';
import { t } from '../util/locale';
@@ -94,9 +93,9 @@ export function presetPreset(id, preset, fields) {
};
var removeTags = preset.removeTags || preset.tags || {};
preset.removeTags = function(tags, geometry) {
tags = _omit(tags, _keys(removeTags));
preset.removeTags = preset.removeTags || preset.tags || {};
preset.unsetTags = function(tags, geometry) {
tags = _omit(tags, Object.keys(preset.removeTags));
for (var f in preset.fields) {
var field = preset.fields[f];
@@ -110,17 +109,18 @@ export function presetPreset(id, preset, fields) {
};
var applyTags = preset.addTags || preset.tags || {};
preset.applyTags = function(tags, geometry) {
preset.addTags = preset.addTags || preset.tags || {};
preset.setTags = function(tags, geometry) {
var addTags = preset.addTags;
var k;
tags = _clone(tags);
for (k in applyTags) {
if (applyTags[k] === '*') {
for (k in addTags) {
if (addTags[k] === '*') {
tags[k] = 'yes';
} else {
tags[k] = applyTags[k];
tags[k] = addTags[k];
}
}
@@ -128,12 +128,12 @@ export function presetPreset(id, preset, fields) {
// This is necessary if the geometry is already an area (e.g. user drew an area) AND any of:
// 1. chosen preset could be either an area or a line (`barrier=city_wall`)
// 2. chosen preset doesn't have a key in areaKeys (`railway=station`)
if (!applyTags.hasOwnProperty('area')) {
if (!addTags.hasOwnProperty('area')) {
delete tags.area;
if (geometry === 'area') {
var needsAreaTag = true;
if (preset.geometry.indexOf('line') === -1) {
for (k in applyTags) {
for (k in addTags) {
if (k in areaKeys) {
needsAreaTag = false;
break;

View File

@@ -85,11 +85,11 @@ export function uiFieldLocalized(field, context) {
.on('accept', function(d) {
var tags = _entity.tags;
var geometry = _entity.geometry(context.graph());
var removed = preset.removeTags(tags, geometry);
var removed = preset.unsetTags(tags, geometry);
for (var k in tags) {
tags[k] = removed[k]; // set removed tags to `undefined`
}
tags = d.suggestion.applyTags(tags, geometry);
tags = d.suggestion.setTags(tags, geometry);
dispatch.call('change', this, tags);
})
);

View File

@@ -69,84 +69,84 @@ describe('iD.presetPreset', function() {
});
});
describe('#applyTags', function() {
describe('#setTags', function() {
it('adds match tags', function() {
var preset = iD.presetPreset('test', {tags: {highway: 'residential'}});
expect(preset.applyTags({}, 'line')).to.eql({highway: 'residential'});
expect(preset.setTags({}, 'line')).to.eql({highway: 'residential'});
});
it('adds wildcard tags with value \'yes\'', function() {
var preset = iD.presetPreset('test', {tags: {building: '*'}});
expect(preset.applyTags({}, 'area')).to.eql({building: 'yes'});
expect(preset.setTags({}, 'area')).to.eql({building: 'yes'});
});
it('prefers to add tags of addTags property', function() {
var preset = iD.presetPreset('test', {tags: {building: '*'}, addTags: {building: 'ok'}});
expect(preset.applyTags({}, 'area')).to.eql({building: 'ok'});
expect(preset.setTags({}, 'area')).to.eql({building: 'ok'});
});
it('adds default tags of fields with matching geometry', function() {
var field = iD.presetField('field', {key: 'building', geometry: 'area', default: 'yes'}),
preset = iD.presetPreset('test', {fields: ['field']}, {field: field});
expect(preset.applyTags({}, 'area')).to.eql({area: 'yes', building: 'yes'});
expect(preset.setTags({}, 'area')).to.eql({area: 'yes', building: 'yes'});
});
it('adds no default tags of fields with non-matching geometry', function() {
var field = iD.presetField('field', {key: 'building', geometry: 'area', default: 'yes'}),
preset = iD.presetPreset('test', {fields: ['field']}, {field: field});
expect(preset.applyTags({}, 'point')).to.eql({});
expect(preset.setTags({}, 'point')).to.eql({});
});
describe('for a preset with no tag in areaKeys', function() {
var preset = iD.presetPreset('test', {geometry: ['line', 'area'], tags: {name: 'testname', highway: 'pedestrian'}});
it('doesn\'t add area=yes to non-areas', function() {
expect(preset.applyTags({}, 'line')).to.eql({name: 'testname', highway: 'pedestrian'});
expect(preset.setTags({}, 'line')).to.eql({name: 'testname', highway: 'pedestrian'});
});
it('adds area=yes to areas', function() {
expect(preset.applyTags({}, 'area')).to.eql({name: 'testname', highway: 'pedestrian', area: 'yes'});
expect(preset.setTags({}, 'area')).to.eql({name: 'testname', highway: 'pedestrian', area: 'yes'});
});
});
describe('for a preset with a tag in areaKeys', function() {
it('doesn\'t add area=yes automatically', function() {
var preset = iD.presetPreset('test', {geometry: ['area'], tags: {name: 'testname', natural: 'water'}});
expect(preset.applyTags({}, 'area')).to.eql({name: 'testname', natural: 'water'});
expect(preset.setTags({}, 'area')).to.eql({name: 'testname', natural: 'water'});
});
it('does add area=yes if asked to', function() {
var preset = iD.presetPreset('test', {geometry: ['area'], tags: {name: 'testname', area: 'yes'}});
expect(preset.applyTags({}, 'area')).to.eql({name: 'testname', area: 'yes'});
expect(preset.setTags({}, 'area')).to.eql({name: 'testname', area: 'yes'});
});
});
});
describe('#removeTags', function() {
describe('#unsetTags', function() {
it('removes tags that match preset tags', function() {
var preset = iD.presetPreset('test', {tags: {highway: 'residential'}});
expect(preset.removeTags({highway: 'residential'}, 'area')).to.eql({});
expect(preset.unsetTags({highway: 'residential'}, 'area')).to.eql({});
});
it('removes tags that match field default tags', function() {
var field = iD.presetField('field', {key: 'building', geometry: 'area', default: 'yes'}),
preset = iD.presetPreset('test', {fields: ['field']}, {field: field});
expect(preset.removeTags({building: 'yes'}, 'area')).to.eql({});
expect(preset.unsetTags({building: 'yes'}, 'area')).to.eql({});
});
it('removes area=yes', function() {
var preset = iD.presetPreset('test', {tags: {highway: 'pedestrian'}});
expect(preset.removeTags({highway: 'pedestrian', area: 'yes'}, 'area')).to.eql({});
expect(preset.unsetTags({highway: 'pedestrian', area: 'yes'}, 'area')).to.eql({});
});
it('preserves tags that do not match field default tags', function() {
var field = iD.presetField('field', {key: 'building', geometry: 'area', default: 'yes'}),
preset = iD.presetPreset('test', {fields: ['field']}, {field: field});
expect(preset.removeTags({building: 'yep'}, 'area')).to.eql({ building: 'yep'});
expect(preset.unsetTags({building: 'yep'}, 'area')).to.eql({ building: 'yep'});
});
it('preserves tags that are not listed in removeTags', function() {
var preset = iD.presetPreset('test', {tags: {a: 'b'}, removeTags: {}});
expect(preset.removeTags({a: 'b'}, 'area')).to.eql({a: 'b'});
expect(preset.unsetTags({a: 'b'}, 'area')).to.eql({a: 'b'});
});
});
});