Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

backbone.localcache

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone.localcache - npm Package Compare versions

Comparing version
0.0.1
to
1.0.0
+10
-8
dist/amd/backbone.localcache.js

@@ -23,2 +23,6 @@ /*!

_flags: {
restoredFromCache: false
},
constructor: mixin('decorate', function (fn, attrs, options) {

@@ -70,7 +74,3 @@ // before

fetchAttrs: function () {
// noop until user defined
},
/* ---------------------------------------------------------------------------

@@ -113,2 +113,6 @@ * event handlers

restoredFromCache: function () {
return !this._flags.restoredFromCache;
},
_bootstrapAttrs: function (attrs) {

@@ -142,5 +146,3 @@ var cacheAttrs = this._loadAttrs();

if (_.isEmpty(cacheAttrs)) {
this.fetchAttrs();
}
this._flags.restoredFromCache = _.isEmpty(cacheAttrs);

@@ -194,2 +196,2 @@ return cacheAttrs.length === 1

});
});

@@ -462,2 +462,3 @@ (function (root, factory) {

return {
_flags: { restoredFromCache: false },
constructor: mixin('decorate', function (fn, attrs, options) {

@@ -497,4 +498,2 @@ // before

},
fetchAttrs: function () {
},
/* ---------------------------------------------------------------------------

@@ -529,2 +528,5 @@ * event handlers

* -------------------------------------------------------------------------*/
restoredFromCache: function () {
return !this._flags.restoredFromCache;
},
_bootstrapAttrs: function (attrs) {

@@ -550,5 +552,3 @@ var cacheAttrs = this._loadAttrs();

var cacheAttrs = this._getCacheAttrs();
if (_.isEmpty(cacheAttrs)) {
this.fetchAttrs();
}
this._flags.restoredFromCache = _.isEmpty(cacheAttrs);
return cacheAttrs.length === 1 ? cacheAttrs[0] : cacheAttrs;

@@ -555,0 +555,0 @@ },

@@ -1,1 +0,1 @@

