diff --git a/modules/util/keepRight/keepRight_error.js b/modules/util/keepRight/keepRight_error.js index 0c8a92151..e2c3a23f3 100644 --- a/modules/util/keepRight/keepRight_error.js +++ b/modules/util/keepRight/keepRight_error.js @@ -51,13 +51,8 @@ var keepRightSchemaFromWeb = { }; export function parseErrorDescriptions(entity) { - if (!(entity instanceof krError)) return; - - // find the matching template from the error schema - var errorType = '_' + entity.error_type; - var matchingTemplate = errorTypes.errors[errorType] || errorTypes.warnings[errorType]; - if (!matchingTemplate) return; - + var parsedDetails = {}; + var html_re = new RegExp(/<\/[a-z][\s\S]*>/); var commonEntities = [ 'node', 'way', @@ -68,9 +63,15 @@ export function parseErrorDescriptions(entity) { 'riverbank' ]; // TODO: expand this list, or implement a different translation function + var errorType; + var errorTemplate; + var errorDescription; + var errorRegex; + var errorMatch; + function fillPlaceholder(d) { return '' + d + ''; } - // arbitrary list of way IDs and their layer value in form: #ID(layer),#ID(layer),#ID(layer)... + // arbitrary list of form: #ID(layer),#ID(layer),#ID(layer)... function parseError231(list) { var newList = []; var items = list.split(','); @@ -89,41 +90,45 @@ export function parseErrorDescriptions(entity) { // layer has trailing ) layer = item[1].slice(0,-1); + // TODO: translation newList.push(id + ' (layer: ' + layer + ')'); }); return newList.join(', '); } - // regex pattern should capture groups to extract appropriate details - var errorDescription = entity.description; - var errorRe = new RegExp(matchingTemplate.description); + if (!(entity instanceof krError)) return; - var errorMatch = errorRe.exec(errorDescription); + // find the matching template from the error schema + errorType = '_' + entity.error_type; + errorTemplate = errorTypes.errors[errorType] || errorTypes.warnings[errorType]; + if (!errorTemplate) return; + // regex pattern should match description with variable details captured as groups + errorDescription = entity.description; + errorRegex = new RegExp(errorTemplate.description); + errorMatch = errorRegex.exec(errorDescription); if (!errorMatch) { // TODO: Remove, for regex dev testing - console.log('Unmatched:', errorType, errorDescription, errorRe); + console.log('Unmatched:', errorType, errorDescription, errorRegex); return; } - var parsedDetails = {}; - var html_re = new RegExp(/<\/[a-z][\s\S]*>/); + errorMatch.forEach(function(group, index) { + var idType; - // index 0 is the whole match, groups start from 1 - for (var i = 1; i < errorMatch.length; i++) { - var group = errorMatch[i]; - - // Clean and link IDs if present in the error - if ('IDs' in matchingTemplate && matchingTemplate.IDs[i-1]) { - var prefix = matchingTemplate.IDs[i-1]; + // index 0 is the whole match, skip it + if (!index) return; + // Clean and link IDs if present in the group + idType = 'IDs' in errorTemplate ? errorTemplate.IDs[index-1] : ''; + if (idType) { // some errors have more complex ID lists/variance - if (prefix === '231') { + if (idType === '231') { group = parseError231(group); - } else if (['n','w','r'].includes(prefix)) { - // wrap with linking span if simple case - group = fillPlaceholder(prefix + group); + } else if (['n','w','r'].includes(idType)) { + // simple case just needs a linking span + group = fillPlaceholder(idType + group); } } else if (html_re.test(group)) { // escape any html @@ -135,8 +140,8 @@ export function parseErrorDescriptions(entity) { group = t('QA.keepRight.entities.' + group); } - parsedDetails['var' + i] = group; - } + parsedDetails['var' + index] = group; + }); return parsedDetails; }