-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
48 lines (35 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var reduxPersist = require('redux-persist');
var traverse = require('traverse');
var PERSIST_EXPIRE_DEFAULT_KEY = 'persistExpiresAt';
module.exports = function (config) {
config = config || {};
config.expireKey = config.expireKey || PERSIST_EXPIRE_DEFAULT_KEY;
config.defaultState = config.defaultState || {};
function dateToUnix (date) {
return +(date.getTime() / 1000).toFixed(0);
}
function inbound (state) {
if (!state) return state;
return state;
}
function outbound (state) {
if (!state) return state;
var validState = traverse(state).forEach(function (value) {
if (!value || typeof value !== 'object') {
return;
}
if (!value.hasOwnProperty(config.expireKey)) {
return;
}
var expireDate = value[config.expireKey];
if (!expireDate) {
return;
}
if (dateToUnix(new Date(expireDate)) < dateToUnix(new Date())) {
this.update(config.defaultState);
}
});
return validState;
}
return reduxPersist.createTransform(inbound, outbound);
};