@tool-developer/wo-storage
Advanced tools
| 'use strict'; | ||
| // default expired time | ||
| var defaultExpireTime = 3600000; // 60*60*1000,1hour | ||
| // | ||
| function Storage(storage) { | ||
| // | ||
| function tryCatchParse(data) { | ||
| // | ||
| if (typeof data !== 'string') { | ||
| // | ||
| return data; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.parse(data); | ||
| } catch (e) { | ||
| data = {}; | ||
| } // | ||
| return data; | ||
| } // | ||
| function toReturn(data, callback) { | ||
| // | ||
| if (callback && typeof callback === 'function') { | ||
| // | ||
| callback(null, data); | ||
| } // | ||
| return data; | ||
| } | ||
| function beforeSet(k, v, e, cb) { | ||
| // | ||
| if (typeof e === 'function') { | ||
| cb = e; | ||
| e = defaultExpireTime; // 60*60*1000,1小时 | ||
| } // | ||
| e = parseInt(e, 10) || defaultExpireTime; // | ||
| var data = { | ||
| data: v | ||
| }; // e = -1 为永久缓存 | ||
| if (e !== -1) { | ||
| // 使用Date.now在jest 27可能会有bug | ||
| data.expired = Date.now() + e; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.stringify(data); | ||
| } catch (ex) { | ||
| // | ||
| return console.log('set storage data json format error', ex); | ||
| } // | ||
| return { | ||
| k: k, | ||
| data: data, | ||
| e: e, | ||
| cb: cb | ||
| }; | ||
| } | ||
| function afterGet(data, remove) { | ||
| if (!data) { | ||
| // | ||
| return undefined; | ||
| } // | ||
| data = tryCatchParse(data) || {}; // | ||
| var _data = data, | ||
| expired = _data.expired; // 永久缓存 | ||
| if (!expired) { | ||
| // | ||
| return data.data; | ||
| } | ||
| expired = parseInt(expired, 10); // 已过期 | ||
| if (Date.now() > expired) { | ||
| // 清除缓存 | ||
| if (remove && typeof remove === 'function') { | ||
| // | ||
| remove(); | ||
| } // | ||
| return undefined; | ||
| } // | ||
| return data.data; | ||
| } // | ||
| var base = { | ||
| get: function get(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| var res = storage.getItem(k); // | ||
| var data = afterGet(res, function () { | ||
| // | ||
| base.remove(k); | ||
| }); // | ||
| return toReturn(data, cb); | ||
| }, | ||
| set: function set(k, v, e, cb) { | ||
| // | ||
| var r = beforeSet(k, v, e, cb); | ||
| var data = r.data; // | ||
| k = r.k; | ||
| cb = r.cb; | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.setItem(k, data), cb); | ||
| }, | ||
| remove: function remove(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.removeItem(k), cb); | ||
| }, | ||
| clear: function clear(cb) { | ||
| // | ||
| return toReturn(storage.clear(), cb); | ||
| } | ||
| }; // | ||
| return base; | ||
| } | ||
| exports.Storage = Storage; |
+23
| var async = function async(storage) { | ||
| if (!storage) { | ||
| return {}; | ||
| } // | ||
| var methods = Object.keys(storage); | ||
| var base = {}; | ||
| methods.forEach(function (method) { | ||
| // | ||
| base[method] = function () { | ||
| for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
| args[_key] = arguments[_key]; | ||
| } // | ||
| return Promise.resolve(storage[method].apply(null, args)); | ||
| }; | ||
| }); | ||
| return base; | ||
| }; | ||
| export default async; |
| // default expired time | ||
| var defaultExpireTime = 3600000; // 60*60*1000,1hour | ||
| // | ||
| function Storage(storage) { | ||
| // | ||
| function tryCatchParse(data) { | ||
| // | ||
| if (typeof data !== 'string') { | ||
| // | ||
| return data; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.parse(data); | ||
| } catch (e) { | ||
| data = {}; | ||
| } // | ||
| return data; | ||
| } // | ||
| function toReturn(data, callback) { | ||
| // | ||
| if (callback && typeof callback === 'function') { | ||
| // | ||
| callback(null, data); | ||
| } // | ||
| return data; | ||
| } | ||
| function beforeSet(k, v, e, cb) { | ||
| // | ||
| if (typeof e === 'function') { | ||
| cb = e; | ||
| e = defaultExpireTime; // 60*60*1000,1小时 | ||
| } // | ||
| e = parseInt(e, 10) || defaultExpireTime; // | ||
| var data = { | ||
| data: v | ||
| }; // e = -1 为永久缓存 | ||
| if (e !== -1) { | ||
| // 使用Date.now在jest 27可能会有bug | ||
| data.expired = Date.now() + e; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.stringify(data); | ||
| } catch (ex) { | ||
| // | ||
| return console.log('set storage data json format error', ex); | ||
| } // | ||
| return { | ||
| k: k, | ||
| data: data, | ||
| e: e, | ||
| cb: cb | ||
| }; | ||
| } | ||
| function afterGet(data, remove) { | ||
| if (!data) { | ||
| // | ||
| return undefined; | ||
| } // | ||
| data = tryCatchParse(data) || {}; // | ||
| var _data = data, | ||
| expired = _data.expired; // 永久缓存 | ||
| if (!expired) { | ||
| // | ||
| return data.data; | ||
| } | ||
| expired = parseInt(expired, 10); // 已过期 | ||
| if (Date.now() > expired) { | ||
| // 清除缓存 | ||
| if (remove && typeof remove === 'function') { | ||
| // | ||
| remove(); | ||
| } // | ||
| return undefined; | ||
| } // | ||
| return data.data; | ||
| } // | ||
| var base = { | ||
| get: function get(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| var res = storage.getItem(k); // | ||
| var data = afterGet(res, function () { | ||
| // | ||
| base.remove(k); | ||
| }); // | ||
| return toReturn(data, cb); | ||
| }, | ||
| set: function set(k, v, e, cb) { | ||
| // | ||
| var r = beforeSet(k, v, e, cb); | ||
| var data = r.data; // | ||
| k = r.k; | ||
| cb = r.cb; | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.setItem(k, data), cb); | ||
| }, | ||
| remove: function remove(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.removeItem(k), cb); | ||
| }, | ||
| clear: function clear(cb) { | ||
| // | ||
| return toReturn(storage.clear(), cb); | ||
| } | ||
| }; // | ||
| return base; | ||
| } | ||
| export { Storage as S }; |
| import { S as Storage } from './index-c5940c13.js'; | ||
| var storage = new Storage(window.localStorage); | ||
| export default storage; |
| import { S as Storage } from './index-c5940c13.js'; | ||
| // | ||
| function SessionStorage() {} // 数据缓存 | ||
| SessionStorage.Caches = {}; | ||
| SessionStorage.prototype.setItem = function (key, data) { | ||
| // | ||
| SessionStorage.Caches[key] = data; | ||
| }; | ||
| SessionStorage.prototype.getItem = function (key) { | ||
| // | ||
| return SessionStorage.Caches[key]; | ||
| }; | ||
| SessionStorage.prototype.removeItem = function (key) { | ||
| // | ||
| delete SessionStorage.Caches[key]; | ||
| }; | ||
| SessionStorage.prototype.clear = function () { | ||
| // | ||
| SessionStorage.Caches = {}; | ||
| }; | ||
| function getSessionStore() { | ||
| try { | ||
| return window.sessionStorage; | ||
| } catch (e) { | ||
| return new SessionStorage(); | ||
| } | ||
| } | ||
| var session = getSessionStore(); | ||
| var storage = new Storage(session); | ||
| export default storage; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| module.exports = require('./dist/async'); | ||
| module.exports = require('./dist/async'); |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import session from './session'; | ||
| import async from './async'; | ||
| import session from './esm/session'; | ||
| import async from './esm/async'; | ||
@@ -4,0 +4,0 @@ const storage = async(session); |
+3
-1
@@ -0,1 +1,3 @@ | ||
| 'use strict'; | ||
| var async = function async(storage) { | ||
@@ -23,2 +25,2 @@ if (!storage) { | ||
| export default async; | ||
| module.exports = async; |
+6
-2
@@ -1,3 +0,7 @@ | ||
| var storage = new Storage(window.localStorage); | ||
| 'use strict'; | ||
| export default storage; | ||
| var index = require('./index-1745ebe8.js'); | ||
| var storage = new index.Storage(window.localStorage); | ||
| module.exports = storage; |
+4
-162
@@ -1,163 +0,5 @@ | ||
| // default expired time | ||
| var defaultExpireTime = 3600000; // 60*60*1000,1hour | ||
| // | ||
| 'use strict'; | ||
| function Storage(storage) { | ||
| // | ||
| function tryCatchParse(data) { | ||
| // | ||
| if (typeof data !== 'string') { | ||
| // | ||
| return data; | ||
| } // | ||
| var index = require('./index-1745ebe8.js'); | ||
| try { | ||
| // | ||
| data = JSON.parse(data); | ||
| } catch (e) { | ||
| data = {}; | ||
| } // | ||
| return data; | ||
| } // | ||
| function toReturn(data, callback) { | ||
| // | ||
| if (callback && typeof callback === 'function') { | ||
| // | ||
| callback(null, data); | ||
| } // | ||
| return data; | ||
| } | ||
| function beforeSet(k, v, e, cb) { | ||
| // | ||
| if (typeof e === 'function') { | ||
| cb = e; | ||
| e = defaultExpireTime; // 60*60*1000,1小时 | ||
| } // | ||
| e = parseInt(e, 10) || defaultExpireTime; // | ||
| var data = { | ||
| data: v | ||
| }; // e = -1 为永久缓存 | ||
| if (e !== -1) { | ||
| // 使用Date.now在jest 27可能会有bug | ||
| data.expired = Date.now() + e; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.stringify(data); | ||
| } catch (ex) { | ||
| // | ||
| return console.log('set storage data json format error', ex); | ||
| } // | ||
| return { | ||
| k: k, | ||
| data: data, | ||
| e: e, | ||
| cb: cb | ||
| }; | ||
| } | ||
| function afterGet(data, remove) { | ||
| if (!data) { | ||
| // | ||
| return undefined; | ||
| } // | ||
| data = tryCatchParse(data) || {}; // | ||
| var _data = data, | ||
| expired = _data.expired; // 永久缓存 | ||
| if (!expired) { | ||
| // | ||
| return data.data; | ||
| } | ||
| expired = parseInt(expired, 10); // 已过期 | ||
| if (Date.now() > expired) { | ||
| // 清除缓存 | ||
| if (remove && typeof remove === 'function') { | ||
| // | ||
| remove(); | ||
| } // | ||
| return undefined; | ||
| } // | ||
| return data.data; | ||
| } // | ||
| var base = { | ||
| get: function get(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| var res = storage.getItem(k); // | ||
| var data = afterGet(res, function () { | ||
| // | ||
| base.remove(k); | ||
| }); // | ||
| return toReturn(data, cb); | ||
| }, | ||
| set: function set(k, v, e, cb) { | ||
| // | ||
| var r = beforeSet(k, v, e, cb); | ||
| var data = r.data; // | ||
| k = r.k; | ||
| cb = r.cb; | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.setItem(k, data), cb); | ||
| }, | ||
| remove: function remove(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.removeItem(k), cb); | ||
| }, | ||
| clear: function clear(cb) { | ||
| // | ||
| return toReturn(storage.clear(), cb); | ||
| } | ||
| }; // | ||
| return base; | ||
| } | ||
| // | ||
@@ -199,4 +41,4 @@ function SessionStorage() {} // 数据缓存 | ||
| var storage = new Storage(session); | ||
| var storage = new index.Storage(session); | ||
| export default storage; | ||
| module.exports = storage; |
+5
-5
| { | ||
| "name": "@tool-developer/wo-storage", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "wo storage library", | ||
| "main": "index.js", | ||
| "module": "dist/index.js", | ||
| "main": "dist", | ||
| "module": "esm", | ||
| "scripts": { | ||
@@ -37,5 +37,5 @@ "test": "jest", | ||
| "dependencies": { | ||
| "@tool-developer/wo-base-storage": "^0.0.0-canary.1" | ||
| "@tool-developer/wo-base-storage": "^0.0.2" | ||
| }, | ||
| "gitHead": "6274893b760301017159dc8024da10fbc94e7aac" | ||
| "gitHead": "3e241183bdde5495df1341b6ccc21bcc7310fc0c" | ||
| } |
+1
-1
@@ -1,1 +0,1 @@ | ||
| module.exports = require('./dist/session') | ||
| module.exports = require('./dist/session'); |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import storage from './session'; | ||
| import storage from './esm/session'; | ||
| // 清空 | ||
@@ -3,0 +3,0 @@ storage.clear(); |
+1
-0
@@ -0,1 +1,2 @@ | ||
| import Storage from '@tool-developer/wo-base-storage/dist'; | ||
| export * from '@tool-developer/wo-base-storage/dist'; | ||
@@ -2,0 +3,0 @@ // |
| // default expired time | ||
| const defaultExpireTime = 3600000; // 60*60*1000,1hour | ||
| // | ||
| function Storage(storage) { | ||
| // | ||
| function tryCatchParse(data) { | ||
| // | ||
| if (typeof data !== 'string') { | ||
| // | ||
| return data; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.parse(data); | ||
| } catch (e) { | ||
| data = {}; | ||
| } // | ||
| return data; | ||
| } // | ||
| function toReturn(data, callback) { | ||
| // | ||
| if (callback && typeof callback === 'function') { | ||
| // | ||
| callback(null, data); | ||
| } // | ||
| return data; | ||
| } | ||
| function beforeSet(k, v, e, cb) { | ||
| // | ||
| if (typeof e === 'function') { | ||
| cb = e; | ||
| e = defaultExpireTime; // 60*60*1000,1小时 | ||
| } // | ||
| e = parseInt(e, 10) || defaultExpireTime; // | ||
| let data = { | ||
| data: v | ||
| }; // e = -1 为永久缓存 | ||
| if (e !== -1) { | ||
| // 使用Date.now在jest 27可能会有bug | ||
| data.expired = Date.now() + e; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.stringify(data); | ||
| } catch (ex) { | ||
| // | ||
| return console.log('set storage data json format error', ex); | ||
| } // | ||
| return { | ||
| k, | ||
| data, | ||
| e, | ||
| cb | ||
| }; | ||
| } | ||
| function afterGet(data, remove) { | ||
| if (!data) { | ||
| // | ||
| return undefined; | ||
| } // | ||
| data = tryCatchParse(data) || {}; // | ||
| let { | ||
| expired | ||
| } = data; // 永久缓存 | ||
| if (!expired) { | ||
| // | ||
| return data.data; | ||
| } | ||
| expired = parseInt(expired, 10); // 已过期 | ||
| if (Date.now() > expired) { | ||
| // 清除缓存 | ||
| if (remove && typeof remove === 'function') { | ||
| // | ||
| remove(); | ||
| } // | ||
| return undefined; | ||
| } // | ||
| return data.data; | ||
| } // | ||
| const base = { | ||
| get(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| const res = storage.getItem(k); // | ||
| const data = afterGet(res, function () { | ||
| // | ||
| base.remove(k); | ||
| }); // | ||
| return toReturn(data, cb); | ||
| }, | ||
| set(k, v, e, cb) { | ||
| // | ||
| const r = beforeSet(k, v, e, cb); | ||
| const { | ||
| data | ||
| } = r; // | ||
| k = r.k; | ||
| cb = r.cb; | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.setItem(k, data), cb); | ||
| }, | ||
| remove(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.removeItem(k), cb); | ||
| }, | ||
| clear(cb) { | ||
| // | ||
| return toReturn(storage.clear(), cb); | ||
| } | ||
| }; // | ||
| return base; | ||
| } | ||
| export { Storage as S }; |
| // default expired time | ||
| var defaultExpireTime = 3600000; // 60*60*1000,1hour | ||
| // | ||
| function Storage(storage) { | ||
| // | ||
| function tryCatchParse(data) { | ||
| // | ||
| if (typeof data !== 'string') { | ||
| // | ||
| return data; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.parse(data); | ||
| } catch (e) { | ||
| data = {}; | ||
| } // | ||
| return data; | ||
| } // | ||
| function toReturn(data, callback) { | ||
| // | ||
| if (callback && typeof callback === 'function') { | ||
| // | ||
| callback(null, data); | ||
| } // | ||
| return data; | ||
| } | ||
| function beforeSet(k, v, e, cb) { | ||
| // | ||
| if (typeof e === 'function') { | ||
| cb = e; | ||
| e = defaultExpireTime; // 60*60*1000,1小时 | ||
| } // | ||
| e = parseInt(e, 10) || defaultExpireTime; // | ||
| var data = { | ||
| data: v | ||
| }; // e = -1 为永久缓存 | ||
| if (e !== -1) { | ||
| // 使用Date.now在jest 27可能会有bug | ||
| data.expired = Date.now() + e; | ||
| } // | ||
| try { | ||
| // | ||
| data = JSON.stringify(data); | ||
| } catch (ex) { | ||
| // | ||
| return console.log('set storage data json format error', ex); | ||
| } // | ||
| return { | ||
| k: k, | ||
| data: data, | ||
| e: e, | ||
| cb: cb | ||
| }; | ||
| } | ||
| function afterGet(data, remove) { | ||
| if (!data) { | ||
| // | ||
| return undefined; | ||
| } // | ||
| data = tryCatchParse(data) || {}; // | ||
| var _data = data, | ||
| expired = _data.expired; // 永久缓存 | ||
| if (!expired) { | ||
| // | ||
| return data.data; | ||
| } | ||
| expired = parseInt(expired, 10); // 已过期 | ||
| if (Date.now() > expired) { | ||
| // 清除缓存 | ||
| if (remove && typeof remove === 'function') { | ||
| // | ||
| remove(); | ||
| } // | ||
| return undefined; | ||
| } // | ||
| return data.data; | ||
| } // | ||
| var base = { | ||
| get: function get(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| var res = storage.getItem(k); // | ||
| var data = afterGet(res, function () { | ||
| // | ||
| base.remove(k); | ||
| }); // | ||
| return toReturn(data, cb); | ||
| }, | ||
| set: function set(k, v, e, cb) { | ||
| // | ||
| var r = beforeSet(k, v, e, cb); | ||
| var data = r.data; // | ||
| k = r.k; | ||
| cb = r.cb; | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.setItem(k, data), cb); | ||
| }, | ||
| remove: function remove(k, cb) { | ||
| // | ||
| if (k === undefined || k === null) { | ||
| // | ||
| return toReturn(undefined, cb); | ||
| } // | ||
| return toReturn(storage.removeItem(k), cb); | ||
| }, | ||
| clear: function clear(cb) { | ||
| // | ||
| return toReturn(storage.clear(), cb); | ||
| } | ||
| }; // | ||
| return base; | ||
| } | ||
| export { Storage as S }; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21
16.67%16817
-5.74%472
-11.94%1
Infinity%+ Added
- Removed