From 55f5a96a42d4625cd476a2640f9b3835485a05df Mon Sep 17 00:00:00 2001 From: burrscurr Date: Wed, 5 Feb 2025 12:35:28 +0100 Subject: [PATCH] handle key values that should not be reversed together --- modules/actions/reverse.js | 58 ++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/modules/actions/reverse.js b/modules/actions/reverse.js index 3fc799101..2ade4b61e 100644 --- a/modules/actions/reverse.js +++ b/modules/actions/reverse.js @@ -18,10 +18,8 @@ References: http://wiki.openstreetmap.org/wiki/Key:traffic_sign#On_a_way_or_area */ export function actionReverse(entityID, options) { - var ignoreKey = /^.*(_|:)?(description|name|note|website|ref|source|comment|watch|attribution)(_|:)?/; var numeric = /^([+\-]?)(?=[\d.])/; var directionKey = /direction$/; - var turn_lanes = /^turn:lanes:?/; const keysToKeepUnchanged = [ // https://github.com/openstreetmap/iD/issues/10736 /^red_turn:(right|left)/ @@ -46,13 +44,28 @@ export function actionReverse(entityID, options) { forwards: 'backward', backwards: 'forward', }; - // tags whose values should not be reversed when certain other tags are also present - // https://github.com/openstreetmap/iD/issues/10128 - const valueReplacementsExceptions = { - 'side': [ - {highway: 'cyclist_waiting_aid'} + // For some keys, values like left/right/... don't refer to way direction and thus + // should not be reversed. + // + // If a key matches the key regex and any of the provided context tag sets, it will + // not be reversed. + const keyValuesThatShouldNotBeReversed = [ + [ + /^.*(_|:)?(description|name|note|website|ref|source|comment|watch|attribution)(_|:)?/, + [{}] + ], + // https://github.com/openstreetmap/iD/issues/10128 + [ + /^side$/, + [{highway: 'cyclist_waiting_aid'}] + ], + // Turn lanes are left/right to key (not way) direction - #5674 + [ + /^turn:lanes:?/, + [{}] ] - }; + + ]; var roleReplacements = { forward: 'backward', backward: 'forward', @@ -100,13 +113,19 @@ export function actionReverse(entityID, options) { function reverseValue(key, value, includeAbsolute, allTags) { - if (ignoreKey.test(key)) return value; + for (let i = 0; i < keyValuesThatShouldNotBeReversed.length; i++) { + const keyRegex = keyValuesThatShouldNotBeReversed[i][0]; + const tagContexts = keyValuesThatShouldNotBeReversed[i][1]; + if (keyRegex.test(key) && tagContexts.some(expectedTags => + Object.entries(expectedTags).every(([k, v]) => { + return allTags[k] && (v === '*' || allTags[k] === v); + }) + )) { + return value; + } + } - // Turn lanes are left/right to key (not way) direction - #5674 - if (turn_lanes.test(key)) { - return value; - - } else if (key === 'incline' && numeric.test(value)) { + if (key === 'incline' && numeric.test(value)) { return value.replace(numeric, function(_, sign) { return sign === '-' ? '' : '-'; }); } else if (options && options.reverseOneway && key === 'oneway') { @@ -129,17 +148,6 @@ export function actionReverse(entityID, options) { } }).join(';'); } - - if (valueReplacementsExceptions[key] && valueReplacementsExceptions[key].some(exceptionTags => - Object.keys(exceptionTags).every(k => { - const v = exceptionTags[k]; - return allTags[k] && (v === '*' || allTags[k] === v); - }) - )) { - // don't reverse, for example, side=left/right on highway=cyclist_waiting_aid features - // see https://github.com/openstreetmap/iD/issues/10128 - return value; - } return valueReplacements[value] || value; }