Skip to content

Commit

Permalink
Release 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisjordangarcia committed Nov 22, 2017
1 parent af81b0a commit bd5f6aa
Show file tree
Hide file tree
Showing 3 changed files with 4,209 additions and 5 deletions.
42 changes: 42 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

import { reduxPersist, createTransform } from 'redux-persist';

const PERSIST_EXPIRE_DEFAULT_KEY = 'persistExpiresAt';

function dateToUnix(date) {
return +(date.getTime() / 1000).toFixed(0);
}

export default function createExpirationTransform(expireDatas) {
expireDatas = expireDatas || {};
let stateMap = new Map();
const inbound = (state, key) => {
if ((state || typeof state === 'object') && expireDatas.hasOwnProperty(key)) {
if (stateMap.has(key)) {
if (typeof state.toJS === 'function') {
let newState = state.toJS();
newState.expireDate = new Date(new Date().getTime() + expireDatas[key].expireSpan);
return newState;
} else {
state.expireDate = new Date(new Date().getTime() + expireDatas[key].expireSpan);
}
} else {
stateMap.set(key, true);
}
}
return state;
};
const outbound = (state, key) => {
if ((state || typeof state === 'object') && expireDatas.hasOwnProperty(key)) {
if (state.expireDate) {
const data = expireDatas[key];
if (dateToUnix(new Date(state.expireDate)) < dateToUnix(new Date())) {
state = data.default;
}
}
}
return state;
};
return createTransform(inbound, outbound);
}
Loading

0 comments on commit bd5f6aa

Please sign in to comment.