From ce5efa344cecb84280a764b0d0637909cd95416b Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Wed, 24 Jul 2024 23:44:34 +0200 Subject: [PATCH] add exclusion filters to filterObjectsByStringEntries --- dist/index.js | 62 +++++++++++++++++++++++++++++------- src/Model/Utility/utility.ts | 40 ++++++++++++++++++++--- src/index.tsx | 1 + 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/dist/index.js b/dist/index.js index e9961965..3ef3630c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -330,12 +330,33 @@ } }; }; - function filterObjectsByStringEntries(reference, objects) { + function filterObjectsByStringEntries(reference, converter, objects) { const matches = []; object_loop: for (const object of objects) { - reference_entry_loop: for (const referenceEntry of Object.entries(reference)) { - const [key, value] = referenceEntry; - if (object[key] != value) continue object_loop; + reference_entry_loop: for (const referenceEntry of Object.entries( + reference + )) { + const [referenceKey, referenceValue] = referenceEntry; + const comparableObject = converter(object); + const comparableObjectValue = comparableObject[referenceKey]; + if (referenceValue[0] == "-") { + const strippedReferenceValue = referenceValue.substring(1); + if (strippedReferenceValue == "" && comparableObjectValue != void 0 && comparableObjectValue != "") { + continue object_loop; + } + if (comparableObjectValue == strippedReferenceValue) { + continue object_loop; + } + } else { + if (referenceValue == "" && (comparableObjectValue == void 0 || comparableObjectValue == "")) { + continue object_loop; + } else if (referenceValue == "") { + continue reference_entry_loop; + } + if (comparableObjectValue != referenceValue) { + continue object_loop; + } + } } matches.push(object); } @@ -3528,13 +3549,32 @@ // src/index.tsx console.log( - filterObjectsByStringEntries({ a: "hello", b: "test" }, [ - { a: "hello", b: "test" }, - { a: "hello", b: "test", c: "something" }, - { a: "hello", b: "test 123" }, - { a: "hello blabla", b: "test" }, - { a: "hello" } - ]) + filterObjectsByStringEntries( + { a: "a", b: "b", c: "-c", d: "", e: "-" }, + (a) => a.a, + [ + { a: { a: "a", b: "b" } }, + { a: { a: "a", b: "b1" } }, + { a: { a: "a", b: "b", c: "c" } }, + { a: { a: "a", b: "b", c: "c1" } }, + { a: { a: "a", b: "b", d: "" } }, + { a: { a: "a", b: "b1", d: "" } }, + { a: { a: "a", b: "b", c: "c", d: "" } }, + { a: { a: "a", b: "b", c: "c1", d: "" } }, + { a: { a: "a", b: "b", d: "d" } }, + { a: { a: "a", b: "b1", d: "d" } }, + { a: { a: "a", b: "b", c: "c", d: "d" } }, + { a: { a: "a", b: "b", c: "c1", d: "d" } }, + { a: { a: "a", b: "b", d: "d", e: "" } }, + { a: { a: "a", b: "b1", d: "d", e: "" } }, + { a: { a: "a", b: "b", c: "c", d: "d", e: "" } }, + { a: { a: "a", b: "b", c: "c1", d: "d", e: "" } }, + { a: { a: "a", b: "b", d: "d", e: "e" } }, + { a: { a: "a", b: "b1", d: "d", e: "e" } }, + { a: { a: "a", b: "b", c: "c", d: "d", e: "e" } }, + { a: { a: "a", b: "b", c: "c1", d: "d", e: "e" } } + ] + ) ); var storageModel = new StorageModel(); var settingsModel = new SettingsModel(storageModel); diff --git a/src/Model/Utility/utility.ts b/src/Model/Utility/utility.ts index 046d0652..ed3e4a63 100644 --- a/src/Model/Utility/utility.ts +++ b/src/Model/Utility/utility.ts @@ -29,7 +29,7 @@ export class HandlerManager { } // filters -export type ComparableObject = {[key: string]: string}; +export type ComparableObject = { [key: string]: string }; export function filterObjectsByStringEntries( reference: ComparableObject, converter: (T) => ComparableObject, @@ -41,11 +41,41 @@ export function filterObjectsByStringEntries( 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); } diff --git a/src/index.tsx b/src/index.tsx index 4b4e2b42..8a28b7fa 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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();