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
+51 -11
View File
@@ -330,12 +330,33 @@
} }
}; };
}; };
function filterObjectsByStringEntries(reference, objects) { function filterObjectsByStringEntries(reference, converter, objects) {
const matches = []; const matches = [];
object_loop: for (const object of objects) { object_loop: for (const object of objects) {
reference_entry_loop: for (const referenceEntry of Object.entries(reference)) { reference_entry_loop: for (const referenceEntry of Object.entries(
const [key, value] = referenceEntry; reference
if (object[key] != value) continue object_loop; )) {
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); matches.push(object);
} }
@@ -3528,13 +3549,32 @@
// src/index.tsx // src/index.tsx
console.log( console.log(
filterObjectsByStringEntries({ a: "hello", b: "test" }, [ filterObjectsByStringEntries(
{ a: "hello", b: "test" }, { a: "a", b: "b", c: "-c", d: "", e: "-" },
{ a: "hello", b: "test", c: "something" }, (a) => a.a,
{ a: "hello", b: "test 123" }, [
{ a: "hello blabla", b: "test" }, { a: { a: "a", b: "b" } },
{ a: "hello" } { 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 storageModel = new StorageModel();
var settingsModel = new SettingsModel(storageModel); var settingsModel = new SettingsModel(storageModel);
+34 -4
View File
@@ -41,11 +41,41 @@ export function filterObjectsByStringEntries<T>(
reference_entry_loop: for (const referenceEntry of Object.entries( reference_entry_loop: for (const referenceEntry of Object.entries(
reference reference
)) { )) {
const [key, value] = referenceEntry; const [referenceKey, referenceValue] = referenceEntry;
if (value == "") continue reference_entry_loop;
const comparableObject: ComparableObject = converter(object); 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); matches.push(object);
} }
+1
View File
@@ -16,6 +16,7 @@ import SettingsViewModel from "./ViewModel/Global/settingsViewModel";
import { StorageModal } from "./View/Modals/storageModal"; import { StorageModal } from "./View/Modals/storageModal";
import StorageModel from "./Model/Global/storageModel"; import StorageModel from "./Model/Global/storageModel";
import StorageViewModel from "./ViewModel/Global/storageViewModel"; import StorageViewModel from "./ViewModel/Global/storageViewModel";
import { filterObjectsByStringEntries } from "./Model/Utility/utility";
// models // models
const storageModel = new StorageModel(); const storageModel = new StorageModel();