From 840cb98b8a122c3c1462103d6fda67c92efcd579 Mon Sep 17 00:00:00 2001 From: Akinsola Lawanson <106528085+akinsola-guardian@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:51:04 +0000 Subject: [PATCH] Create participants.ts --- .../lib/participants.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 libs/@guardian/libs/src/consent-management-platform/lib/participants.ts diff --git a/libs/@guardian/libs/src/consent-management-platform/lib/participants.ts b/libs/@guardian/libs/src/consent-management-platform/lib/participants.ts new file mode 100644 index 000000000..3bc097495 --- /dev/null +++ b/libs/@guardian/libs/src/consent-management-platform/lib/participants.ts @@ -0,0 +1,23 @@ +import { isObject } from '../../isObject/isObject'; +import { isString } from '../../isString/isString'; +import { storage } from '../../storage/storage'; + +export type Participations = Record; + +const participationsKey = 'gu.ab.participations'; +export const getParticipationsFromLocalStorage = (): Participations => { + const participations = storage.local.get(participationsKey); + return isParticipations(participations) ? participations : {}; +}; + +const isParticipations = ( + participations: unknown, +): participations is Participations => { + return ( + isObject(participations) && + Object.values(participations).every( + (participation) => + isObject(participation) && isString(participation.variant), + ) + ); +};