diff --git a/README.md b/README.md index 4e29347..ab2d886 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ const expirationKey = "expirationKey"; const persistConfig = { key: "v1", storage, - transforms: [expireIn(expireIn, expirationKey)] + transforms: [expireIn(expireIn, expirationKey, [])] }; const persistedReducer = persistCombineReducers(persistConfig, reducer); @@ -43,6 +43,7 @@ export const persistor = persistStore(store); | ------------- | ------ | ----------------------- | ----------------------------------------------- | | expireIn | Number | none | For how long the state is going to be preserved | | expirationKey | String | 'persistencyExpiration' | Key used by the localStorage | +| defaultValue | any | {} | Value to which state will be cleared to | ## Difference with `redux-persist-transform-expire` diff --git a/src/index.test.ts b/src/index.test.ts index 9a4b729..703b425 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -79,4 +79,15 @@ describe("redux-persist-transform-expire-in", () => { expect(createTransformMock).toHaveBeenCalled(); expect(createTransformMock.mock.calls[0][1]({ foo: "bar" })).toEqual({}); }); + + it("should return default the state if expired", () => { + localStorage.setItem( + "persistencyExpiration", + new Date().getTime().toString(), + ); + mockdate.set("1/3/2000"); + expireIn(oneDay, 'persistencyExpiration', []); + expect(createTransformMock).toHaveBeenCalled(); + expect(createTransformMock.mock.calls[0][1]({ foo: "bar" })).toEqual([]); + }); }); diff --git a/src/index.ts b/src/index.ts index ad5234c..5621772 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,35 +9,34 @@ import { * `redux-persist` transformer that reset the persisted redux state after a specific period of time. * @param {number} expireIn For how long the state is going to be preserved * @param {string} [expireKey="persistencyExpiration"] Key used by the localStorage + * @param {any} defaultValue Value to which state will be cleared to */ + const transformExpire = ( expireIn: number, - expireKey: string = "persistencyExpiration" + expireKey: string = "persistencyExpiration", + defaultValue = {} ): Transform => { - let expired = false; const storedExpiration = localStorage.getItem(expireKey); + let expired = false; + if (storedExpiration) { const expiring = parseInt(storedExpiration); const now = new Date().getTime(); expired = !isNaN(expiring) && now > expiring; } + return createTransform( - ( - inboundState: TransformIn - ): TransformIn => { - setTimeout( - (): void => - localStorage.setItem( - expireKey, - (new Date().getTime() + expireIn).toString() - ), - 0 - ); + (inboundState: TransformIn) => { + setTimeout(() => { + const expireValue = (new Date().getTime() + expireIn).toString(); + localStorage.setItem(expireKey, expireValue); + }, 0); + return inboundState; }, - ( - outboundState: TransformOut - ): TransformOut | any => (expired ? {} : outboundState) + (outboundState: TransformOut) => + expired ? defaultValue : outboundState ); };