From 7b4a9a43b0cec4889bc12375a5f030a22885c735 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Thu, 2 May 2019 22:59:41 -0400 Subject: [PATCH] Change `utilTagDiff` to return an object with details --- modules/util/util.js | 16 ++++++++++++++-- modules/validations/outdated_tags.js | 4 ++-- modules/validations/private_data.js | 4 ++-- test/spec/util/util.js | 16 ++++++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/modules/util/util.js b/modules/util/util.js index ea8d477da..a5bfc2534 100644 --- a/modules/util/util.js +++ b/modules/util/util.js @@ -22,10 +22,22 @@ export function utilTagDiff(oldTags, newTags) { var newVal = newTags[k]; if (oldVal && (!newVal || newVal !== oldVal)) { - tagDiff.push('- ' + k + '=' + oldVal); + tagDiff.push({ + type: '-', + key: k, + oldVal: oldVal, + newVal: newVal, + display: '- ' + k + '=' + oldVal + }); } if (newVal && (!oldVal || newVal !== oldVal)) { - tagDiff.push('+ ' + k + '=' + newVal); + tagDiff.push({ + type: '+', + key: k, + oldVal: oldVal, + newVal: newVal, + display: '+ ' + k + '=' + newVal + }); } }); return tagDiff; diff --git a/modules/validations/outdated_tags.js b/modules/validations/outdated_tags.js index d73ed0b77..e3336e68d 100644 --- a/modules/validations/outdated_tags.js +++ b/modules/validations/outdated_tags.js @@ -99,10 +99,10 @@ export function validationOutdatedTags() { .attr('class', 'tagDiff-row') .append('td') .attr('class', function(d) { - var klass = d.charAt(0) === '+' ? 'add' : 'remove'; + var klass = d.type === '+' ? 'add' : 'remove'; return 'tagDiff-cell tagDiff-cell-' + klass; }) - .text(function(d) { return d; }); + .text(function(d) { return d.display; }); } } diff --git a/modules/validations/private_data.js b/modules/validations/private_data.js index 972e77dec..15c6ae5b7 100644 --- a/modules/validations/private_data.js +++ b/modules/validations/private_data.js @@ -107,10 +107,10 @@ export function validationPrivateData() { .attr('class', 'tagDiff-row') .append('td') .attr('class', function(d) { - var klass = d.charAt(0) === '+' ? 'add' : 'remove'; + var klass = d.type === '+' ? 'add' : 'remove'; return 'tagDiff-cell tagDiff-cell-' + klass; }) - .text(function(d) { return d; }); + .text(function(d) { return d.display; }); } }; diff --git a/test/spec/util/util.js b/test/spec/util/util.js index 3811273be..63b999955 100644 --- a/test/spec/util/util.js +++ b/test/spec/util/util.js @@ -58,10 +58,18 @@ describe('iD.util', function() { var newTags = { a: 'one', b: 'three', d: 'four' }; var diff = iD.utilTagDiff(oldTags, newTags); expect(diff).to.have.length(4); - expect(diff[0]).to.eql('- b=two'); // delete-modify - expect(diff[1]).to.eql('+ b=three'); // insert-modify - expect(diff[2]).to.eql('- c=three'); // delete - expect(diff[3]).to.eql('+ d=four'); // insert + expect(diff[0]).to.eql({ + type: '-', key: 'b', oldVal: 'two', newVal: 'three', display: '- b=two' // delete-modify + }); + expect(diff[1]).to.eql({ + type: '+', key: 'b', oldVal: 'two', newVal: 'three', display: '+ b=three' // insert-modify + }); + expect(diff[2]).to.eql({ + type: '-', key: 'c', oldVal: 'three', newVal: undefined, display: '- c=three' // delete + }); + expect(diff[3]).to.eql({ + type: '+', key: 'd', oldVal: undefined, newVal: 'four', display: '+ d=four' // insert + }); }); it('utilTagText', function() {