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) { function collectObjectValuesForKey(key, converter, objects) {
const matches = []; const values = /* @__PURE__ */ new Set();
object_loop: for (const object of objects) { for (const object of objects) {
reference_entry_loop: for (const referenceEntry of Object.entries( const stringEntryObject = converter(object);
reference const stringEntryObjectValue = stringEntryObject[key];
)) { if (stringEntryObjectValue == void 0 || stringEntryObjectValue == "")
const [referenceKey, referenceValue] = referenceEntry; continue;
const comparableObject = converter(object); values.add(stringEntryObjectValue);
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);
} }
return matches; return [...values.values()];
} }
var IndexManager = class { var IndexManager = class {
itemToString; itemToString;
@@ -3549,32 +3528,11 @@
// src/index.tsx // src/index.tsx
console.log( console.log(
filterObjectsByStringEntries( collectObjectValuesForKey("test", (x) => x, [
{ a: "a", b: "b", c: "-c", d: "", e: "-" }, { test: "hello" },
(a) => a.a, { test: "123" },
[ { test: "hello" }
{ 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 storageModel = new StorageModel();
var settingsModel = new SettingsModel(storageModel); var settingsModel = new SettingsModel(storageModel);
+31 -12
View File
@@ -28,13 +28,13 @@ export class HandlerManager<T> {
}; };
} }
// filters // objects
export type ComparableObject = { [key: string]: string }; export type StringEntryObject = { [key: string]: string };
export function filterObjectsByStringEntries<T>( export function filterObjectsByStringEntries<T>(
reference: ComparableObject, reference: StringEntryObject,
converter: (T) => ComparableObject, converter: (T) => StringEntryObject,
objects: T[] objects: T[]
): any[] { ): T[] {
const matches: any[] = []; const matches: any[] = [];
object_loop: for (const object of objects) { object_loop: for (const object of objects) {
@@ -42,29 +42,29 @@ export function filterObjectsByStringEntries<T>(
reference reference
)) { )) {
const [referenceKey, referenceValue] = referenceEntry; const [referenceKey, referenceValue] = referenceEntry;
const comparableObject: ComparableObject = converter(object); const stringEntryObject: StringEntryObject = converter(object);
const comparableObjectValue: string = comparableObject[referenceKey]; const stringEntryObjectValue: string = stringEntryObject[referenceKey];
if (referenceValue[0] == "-") { if (referenceValue[0] == "-") {
const strippedReferenceValue: string = referenceValue.substring(1); const strippedReferenceValue: string = referenceValue.substring(1);
// property may not exist // property may not exist
if ( if (
strippedReferenceValue == "" && strippedReferenceValue == "" &&
comparableObjectValue != undefined && stringEntryObjectValue != undefined &&
comparableObjectValue != "" stringEntryObjectValue != ""
) { ) {
continue object_loop; continue object_loop;
} }
// property may not match // property may not match
if (comparableObjectValue == strippedReferenceValue) { if (stringEntryObjectValue == strippedReferenceValue) {
continue object_loop; continue object_loop;
} }
} else { } else {
// property must exist but be anything // property must exist but be anything
if ( if (
referenceValue == "" && referenceValue == "" &&
(comparableObjectValue == undefined || comparableObjectValue == "") (stringEntryObjectValue == undefined || stringEntryObjectValue == "")
) { ) {
continue object_loop; continue object_loop;
} else if (referenceValue == "") { } else if (referenceValue == "") {
@@ -72,7 +72,7 @@ export function filterObjectsByStringEntries<T>(
} }
// property must match // property must match
if (comparableObjectValue != referenceValue) { if (stringEntryObjectValue != referenceValue) {
continue object_loop; continue object_loop;
} }
} }
@@ -83,6 +83,25 @@ export function filterObjectsByStringEntries<T>(
return matches; 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 // sorting
export class IndexManager<T> { export class IndexManager<T> {
private itemToString: (item: T) => string; private itemToString: (item: T) => string;