From 13f065a5d13d1bc7d1644dd9e1120e5011c88755 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 3 Feb 2021 16:41:09 -0800 Subject: [PATCH] chore(tag_mapper): refactor tag mapping order This makes it easier to add custom logic by working through the tags in a specified order. --- stream/tag_mapper.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/stream/tag_mapper.js b/stream/tag_mapper.js index 33099690..f3d413bc 100644 --- a/stream/tag_mapper.js +++ b/stream/tag_mapper.js @@ -30,8 +30,27 @@ module.exports = function(){ return next( null, doc ); } - // Unfortunately we need to iterate over every tag, - // so we only do the iteration once to save CPU. + // handle the most likely source of name.default first + const trimmed_name = trim(tags.name); + if (trimmed_name) { + doc.setName('default', trimmed_name); + } + + // check the other tags that might go into name.default second + Object.entries(NAME_SCHEMA).forEach(([key, value]) => { + if (value === 'default' && key !== 'name') { + const trimmed_value = trim(tags[key]); + if (trimmed_value) { + if (!trimmed_name ) { + doc.setName('default', trim( tags[key])); + } else { + doc.setNameAlias('default', trim( tags[key])); + } + } + } + }); + + // iterate through all tags, catching any address/localized names _.each(tags, (value, key) => { // Map localized names which begin with 'name:' @@ -44,20 +63,6 @@ module.exports = function(){ } } - // Map name data from our name mapping schema - else if( _.has(NAME_SCHEMA, key) ){ - var val2 = trim( value ); - if( val2 ){ - if( key === NAME_SCHEMA._primary ){ - doc.setName( NAME_SCHEMA[key], val2 ); - } else if ( 'default' === NAME_SCHEMA[key] ) { - doc.setNameAlias( NAME_SCHEMA[key], val2 ); - } else { - doc.setName( NAME_SCHEMA[key], val2 ); - } - } - } - // Map address data from our address mapping schema else if( _.has(ADDRESS_SCHEMA, key) ){ var val3 = trim( value );