Merge branch 'renovate/playwright' into 'main'
[ProtonMail-WebClient.git] / packages / redux-shared-store / persist.ts
blobf2a67980c91e9298edd53bc49ca682ac6dbf80a1
1 import isTruthy from '@proton/utils/isTruthy';
3 export const getPersistedState = <T extends object, K extends keyof T>(
4     state: T,
5     persistReducer: Partial<{ [key in K]: (value: any) => any }>
6 ) => {
7     return JSON.stringify(
8         Object.fromEntries(
9             Object.entries(state)
10                 .map(([key, value]) => {
11                     const transformedValue = persistReducer[key as keyof typeof persistReducer]?.(value);
12                     return transformedValue ? [key, transformedValue] : undefined;
13                 })
14                 .filter(isTruthy)
15         ),
16         (key, value) => {
17             if (key === 'fetchedEphemeral') {
18                 return undefined;
19             }
20             return value;
21         }
22     );