From cb9a79a10dbb88361fc78145370c097b8f65c685 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Thu, 25 Jul 2024 01:20:48 +0200 Subject: [PATCH] separate checkDoesObjectMatchReference from StringEntryObject --- src/Model/Utility/utility.ts | 92 ++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/src/Model/Utility/utility.ts b/src/Model/Utility/utility.ts index 7f3c6267..5b35d4ec 100644 --- a/src/Model/Utility/utility.ts +++ b/src/Model/Utility/utility.ts @@ -35,54 +35,64 @@ export function filterObjectsByStringEntries( converter: (T) => StringEntryObject, objects: T[] ): Set { - const matches: Set = new Set() + const matches: Set = new Set(); object_loop: for (const object of objects) { - reference_entry_loop: for (const referenceEntry of Object.entries( - reference - )) { - const [referenceKey, referenceValue] = referenceEntry; - const stringEntryObject: StringEntryObject = converter(object); - const stringEntryObjectValue: string = stringEntryObject[referenceKey]; - - if (referenceValue[0] == "-") { - const strippedReferenceValue: string = referenceValue.substring(1); - // property may not exist - if ( - strippedReferenceValue == "" && - stringEntryObjectValue != undefined && - stringEntryObjectValue != "" - ) { - continue object_loop; - } - - // property may not match - if (stringEntryObjectValue == strippedReferenceValue) { - continue object_loop; - } - } else { - // property must exist but be anything - if ( - referenceValue == "" && - (stringEntryObjectValue == undefined || stringEntryObjectValue == "") - ) { - continue object_loop; - } else if (referenceValue == "") { - continue reference_entry_loop; - } - - // property must match - if (stringEntryObjectValue != referenceValue) { - continue object_loop; - } - } - } - matches.add(object); + const doesMatch: boolean = checkDoesObjectMatchReference( + reference, + converter(object) + ); + if (doesMatch) matches.add(object); } return matches; } +export function checkDoesObjectMatchReference( + reference: StringEntryObject, + stringEntryObject: StringEntryObject +): boolean { + reference_entry_loop: for (const referenceEntry of Object.entries( + reference + )) { + const [referenceKey, referenceValue] = referenceEntry; + const stringEntryObjectValue: string = stringEntryObject[referenceKey]; + + if (referenceValue[0] == "-") { + const strippedReferenceValue: string = referenceValue.substring(1); + // property may not exist + if ( + strippedReferenceValue == "" && + stringEntryObjectValue != undefined && + stringEntryObjectValue != "" + ) { + return false; + } + + // property may not match + if (stringEntryObjectValue == strippedReferenceValue) { + return false; + } + } else { + // property must exist but be anything + if ( + referenceValue == "" && + (stringEntryObjectValue == undefined || stringEntryObjectValue == "") + ) { + return false; + } else if (referenceValue == "") { + continue reference_entry_loop; + } + + // property must match + if (stringEntryObjectValue != referenceValue) { + return false; + } + } + } + return true; +} + export function collectObjectValuesForKey( key: string, converter: (object: T) => StringEntryObject,