From 4a2c14dc2e68b9b1dd6b5dbc65c025b07daddd1c Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Fri, 1 Nov 2024 09:49:31 +1000 Subject: [PATCH] [PM-14161] Add getById and getByIds rjxs helpers (#11742) --- libs/common/src/platform/misc/index.ts | 1 + .../src/platform/misc/rxjs-operators.spec.ts | 58 +++++++++++++++++++ .../src/platform/misc/rxjs-operators.ts | 21 +++++++ 3 files changed, 80 insertions(+) create mode 100644 libs/common/src/platform/misc/index.ts create mode 100644 libs/common/src/platform/misc/rxjs-operators.spec.ts create mode 100644 libs/common/src/platform/misc/rxjs-operators.ts diff --git a/libs/common/src/platform/misc/index.ts b/libs/common/src/platform/misc/index.ts new file mode 100644 index 0000000000..56fc18c282 --- /dev/null +++ b/libs/common/src/platform/misc/index.ts @@ -0,0 +1 @@ +export * from "./rxjs-operators"; diff --git a/libs/common/src/platform/misc/rxjs-operators.spec.ts b/libs/common/src/platform/misc/rxjs-operators.spec.ts new file mode 100644 index 0000000000..c9ec2c091e --- /dev/null +++ b/libs/common/src/platform/misc/rxjs-operators.spec.ts @@ -0,0 +1,58 @@ +import { firstValueFrom, of } from "rxjs"; + +import { getById, getByIds } from "./rxjs-operators"; + +describe("custom rxjs operators", () => { + describe("getById", () => { + it("returns an object with a matching id", async () => { + const obs = of([ + { + id: 1, + data: "one", + }, + { + id: 2, + data: "two", + }, + { + id: 3, + data: "three", + }, + ]).pipe(getById(2)); + + const result = await firstValueFrom(obs); + + expect(result).toEqual({ id: 2, data: "two" }); + }); + }); + + describe("getByIds", () => { + it("returns an array of objects with matching ids", async () => { + const obs = of([ + { + id: 1, + data: "one", + }, + { + id: 2, + data: "two", + }, + { + id: 3, + data: "three", + }, + { + id: 4, + data: "four", + }, + ]).pipe(getByIds([2, 3])); + + const result = await firstValueFrom(obs); + + expect(result).toEqual([ + { id: 2, data: "two" }, + { id: 3, data: "three" }, + ]); + }); + }); +}); diff --git a/libs/common/src/platform/misc/rxjs-operators.ts b/libs/common/src/platform/misc/rxjs-operators.ts new file mode 100644 index 0000000000..689b928cd2 --- /dev/null +++ b/libs/common/src/platform/misc/rxjs-operators.ts @@ -0,0 +1,21 @@ +import { map } from "rxjs"; + +/** + * An rxjs operator that extracts an object by ID from an array of objects. + * @param id The ID of the object to return. + * @returns The first object with a matching ID, or undefined if no matching object is present. + */ +export const getById = (id: TId) => + map((objects) => objects.find((o) => o.id === id)); + +/** + * An rxjs operator that extracts a subset of objects by their IDs from an array of objects. + * @param id The IDs of the objects to return. + * @returns An array containing objects with matching IDs, or an empty array if there are no matching objects. + */ +export const getByIds = (ids: TId[]) => { + const idSet = new Set(ids); + return map((objects) => { + return objects.filter((o) => idSet.has(o.id)); + }); +};