!function(a,b){"function"==typeof define&&define.amd?define(["underscore"],function(c){return a.returnExportsGlobal=b(c)}):"object"==typeof exports?module.exports=b(require("underscore")):a.backboneLocalcache=b(a.underscore)}(this,function(a){var b,c,d,e;return b=function(a){var b=function(a,b){return Object.defineProperty(b,"__mixin",{enumerable:!1,writable:!0}),b.__mixin=a,b};return b}({}),function(a,b){e=function(){return"function"==typeof b?b():b}()}(this,function(){var a,b,c="",d=!1,e={PREFIX:"lscache-",SUFFIX:"-cacheexpiration",EXPIRY_RADIX:10,EXPIRY_UNITS:6e4,_supportsStorage:function(){var b="__lscachetest__",c=b;if(void 0!==a)return a;try{e._setItem(b,c),e._removeItem(b),a=!0}catch(d){a=!!e._isOutOfSpace(d)}return a},_isOutOfSpace:function(a){return!!(a&&"QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)},_supportsJSON:function(){return void 0===b&&(b=null!=window.JSON),b},_escapeRegExpSpecialCharacters:function(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")},_expirationKey:function(a){return a+e.SUFFIX},_currentTime:function(){return Math.floor((new Date).getTime()/e.EXPIRY_UNITS)},_getMaxDate:function(){return e._maxDate=e._maxDate||Math.floor(864e13/e.EXPIRY_UNITS),e._maxDate},_getItem:function(a){return localStorage.getItem(e.PREFIX+c+a)},_setItem:function(a,b){localStorage.removeItem(e.PREFIX+c+a),localStorage.setItem(e.PREFIX+c+a,b)},_removeItem:function(a){localStorage.removeItem(e.PREFIX+c+a)},_eachKey:function(a){for(var b=new RegExp("^"+e.PREFIX+e._escapeRegExpSpecialCharacters(c)+"(.*)"),d=localStorage.length-1;d>=0;--d){var f=localStorage.key(d);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(e.SUFFIX)<0&&a(f,e._expirationKey(f))}},_flushItem:function(a){var b=e._expirationKey(a);e._removeItem(a),e._removeItem(b)},_flushExpiredItem:function(a){var b=e._expirationKey(a),c=e._getItem(b);if(c){var d=parseInt(c,e.EXPIRY_RADIX);if(e._currentTime()>=d)return e._removeItem(a),e._removeItem(b),!0}},_warn:function(a,b){d&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))},set:function(a,b,c){if(e._supportsStorage()){if("string"!=typeof b){if(!e._supportsJSON())return;try{b=JSON.stringify(b)}catch(d){return}}try{e._setItem(a,b)}catch(d){if(!e._isOutOfSpace(d))return void e._warn("Could not add item with key '"+a+"'",d);var f,g=[];e._eachKey(function(a,b){var c=e._getItem(b);c=c?parseInt(c,e.EXPIRY_RADIX):e._getMaxDate(),g.push({key:a,size:(e._getItem(a)||"").length,expiration:c})}),g.sort(function(a,b){return b.expiration-a.expiration});for(var h=(b||"").length;g.length&&h>0;)f=g.pop(),e._warn("Cache is full, removing item with key '"+a+"'"),e._flushItem(f.key),h-=f.size;try{e._setItem(a,b)}catch(d){return void e._warn("Could not add item with key '"+a+"', perhaps it's too big?",d)}}c?e._setItem(e._expirationKey(a),(e._currentTime()+c).toString(e.EXPIRY_RADIX)):e._removeItem(e._expirationKey(a))}},get:function(a){if(!e._supportsStorage())return null;if(e._flushExpiredItem(a))return null;var b=e._getItem(a);if(!b||!e._supportsJSON())return b;try{return JSON.parse(b)}catch(c){return b}},remove:function(a){e._supportsStorage()&&e._flushItem(a)},supported:function(){return e._supportsStorage()},flush:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushItem(a)})},flushExpired:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushExpiredItem(a)})},setBucket:function(a){c=a},resetBucket:function(){c=""},enableWarnings:function(a){d=a}};return e}),c=function(a){var b=e,c=function(a){a=a||{},this.namespace=a.namespace,this.bucket=a.bucket,this.ttl=a.ttl};return c.prototype.bucketId=function(){var a="";return this.namespace&&(a+=this.namespace+"-"),this.bucket&&(a+=this.bucket+"-"),a},c.prototype.get=function(){return b.setBucket(this.bucketId()),b.get.apply(b,arguments)},c.prototype.set=function(a,c,d){return b.setBucket(this.bucketId()),b.set.call(b,a,c,d||this.ttl)},c.prototype.remove=function(){return b.setBucket(this.bucketId()),b.remove.apply(b,arguments)},c.prototype.flush=function(){return b.setBucket(this.bucketId()),b.flush.apply(b,arguments)},c.prototype.flushExpired=function(){return b.setBucket(this.bucketId()),b.flushExpired.apply(b,arguments)},c.prototype.all=function(){var a={};return b.setBucket(this.bucketId()),b._eachKey(function(c,d){a[c]=b._getItem(c)}),a},c}({}),d=function(d){var e=a,f=b,g=c;return{constructor:f("decorate",function(a,b,c){if(!this.cacheName)throw new Error("Required attribute `cacheName` missing.");this._createCache(),b=this._bootstrapAttrs(b),c=c||{},c.meta=this._bootstrapMeta(c.meta),a.call(this,b,c),this.models?e.each(this.models,this._onAddModel,this):e.isEmpty(this.attributes)||this._onAddModel(this),this._addCacheListeners()}),getCacheOptions:function(){return e.extend(this.getCacheDefaults(),this.cacheOptions||{})},getCacheDefaults:function(){return{bucket:this.getBucketName()}},getBucketName:function(){return this.cacheName},fetchAttrs:function(){},_onAddModel:function(a,b,c){c&&c.ignoreCache||this._setCacheEntry(a)},_onRemoveModel:function(a,b,c){c&&c.ignoreCache||this.cache.remove(a.id)},_onChangeModel:function(a,b){b&&b.ignoreCache||this._setCacheEntry(a)},_onResetCollection:function(a,b){b&&b.ignoreCache||this.cache.flush()},_onChangeMeta:function(a){this.cache.set("__meta",a.attributes)},_bootstrapAttrs:function(a){var b=this._loadAttrs(),c=this.modelId||this.idAttribute,d=function(a){return a[c]};return a?e.isEmpty(b)?a:e.isArray(a)&&e.isArray(b)?e.uniq(e.union(a,b),d):!e.isArray(a)&&e.isArray(b)?e.uniq([a].concat(b),d):e.extend(b,a):b},_loadAttrs:function(){var a=this._getCacheAttrs();return e.isEmpty(a)&&this.fetchAttrs(),1===a.length?a[0]:a},_getCacheAttrs:function(){return e.chain(this.cache.all()).filter(this._filterCacheAttrs.bind(this)).map(JSON.parse.bind(JSON)).value()},_filterCacheAttrs:function(a,b){return"__meta"!==b},_bootstrapMeta:function(a){return e.extend(this._loadMeta()||{},a||{})},_loadMeta:function(){return this.cache.get("__meta")},_createCache:function(){this.cache=new g(this.getCacheOptions()),this.cache.flushExpired()},_addCacheListeners:function(){this.on("add",this._onAddModel,this),this.on("remove",this._onRemoveModel,this),this.on("change",this._onChangeModel,this),this.on("reset",this._onResetCollection,this),this.on("meta:change",this._onChangeMeta,this)},_setCacheEntry:function(a){var b=a.cacheKey||a.id;b&&this.cache.set(b,a.attributes)}}}({})});
!function(a,b){"function"==typeof define&&define.amd?define(["underscore"],function(c){return a.returnExportsGlobal=b(c)}):"object"==typeof exports?module.exports=b(require("underscore")):a.backboneLocalcache=b(a.underscore)}(this,function(a){var b,c,d,e;return b=function(a){var b=function(a,b){return Object.defineProperty(b,"__mixin",{enumerable:!1,writable:!0}),b.__mixin=a,b};return b}({}),function(a,b){e=function(){return"function"==typeof b?b():b}()}(this,function(){var a,b,c="",d=!1,e={PREFIX:"lscache-",SUFFIX:"-cacheexpiration",EXPIRY_RADIX:10,EXPIRY_UNITS:6e4,_supportsStorage:function(){var b="__lscachetest__",c=b;if(void 0!==a)return a;try{e._setItem(b,c),e._removeItem(b),a=!0}catch(d){a=!!e._isOutOfSpace(d)}return a},_isOutOfSpace:function(a){return!!(a&&"QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)},_supportsJSON:function(){return void 0===b&&(b=null!=window.JSON),b},_escapeRegExpSpecialCharacters:function(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")},_expirationKey:function(a){return a+e.SUFFIX},_currentTime:function(){return Math.floor((new Date).getTime()/e.EXPIRY_UNITS)},_getMaxDate:function(){return e._maxDate=e._maxDate||Math.floor(864e13/e.EXPIRY_UNITS),e._maxDate},_getItem:function(a){return localStorage.getItem(e.PREFIX+c+a)},_setItem:function(a,b){localStorage.removeItem(e.PREFIX+c+a),localStorage.setItem(e.PREFIX+c+a,b)},_removeItem:function(a){localStorage.removeItem(e.PREFIX+c+a)},_eachKey:function(a){for(var b=new RegExp("^"+e.PREFIX+e._escapeRegExpSpecialCharacters(c)+"(.*)"),d=localStorage.length-1;d>=0;--d){var f=localStorage.key(d);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(e.SUFFIX)<0&&a(f,e._expirationKey(f))}},_flushItem:function(a){var b=e._expirationKey(a);e._removeItem(a),e._removeItem(b)},_flushExpiredItem:function(a){var b=e._expirationKey(a),c=e._getItem(b);if(c){var d=parseInt(c,e.EXPIRY_RADIX);if(e._currentTime()>=d)return e._removeItem(a),e._removeItem(b),!0}},_warn:function(a,b){d&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))},set:function(a,b,c){if(e._supportsStorage()){if("string"!=typeof b){if(!e._supportsJSON())return;try{b=JSON.stringify(b)}catch(d){return}}try{e._setItem(a,b)}catch(d){if(!e._isOutOfSpace(d))return void e._warn("Could not add item with key '"+a+"'",d);var f,g=[];e._eachKey(function(a,b){var c=e._getItem(b);c=c?parseInt(c,e.EXPIRY_RADIX):e._getMaxDate(),g.push({key:a,size:(e._getItem(a)||"").length,expiration:c})}),g.sort(function(a,b){return b.expiration-a.expiration});for(var h=(b||"").length;g.length&&h>0;)f=g.pop(),e._warn("Cache is full, removing item with key '"+a+"'"),e._flushItem(f.key),h-=f.size;try{e._setItem(a,b)}catch(d){return void e._warn("Could not add item with key '"+a+"', perhaps it's too big?",d)}}c?e._setItem(e._expirationKey(a),(e._currentTime()+c).toString(e.EXPIRY_RADIX)):e._removeItem(e._expirationKey(a))}},get:function(a){if(!e._supportsStorage())return null;if(e._flushExpiredItem(a))return null;var b=e._getItem(a);if(!b||!e._supportsJSON())return b;try{return JSON.parse(b)}catch(c){return b}},remove:function(a){e._supportsStorage()&&e._flushItem(a)},supported:function(){return e._supportsStorage()},flush:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushItem(a)})},flushExpired:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushExpiredItem(a)})},setBucket:function(a){c=a},resetBucket:function(){c=""},enableWarnings:function(a){d=a}};return e}),c=function(a){var b=e,c=function(a){a=a||{},this.namespace=a.namespace,this.bucket=a.bucket,this.ttl=a.ttl};return c.prototype.bucketId=function(){var a="";return this.namespace&&(a+=this.namespace+"-"),this.bucket&&(a+=this.bucket+"-"),a},c.prototype.get=function(){return b.setBucket(this.bucketId()),b.get.apply(b,arguments)},c.prototype.set=function(a,c,d){return b.setBucket(this.bucketId()),b.set.call(b,a,c,d||this.ttl)},c.prototype.remove=function(){return b.setBucket(this.bucketId()),b.remove.apply(b,arguments)},c.prototype.flush=function(){return b.setBucket(this.bucketId()),b.flush.apply(b,arguments)},c.prototype.flushExpired=function(){return b.setBucket(this.bucketId()),b.flushExpired.apply(b,arguments)},c.prototype.all=function(){var a={};return b.setBucket(this.bucketId()),b._eachKey(function(c,d){a[c]=b._getItem(c)}),a},c}({}),d=function(d){var e=a,f=b,g=c;return{_flags:{restoredFromCache:!1},constructor:f("decorate",function(a,b,c){if(!this.cacheName)throw new Error("Required attribute `cacheName` missing.");this._createCache(),b=this._bootstrapAttrs(b),c=c||{},c.meta=this._bootstrapMeta(c.meta),a.call(this,b,c),this.models?e.each(this.models,this._onAddModel,this):e.isEmpty(this.attributes)||this._onAddModel(this),this._addCacheListeners()}),getCacheOptions:function(){return e.extend(this.getCacheDefaults(),this.cacheOptions||{})},getCacheDefaults:function(){return{bucket:this.getBucketName()}},getBucketName:function(){return this.cacheName},_onAddModel:function(a,b,c){c&&c.ignoreCache||this._setCacheEntry(a)},_onRemoveModel:function(a,b,c){c&&c.ignoreCache||this.cache.remove(a.id)},_onChangeModel:function(a,b){b&&b.ignoreCache||this._setCacheEntry(a)},_onResetCollection:function(a,b){b&&b.ignoreCache||this.cache.flush()},_onChangeMeta:function(a){this.cache.set("__meta",a.attributes)},restoredFromCache:function(){return!this._flags.restoredFromCache},_bootstrapAttrs:function(a){var b=this._loadAttrs(),c=this.modelId||this.idAttribute,d=function(a){return a[c]};return a?e.isEmpty(b)?a:e.isArray(a)&&e.isArray(b)?e.uniq(e.union(a,b),d):!e.isArray(a)&&e.isArray(b)?e.uniq([a].concat(b),d):e.extend(b,a):b},_loadAttrs:function(){var a=this._getCacheAttrs();return this._flags.restoredFromCache=e.isEmpty(a),1===a.length?a[0]:a},_getCacheAttrs:function(){return e.chain(this.cache.all()).filter(this._filterCacheAttrs.bind(this)).map(JSON.parse.bind(JSON)).value()},_filterCacheAttrs:function(a,b){return"__meta"!==b},_bootstrapMeta:function(a){return e.extend(this._loadMeta()||{},a||{})},_loadMeta:function(){return this.cache.get("__meta")},_createCache:function(){this.cache=new g(this.getCacheOptions()),this.cache.flushExpired()},_addCacheListeners:function(){this.on("add",this._onAddModel,this),this.on("remove",this._onRemoveModel,this),this.on("change",this._onChangeModel,this),this.on("reset",this._onResetCollection,this),this.on("meta:change",this._onChangeMeta,this)},_setCacheEntry:function(a){var b=a.cacheKey||a.id;b&&this.cache.set(b,a.attributes)}}}({})});

