Skip to content

Commit

Permalink
feat: pass full state to persist transforms (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
damassi committed Apr 18, 2023
1 parent aa5dd51 commit 90db988
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,8 @@ export interface PersistStorage {
}

export interface Transformer {
in?: (data: any, key: string) => any;
out?: (data: any, key: string) => any;
in?: (data: any, key: string, fullState?: any) => any;
out?: (data: any, key: string, fullState?: any) => any;
}

export interface PersistConfig<Model extends object> {
Expand All @@ -1054,8 +1054,8 @@ export interface TransformConfig {
}

export function createTransform(
inbound?: (data: any, key: string) => any,
outbound?: (data: any, key: string) => any,
inbound?: (data: any, key: string, fullState?: any) => any,
outbound?: (data: any, key: string, fullState?: any) => any,
config?: TransformConfig,
): Transformer;

Expand Down
4 changes: 2 additions & 2 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function createStorageWrapper(storage, transformers = [], migrations = {}) {
if (transformers.length > 0 && data != null && typeof data === 'object') {
Object.keys(data).forEach((key) => {
data[key] = transformers.reduce(
(acc, cur) => cur.in(acc, key),
(acc, cur) => cur.in(acc, key, data),
data[key],
);
});
Expand All @@ -85,7 +85,7 @@ function createStorageWrapper(storage, transformers = [], migrations = {}) {
) {
Object.keys(result).forEach((key) => {
result[key] = outTransformers.reduce(
(acc, cur) => cur.out(acc, key),
(acc, cur) => cur.out(acc, key, result),
result[key],
);
});
Expand Down
12 changes: 8 additions & 4 deletions tests/persist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,14 @@ test('clear', async () => {
test('transformers', async () => {
// ARRANGE
const upperCaseTransformer = createTransform(
(data, key) => {
(data, key, fullState) => {
expect(key).toBe('one');
expect(fullState).toEqual({"one": "item one", "two": "item two"})
return data.toUpperCase();
},
(data, key) => {
(data, key, fullState) => {
expect(key).toBe('one');
expect(fullState).toEqual({"one": "_ITEM ONE_", "two": "item two"})
return data.toLowerCase();
},
{
Expand All @@ -659,12 +661,14 @@ test('transformers', async () => {
);

const padTransformer = createTransform(
(data, key) => {
(data, key, fullState) => {
expect(key).toBe('one');
expect(fullState).toEqual({"one": "item one", "two": "item two"})
return `_${data}_`;
},
(data, key) => {
(data, key, fullState) => {
expect(key).toBe('one');
expect(fullState).toEqual({"one": "_ITEM ONE_", "two": "item two"})
return data.substr(1, data.length - 2);
},
{
Expand Down
2 changes: 1 addition & 1 deletion tests/typescript/persist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ createTransform(
(data, key) => `${key}foo`,
);

createTransform((data, key) => `${key}foo`);
createTransform((data, key, fullState) => `${key}foo`);

createTransform(undefined, (data, key) => `${key}foo`, {
whitelist: ['foo'],
Expand Down
8 changes: 4 additions & 4 deletions website/docs/docs/api/create-transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ This helper has been directly copied from [`redux-persist`](https://github.com/r

The function accepts the following arguments:

- `inbound` (data: any, key: string) => any; *optional*
- `inbound` (data: any, key: string, fullState: any) => any; *optional*

This function will be executed against data prior to it being persisted by the configured storage engine.
- `outbound` (data: any, key: string) => any; *optional*

- `outbound` (data: any, key: string, fullState: any) => any; *optional*

This function will be executed against data prior after it is extracted from the configured storage engine.

Expand All @@ -29,7 +29,7 @@ The function accepts the following arguments:
- `whitelist` Array&lt;string&gt;; *optional*

The data keys that this transformer would apply to.

- `blacklist` Array&lt;string&gt;; *optional*

The data keys that this transformer would not apply to.
Expand Down

1 comment on commit 90db988

@vercel
Copy link

@vercel vercel bot commented on 90db988 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

easy-peasy – ./

easy-peasy-ctrlplusb.vercel.app
easy-peasy.dev
easy-peasy-git-master-ctrlplusb.vercel.app

Please sign in to comment.