From cde634fbf4382198f72331ad5a652bd32f1a10a3 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Fri, 24 May 2024 16:31:27 -0400 Subject: [PATCH] Add Helper For Preparing a Record For Use in `forkJoin` --- .../common/src/platform/misc/convertValues.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 libs/common/src/platform/misc/convertValues.ts diff --git a/libs/common/src/platform/misc/convertValues.ts b/libs/common/src/platform/misc/convertValues.ts new file mode 100644 index 00000000000..7a1087ec360 --- /dev/null +++ b/libs/common/src/platform/misc/convertValues.ts @@ -0,0 +1,23 @@ +import { ObservableInput, OperatorFunction, map } from "rxjs"; + +/** + * Converts a record of keys and values into a record preserving the original key and converting each value into an {@link ObservableInput}. + * @param project A function to project a given key and value pair into an {@link ObservableInput} + */ +export function convertValues( + project: (key: TKey, value: TInput) => ObservableInput, +): OperatorFunction, Record>> { + return map((inputRecord) => { + if (inputRecord == null) { + return null; + } + + // Can't use TKey in here, have to use `PropertyKey` + const result: Record> = {}; + for (const [key, value] of Object.entries(inputRecord) as [TKey, TInput][]) { + result[key] = project(key, value); + } + + return result; + }); +}