add exclusion filters to filterObjectsByStringEntries

This commit is contained in:
marlon-erler
2024-07-24 23:44:34 +02:00
parent 948b2e7f16
commit ce5efa344c
3 changed files with 87 additions and 16 deletions
+35 -5
View File
@@ -29,7 +29,7 @@ export class HandlerManager<T> {
}
// filters
export type ComparableObject = {[key: string]: string};
export type ComparableObject = { [key: string]: string };
export function filterObjectsByStringEntries<T>(
reference: ComparableObject,
converter: (T) => ComparableObject,
@@ -41,11 +41,41 @@ export function filterObjectsByStringEntries<T>(
reference_entry_loop: for (const referenceEntry of Object.entries(
reference
)) {
const [key, value] = referenceEntry;
if (value == "") continue reference_entry_loop;
const [referenceKey, referenceValue] = referenceEntry;
const comparableObject: ComparableObject = converter(object);
if (comparableObject[key] != value) continue object_loop;
const comparableObjectValue: string = comparableObject[referenceKey];
if (referenceValue[0] == "-") {
const strippedReferenceValue: string = referenceValue.substring(1);
// property may not exist
if (
strippedReferenceValue == "" &&
comparableObjectValue != undefined &&
comparableObjectValue != ""
) {
continue object_loop;
}
// property may not match
if (comparableObjectValue == strippedReferenceValue) {
continue object_loop;
}
} else {
// property must exist but be anything
if (
referenceValue == "" &&
(comparableObjectValue == undefined || comparableObjectValue == "")
) {
continue object_loop;
} else if (referenceValue == "") {
continue reference_entry_loop;
}
// property must match
if (comparableObjectValue != referenceValue) {
continue object_loop;
}
}
}
matches.push(object);
}
+1
View File
@@ -16,6 +16,7 @@ import SettingsViewModel from "./ViewModel/Global/settingsViewModel";
import { StorageModal } from "./View/Modals/storageModal";
import StorageModel from "./Model/Global/storageModel";
import StorageViewModel from "./ViewModel/Global/storageViewModel";
import { filterObjectsByStringEntries } from "./Model/Utility/utility";
// models
const storageModel = new StorageModel();