@@ -23,2 +23,6 @@ /*!

_flags: {
restoredFromCache: false
},
constructor: mixin('decorate', function (fn, attrs, options) {

@@ -70,7 +74,3 @@ // before

fetchAttrs: function () {
// noop until user defined
},
/* ---------------------------------------------------------------------------

@@ -113,2 +113,6 @@ * event handlers

restoredFromCache: function () {
return !this._flags.restoredFromCache;
},
_bootstrapAttrs: function (attrs) {

@@ -142,5 +146,3 @@ var cacheAttrs = this._loadAttrs();

if (_.isEmpty(cacheAttrs)) {
this.fetchAttrs();
}
this._flags.restoredFromCache = _.isEmpty(cacheAttrs);

@@ -194,1 +196,2 @@ return cacheAttrs.length === 1

@@ -5,3 +5,3 @@ {

"author": "Jarid Margolin <jarid@firstopinionapp.com>",
"version": "0.0.1",
"version": "1.0.0",
"homepage": "https://github.com/firstopinion/backbone.localcache",

@@ -36,2 +36,2 @@ "repository": {

"license": "MIT"
}
}

@@ -23,2 +23,6 @@ /*!

_flags: {
restoredFromCache: false
},
constructor: mixin('decorate', function (fn, attrs, options) {

@@ -70,7 +74,3 @@ // before

fetchAttrs: function () {
// noop until user defined
},
/* ---------------------------------------------------------------------------

@@ -113,2 +113,6 @@ * event handlers

restoredFromCache: function () {
return !this._flags.restoredFromCache;
},
_bootstrapAttrs: function (attrs) {

@@ -142,5 +146,3 @@ var cacheAttrs = this._loadAttrs();

if (_.isEmpty(cacheAttrs)) {
this.fetchAttrs();
}
this._flags.restoredFromCache = _.isEmpty(cacheAttrs);

@@ -194,2 +196,2 @@ return cacheAttrs.length === 1

});
});

@@ -28,3 +28,3 @@ /*!

assert.isFunction(backboneLocalcache.getBucketName);
assert.isFunction(backboneLocalcache.fetchAttrs);
assert.isFunction(backboneLocalcache.restoredFromCache);
});

@@ -31,0 +31,0 @@

@@ -28,3 +28,3 @@ /*!

assert.isFunction(backboneLocalcache.getBucketName);
assert.isFunction(backboneLocalcache.fetchAttrs);
assert.isFunction(backboneLocalcache.restoredFromCache);
});

@@ -31,0 +31,0 @@