|
|
@ -12,8 +12,22 @@ export const Cache = { |
|
|
|
_generateKey: function (key) { |
|
|
|
_generateKey: function (key) { |
|
|
|
return Cache._getKeyPrefix() + (key || ""); |
|
|
|
return Cache._getKeyPrefix() + (key || ""); |
|
|
|
}, |
|
|
|
}, |
|
|
|
getItem: function (key) { |
|
|
|
getItem: function (key, opt = {}) { |
|
|
|
return localStorage.getItem(Cache._generateKey(key)); |
|
|
|
const { defaultValue, typeConversion } = opt; |
|
|
|
|
|
|
|
const storageKey = Cache._generateKey(key); |
|
|
|
|
|
|
|
let value = localStorage.getItem(storageKey); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (value !== null && typeConversion) { |
|
|
|
|
|
|
|
if (value === "true") { |
|
|
|
|
|
|
|
value = true; |
|
|
|
|
|
|
|
} else if (value === "false") { |
|
|
|
|
|
|
|
value = false; |
|
|
|
|
|
|
|
} else if (Number.isNaN(Number(value))) { |
|
|
|
|
|
|
|
value = Number(value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return value !== null ? value : defaultValue; |
|
|
|
}, |
|
|
|
}, |
|
|
|
setItem: function (key, value) { |
|
|
|
setItem: function (key, value) { |
|
|
|
localStorage.setItem(Cache._generateKey(key), value); |
|
|
|
localStorage.setItem(Cache._generateKey(key), value); |
|
|
|