/
index.js
55 lines (41 loc) · 1.56 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
49
50
51
52
53
54
module.exports = syncInterface => store => next => action => {
const result = next(action);
const nextState = store.getState();
const reducedState = applyInterface(nextState, syncInterface);
window.localStorage.setItem(`reduxLocalStorage`, JSON.stringify(reducedState));
return result;
};
function applyInterface(state, syncInterface) {
let result = {};
if (!isPlainObject(state)) {
console.warn('WARNING! "state" must be a plain object.' );
console.warn('state: ', JSON.stringify(state, null, 2));
}
if (!isPlainObject(syncInterface)) {
console.warn('WARNING! "syncInterface" must be a plain object.' );
console.warn('syncInterface: ', JSON.stringify(syncInterface, null, 2));
}
for (var key in syncInterface) {
result[key] = get(syncInterface[key], state[key]);
}
return result;
}
function get(currentInterfaceInstance, currentStateLevel) {
let result;
if (currentInterfaceInstance instanceof Array) {
result = currentStateLevel.map(item => {
return get(currentInterfaceInstance[0], item);
});
} else if (isPlainObject(currentInterfaceInstance)) {
result = {};
for (var currentKey in currentInterfaceInstance) {
result[currentKey] = get(currentInterfaceInstance[currentKey], currentStateLevel[currentKey])
}
} else if (currentInterfaceInstance) {
result = currentStateLevel;
}
return result;
}
function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
};