From 9c1b8905dc27cc8775e14e8881b86f2d95ced4d5 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Fri, 19 Feb 2021 11:11:07 -0500 Subject: [PATCH] Return early --- .../saved_objects/service/lib/filter_utils.ts | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/core/server/saved_objects/service/lib/filter_utils.ts b/src/core/server/saved_objects/service/lib/filter_utils.ts index f48cb339caced..688b7ad96e8ed 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.ts @@ -206,39 +206,34 @@ export const hasFilterKeyError = ( }; export const fieldDefined = (indexMappings: IndexMapping, key: string): boolean => { - let isFieldDefined = false; - const mappingKey = 'properties.' + key.split('.').join('.properties.'); - isFieldDefined = get(indexMappings, mappingKey) != null; + if (get(indexMappings, mappingKey) != null) { + return true; + } - // If the `mappingKey` does not match a valid path, before returning null, + // If the `mappingKey` does not match a valid path, before returning false, // we want to check and see if the intended path was for a multi-field // such as `x.attributes.field.text` where `field` is mapped to both text // and keyword - if (!isFieldDefined) { - const propertiesAttribute = 'properties'; - const indexOfLastProperties = mappingKey.lastIndexOf(propertiesAttribute); - const fieldMapping = mappingKey.substr(0, indexOfLastProperties); - const fieldType = mappingKey.substr( - mappingKey.lastIndexOf(propertiesAttribute) + `${propertiesAttribute}.`.length - ); - const mapping = `${fieldMapping}fields.${fieldType}`; - - isFieldDefined = get(indexMappings, mapping) != null; + const propertiesAttribute = 'properties'; + const indexOfLastProperties = mappingKey.lastIndexOf(propertiesAttribute); + const fieldMapping = mappingKey.substr(0, indexOfLastProperties); + const fieldType = mappingKey.substr( + mappingKey.lastIndexOf(propertiesAttribute) + `${propertiesAttribute}.`.length + ); + const mapping = `${fieldMapping}fields.${fieldType}`; + if (get(indexMappings, mapping) != null) { + return true; } // If the path is for a flattned type field, we'll assume the mappings are defined. - if (!isFieldDefined) { - const keys = key.split('.'); - for (let i = 0; i < keys.length; i++) { - const path = `properties.${keys.slice(0, i + 1).join('.properties.')}`; - const mapping = get(indexMappings, path); - if (mapping?.type === 'flattened') { - isFieldDefined = true; - break; - } + const keys = key.split('.'); + for (let i = 0; i < keys.length; i++) { + const path = `properties.${keys.slice(0, i + 1).join('.properties.')}`; + if (get(indexMappings, path)?.type === 'flattened') { + return true; } } - return isFieldDefined; + return false; };