add collectObjectValuesForKey utility function

This commit is contained in:
marlon-erler
2024-07-25 00:26:40 +02:00
parent e81cbf93e1
commit 389aaff52b
2 changed files with 45 additions and 68 deletions
+14 -56
View File
@@ -330,37 +330,16 @@
}
};
};
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 [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);
function collectObjectValuesForKey(key, converter, objects) {
const values = /* @__PURE__ */ new Set();
for (const object of objects) {
const stringEntryObject = converter(object);
const stringEntryObjectValue = stringEntryObject[key];
if (stringEntryObjectValue == void 0 || stringEntryObjectValue == "")
continue;
values.add(stringEntryObjectValue);
}
return matches;
return [...values.values()];
}
var IndexManager = class {
itemToString;
@@ -3549,32 +3528,11 @@
// src/index.tsx
console.log(
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" } }
]
)
collectObjectValuesForKey("test", (x) => x, [
{ test: "hello" },
{ test: "123" },
{ test: "hello" }
])
);
var storageModel = new StorageModel();
var settingsModel = new SettingsModel(storageModel);
+31 -12
View File
@@ -28,13 +28,13 @@ export class HandlerManager<T> {
};
}
// filters
export type ComparableObject = { [key: string]: string };
// objects
export type StringEntryObject = { [key: string]: string };
export function filterObjectsByStringEntries<T>(
reference: ComparableObject,
converter: (T) => ComparableObject,
reference: StringEntryObject,
converter: (T) => StringEntryObject,
objects: T[]
): any[] {
): T[] {
const matches: any[] = [];
object_loop: for (const object of objects) {
@@ -42,29 +42,29 @@ export function filterObjectsByStringEntries<T>(
reference
)) {
const [referenceKey, referenceValue] = referenceEntry;
const comparableObject: ComparableObject = converter(object);
const comparableObjectValue: string = comparableObject[referenceKey];
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 == "" &&
comparableObjectValue != undefined &&
comparableObjectValue != ""
stringEntryObjectValue != undefined &&
stringEntryObjectValue != ""
) {
continue object_loop;
}
// property may not match
if (comparableObjectValue == strippedReferenceValue) {
if (stringEntryObjectValue == strippedReferenceValue) {
continue object_loop;
}
} else {
// property must exist but be anything
if (
referenceValue == "" &&
(comparableObjectValue == undefined || comparableObjectValue == "")
(stringEntryObjectValue == undefined || stringEntryObjectValue == "")
) {
continue object_loop;
} else if (referenceValue == "") {
@@ -72,7 +72,7 @@ export function filterObjectsByStringEntries<T>(
}
// property must match
if (comparableObjectValue != referenceValue) {
if (stringEntryObjectValue != referenceValue) {
continue object_loop;
}
}
@@ -83,6 +83,25 @@ export function filterObjectsByStringEntries<T>(
return matches;
}
export function collectObjectValuesForKey<T>(
key: string,
converter: (object: T) => StringEntryObject,
objects: T[]
): string[] {
const values: Set<string> = new Set();
for (const object of objects) {
const stringEntryObject: StringEntryObject = converter(object);
const stringEntryObjectValue: string = stringEntryObject[key];
if (stringEntryObjectValue == undefined || stringEntryObjectValue == "")
continue;
values.add(stringEntryObjectValue);
}
return [...values.values()];
}
// sorting
export class IndexManager<T> {
private itemToString: (item: T) => string;