add exclusion filters to filterObjectsByStringEntries
This commit is contained in:
Vendored
+51
-11
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user