natty-storage
Advanced tools
Comparing version 2.0.0 to 2.0.1
## Change log | ||
#### v2.0.0 / 2017-03-16 | ||
* 删除了创建缓存对象时的`async`参数,现在一个缓存对象可以同时使用同步和异步方法。 | ||
* 缓存对象添加了一套异步方法,`asyncSet`、`asyncSet`、`asyncHas`、`asyncRemove`、`asyncDestroy`。 | ||
* 添加了`nattyStorage.clean`方法,清理掉因为`until`和`duration`的有效期而过期的数据。 | ||
* `nattyStorage.list`方法,方便开发时调试。 | ||
* `nattyStorage.each`方法,方便自定义处理所有缓存对象。 | ||
* `Gzip`后的文件大小由`2.5K`降到`2K`。 | ||
#### v1.1.2 / 2016-11-07 | ||
@@ -4,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! natty-storage.js v2.0.0 | MIT License | fushan | https://github.com/jias/natty-storage */ | ||
/*! natty-storage.js v2.0.1 | MIT License | fushan | https://github.com/jias/natty-storage */ | ||
(function (global, factory) { | ||
@@ -224,3 +224,3 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
// instance.get('foo.bar') | ||
Storage.prototype.get = function get (path) { | ||
Storage.prototype.get = function get (path, fallbackValue) { | ||
if (!this._data) { | ||
@@ -242,3 +242,3 @@ this._lazyInit(); | ||
return data | ||
return data !== UNDEFINED ? data : fallbackValue | ||
}; | ||
@@ -321,3 +321,3 @@ | ||
// 添加异步方法 | ||
// 添加异步方法:asyncSet asyncGet asyncHas asyncRemove asyncDestroy | ||
var methodHasAsyncMode = ['set', 'get', 'has', 'remove', 'destroy']; | ||
@@ -509,3 +509,3 @@ var loop = function ( i, l ) { | ||
nattyStorage.version = '2.0.0'; | ||
nattyStorage.version = '2.0.1'; | ||
nattyStorage._variable = variable; | ||
@@ -554,2 +554,5 @@ nattyStorage.supportStorage = supportStorage; | ||
// 内置处理一次过期数据 | ||
nattyStorage.clean(); | ||
return nattyStorage; | ||
@@ -556,0 +559,0 @@ |
@@ -1,3 +0,3 @@ | ||
/*! natty-storage.min.js v2.0.0 | MIT License | fushan | https://github.com/jias/natty-storage */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.nattyStorage=e()}(this,function(){"use strict";function t(t){return t=window[t],{set:function(e,i){t.setItem(e,JSON.stringify(i))},get:function(e){var i=t.getItem(e);if(!i)return y;try{i=JSON.parse(i)}catch(t){}return i},remove:function(e){t.removeItem(e)}}}function e(){var t=K;return{set:function(e,i){t[e]=i},get:function(e){return e in t?t[e]:y},remove:function(e){delete t[e]}}}function i(t){return t.split(v).reverse().join(v)}function r(t){var e;if(t.indexOf("\\.")===-1)e=t.split(".");else{e=i(t).split(/\.(?!\\)/).reverse();for(var r=0,n=e.length;r<n;r++)e[r]=i(e[r].replace(/\.\\/g,"."))}return e}function n(t,e,i){for(var n=r(t),a=i;n.length;){var o=n.shift();if(n.length)a[o]=a[o]||{},a=a[o];else{if(!_(a))throw new Error("Cannot create property `"+o+"` on non-object value, path:`"+t+"`");a[o]=e}}return i}function a(t,e,i){if((i=i||!1)===!0||t.indexOf(".")===-1)return e[t];for(var n=r(t);n.length;){if("object"!=typeof(e=a(n.shift(),e,!0))||e===D){n.length&&(e=D);break}}return e}function o(t,e,i){if(i=i||t.indexOf(".")===-1)return e.hasOwnProperty(t);for(var n=r(t);n.length;){var o=n.shift(),s=e.hasOwnProperty(o);if(!s||!n.length)return s;if(e=a(o,e,!0),!_(e))return w}}function s(t,e){for(var i=r(t),n=e;i.length;){var a=i.shift();i.length?(n[a]=n[a]||{},n=n[a]):delete n[a]}return e}var h="undefined"!=typeof window,c="undefined"!=typeof console,u=function(t){return"object"==typeof t&&null!==t},f=function(t){return null!==t&&t===t.window},_=function(t){return null!==t&&u(t)&&!f(t)&&Object.getPrototypeOf(t)===Object.prototype},d=function(t){return"[object Array]"===toString.call(t)},l=function(t,e){void 0===t&&(t={}),void 0===e&&(e={});for(var i in e)e.hasOwnProperty(i)&&e[i]!==undefined&&(d(e[i])?t[i]=[].concat(e[i]):_(e[i])?t[i]=l({},e[i]):t[i]=e[i]);return t},g=function(t){return function(){for(var e=arguments,i=t(e[0],e[1]),r=2,n=e.length;r<n;r++)i=t(i,e[r]);return i}}(l),p=function(){},y=null,v="",k=!0,w=!k,D=undefined,m=function(){if(!h)return w;if("object"!=typeof localStorage)return w;var t=k;try{var e="__nstest__";localStorage.setItem(e,e),t=localStorage.getItem(e)===e,localStorage.removeItem(e)}catch(e){t=w}return t}(),E={type:"variable",key:v,tag:v,duration:0,until:0},b=function(i){if(void 0===i&&(i={}),this.config=g({},E,i),!this.config.key)throw new Error("`key` is required when creating a storage!");this._storage="variable"!==this.config.type&&m?t(this.config.type):e(),this._CHECK_KEY="NSCheck:"+this.config.key,this._DATA_KEY="NSData:"+this.config.key,this._createStamp=+new Date,this._backupData={},this._checkData=this._storage.get(this._CHECK_KEY),this._isNew=this._checkData===y};b.prototype._lazyInit=function(){this._isNew||this.isOutdated()?this._storage.set(this._DATA_KEY,this._data={}):(this._data=this._storage.get(this._DATA_KEY),this._data===y&&this._storage.set(this._DATA_KEY,this._data={})),this._storage.set(this._CHECK_KEY,this._checkData={key:this.config.key,tag:this.config.tag,lastUpdate:this._createStamp,duration:this.config.duration,until:this.config.until})},b.prototype.isOutdated=function(){if(this.config.tag&&this.config.tag!==this._checkData.tag)return k;var t=+new Date;return this._checkData.duration&&t-this._checkData.lastUpdate>this._checkData.duration?k:this._checkData.until&&t>this._checkData.until?k:w},b.prototype.set=function(t,e){this._data||this._lazyInit(),this._backupData=this._data;try{n(t,e,this._data),this._storage.set(this._DATA_KEY,this._data)}catch(t){throw this._storage.set(this._DATA_KEY,this._data=this._backupData),new Error(t)}},b.prototype.get=function(t){this._data||this._lazyInit();var e;try{e=t?a(t,this._data):this._data}catch(t){throw new Error(t)}return e},b.prototype.has=function(t){if(this._data||this._lazyInit(),!t)throw new Error("a `key|path` argument should be passed into the `has` method");return o(t,this._data)?{has:k,value:a(t,this._data)}:{has:w,value:D}},b.prototype.remove=function(t){this._data||this._lazyInit(),this._backupData=this._data,t?s(t,this._data):this._data={};try{this._storage.set(this._DATA_KEY,this._data)}catch(t){throw this._storage.set(this._DATA_KEY,this._data=this._backupData),new Error(t)}},b.prototype.destroy=function(){var t=this;this._data=y,this._checkData=y,this._storage.remove(this._DATA_KEY),this._storage.remove(this._CHECK_KEY);for(var e in t.constructor.prototype)t.constructor.prototype.hasOwnProperty(e)&&(t[e]=D)},b.prototype.dump=function(){this._data||this._lazyInit(),JSON&&c&&console.log(JSON.stringify(this._data,y,4))};for(var S=["set","get","has","remove","destroy"],A=0,O=S.length;A<O;A++)!function(t,e){var i=S[t];b.prototype["async"+i.charAt(0).toUpperCase()+i.substr(1)]=function(){var t=arguments,e=this;return new Promise(function(r,n){try{r(e[i].apply(e,t))}catch(t){n(t)}})}}(A);var K={},C=function(t){return new b(t)};return C.version="2.0.0",C._variable=K,C.supportStorage=m,C.each=function(t){void 0===t&&(t=p);var e={variable:C._variable};m&&(e.localStorage=localStorage,e.sessionStorage=sessionStorage);for(var i in e)for(var r in e[i])if(r.indexOf("NSCheck:")>-1){var n=C({key:r.match(/NSCheck:(.*)/)[1],type:i});t(n)}},C.clean=function(){this.each(function(t){t.isOutdated()&&t.destroy()})},C.list=function(){this.each(function(t){c&&console.log(t.config.type,t.config.key,t.get())})},C}); | ||
/*! natty-storage.min.js v2.0.1 | MIT License | fushan | https://github.com/jias/natty-storage */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.nattyStorage=e()}(this,function(){"use strict";function t(t){return t=window[t],{set:function(e,i){t.setItem(e,JSON.stringify(i))},get:function(e){var i=t.getItem(e);if(!i)return y;try{i=JSON.parse(i)}catch(t){}return i},remove:function(e){t.removeItem(e)}}}function e(){var t=K;return{set:function(e,i){t[e]=i},get:function(e){return e in t?t[e]:y},remove:function(e){delete t[e]}}}function i(t){return t.split(v).reverse().join(v)}function n(t){var e;if(t.indexOf("\\.")===-1)e=t.split(".");else{e=i(t).split(/\.(?!\\)/).reverse();for(var n=0,r=e.length;n<r;n++)e[n]=i(e[n].replace(/\.\\/g,"."))}return e}function r(t,e,i){for(var r=n(t),a=i;r.length;){var o=r.shift();if(r.length)a[o]=a[o]||{},a=a[o];else{if(!_(a))throw new Error("Cannot create property `"+o+"` on non-object value, path:`"+t+"`");a[o]=e}}return i}function a(t,e,i){if((i=i||!1)===!0||t.indexOf(".")===-1)return e[t];for(var r=n(t);r.length;){if("object"!=typeof(e=a(r.shift(),e,!0))||e===D){r.length&&(e=D);break}}return e}function o(t,e,i){if(i=i||t.indexOf(".")===-1)return e.hasOwnProperty(t);for(var r=n(t);r.length;){var o=r.shift(),s=e.hasOwnProperty(o);if(!s||!r.length)return s;if(e=a(o,e,!0),!_(e))return w}}function s(t,e){for(var i=n(t),r=e;i.length;){var a=i.shift();i.length?(r[a]=r[a]||{},r=r[a]):delete r[a]}return e}var h="undefined"!=typeof window,c="undefined"!=typeof console,u=function(t){return"object"==typeof t&&null!==t},f=function(t){return null!==t&&t===t.window},_=function(t){return null!==t&&u(t)&&!f(t)&&Object.getPrototypeOf(t)===Object.prototype},d=function(t){return"[object Array]"===toString.call(t)},l=function(t,e){void 0===t&&(t={}),void 0===e&&(e={});for(var i in e)e.hasOwnProperty(i)&&e[i]!==undefined&&(d(e[i])?t[i]=[].concat(e[i]):_(e[i])?t[i]=l({},e[i]):t[i]=e[i]);return t},g=function(t){return function(){for(var e=arguments,i=t(e[0],e[1]),n=2,r=e.length;n<r;n++)i=t(i,e[n]);return i}}(l),p=function(){},y=null,v="",k=!0,w=!k,D=undefined,m=function(){if(!h)return w;if("object"!=typeof localStorage)return w;var t=k;try{var e="__nstest__";localStorage.setItem(e,e),t=localStorage.getItem(e)===e,localStorage.removeItem(e)}catch(e){t=w}return t}(),E={type:"variable",key:v,tag:v,duration:0,until:0},b=function(i){if(void 0===i&&(i={}),this.config=g({},E,i),!this.config.key)throw new Error("`key` is required when creating a storage!");this._storage="variable"!==this.config.type&&m?t(this.config.type):e(),this._CHECK_KEY="NSCheck:"+this.config.key,this._DATA_KEY="NSData:"+this.config.key,this._createStamp=+new Date,this._backupData={},this._checkData=this._storage.get(this._CHECK_KEY),this._isNew=this._checkData===y};b.prototype._lazyInit=function(){this._isNew||this.isOutdated()?this._storage.set(this._DATA_KEY,this._data={}):(this._data=this._storage.get(this._DATA_KEY),this._data===y&&this._storage.set(this._DATA_KEY,this._data={})),this._storage.set(this._CHECK_KEY,this._checkData={key:this.config.key,tag:this.config.tag,lastUpdate:this._createStamp,duration:this.config.duration,until:this.config.until})},b.prototype.isOutdated=function(){if(this.config.tag&&this.config.tag!==this._checkData.tag)return k;var t=+new Date;return this._checkData.duration&&t-this._checkData.lastUpdate>this._checkData.duration?k:this._checkData.until&&t>this._checkData.until?k:w},b.prototype.set=function(t,e){this._data||this._lazyInit(),this._backupData=this._data;try{r(t,e,this._data),this._storage.set(this._DATA_KEY,this._data)}catch(t){throw this._storage.set(this._DATA_KEY,this._data=this._backupData),new Error(t)}},b.prototype.get=function(t,e){this._data||this._lazyInit();var i;try{i=t?a(t,this._data):this._data}catch(t){throw new Error(t)}return i!==D?i:e},b.prototype.has=function(t){if(this._data||this._lazyInit(),!t)throw new Error("a `key|path` argument should be passed into the `has` method");return o(t,this._data)?{has:k,value:a(t,this._data)}:{has:w,value:D}},b.prototype.remove=function(t){this._data||this._lazyInit(),this._backupData=this._data,t?s(t,this._data):this._data={};try{this._storage.set(this._DATA_KEY,this._data)}catch(t){throw this._storage.set(this._DATA_KEY,this._data=this._backupData),new Error(t)}},b.prototype.destroy=function(){var t=this;this._data=y,this._checkData=y,this._storage.remove(this._DATA_KEY),this._storage.remove(this._CHECK_KEY);for(var e in t.constructor.prototype)t.constructor.prototype.hasOwnProperty(e)&&(t[e]=D)},b.prototype.dump=function(){this._data||this._lazyInit(),JSON&&c&&console.log(JSON.stringify(this._data,y,4))};for(var S=["set","get","has","remove","destroy"],A=0,O=S.length;A<O;A++)!function(t,e){var i=S[t];b.prototype["async"+i.charAt(0).toUpperCase()+i.substr(1)]=function(){var t=arguments,e=this;return new Promise(function(n,r){try{n(e[i].apply(e,t))}catch(t){r(t)}})}}(A);var K={},C=function(t){return new b(t)};return C.version="2.0.1",C._variable=K,C.supportStorage=m,C.each=function(t){void 0===t&&(t=p);var e={variable:C._variable};m&&(e.localStorage=localStorage,e.sessionStorage=sessionStorage);for(var i in e)for(var n in e[i])if(n.indexOf("NSCheck:")>-1){var r=C({key:n.match(/NSCheck:(.*)/)[1],type:i});t(r)}},C.clean=function(){this.each(function(t){t.isOutdated()&&t.destroy()})},C.list=function(){this.each(function(t){c&&console.log(t.config.type,t.config.key,t.get())})},C.clean(),C}); | ||
//# sourceMappingURL=natty-storage.min.js.map |
{ | ||
"name": "natty-storage", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/Jias/natty-storage.git", |
@@ -13,3 +13,3 @@ # natty-storage | ||
* 隐身模式下,有些浏览器不支持`localStorage`,此时自动降级为`variable`模式。 | ||
* `gzip`后的只有`2.5K`。 | ||
* `gzip`后的只有`2K`。 | ||
@@ -156,2 +156,17 @@ ## Install | ||
## async[Set|Get|Has|Remove|Destroy] | ||
上面的`set`、`get`、`has`、`remove`、`destroy`方法都是同步的,同时还有一一对应的一套异步方法,`asyncSet`、`asyncSet`、`asyncHas`、`asyncRemove`、`asyncDestroy`,这些异步方法返回一个标准的`Promise`对象。 | ||
```js | ||
ls.asyncSet('foo', 'x').then(() => { | ||
ls.asyncGet().then(data => { | ||
// data 值为 {"foo":"x"} | ||
}) | ||
}) | ||
``` | ||
## storage.dump() | ||
@@ -174,3 +189,3 @@ | ||
清理掉浏览器`localStorage`中因超过`until`和`duration`的有效期而过期的数据。在`natty-storage`加载后,内部会自动执行一次。 | ||
清理掉因为`until`和`duration`的有效期而过期的数据。在`natty-storage`加载后,内部会自动执行一次。 | ||
@@ -177,0 +192,0 @@ 示例 |
@@ -145,3 +145,3 @@ import {extend, isPlainObject, noop, hasWindow, hasConsole} from './util' | ||
// instance.get('foo.bar') | ||
get(path) { | ||
get(path, fallbackValue) { | ||
if (!this._data) { | ||
@@ -163,3 +163,3 @@ this._lazyInit() | ||
return data | ||
return data !== UNDEFINED ? data : fallbackValue | ||
} | ||
@@ -241,3 +241,3 @@ | ||
// 添加异步方法 | ||
// 添加异步方法:asyncSet asyncGet asyncHas asyncRemove asyncDestroy | ||
const methodHasAsyncMode = ['set', 'get', 'has', 'remove', 'destroy'] | ||
@@ -466,2 +466,5 @@ for (let i=0, l=methodHasAsyncMode.length; i<l; i++) { | ||
// 内置处理一次过期数据 | ||
nattyStorage.clean() | ||
export default nattyStorage |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
99227
955
260
0