angular-storage
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "a0-angular-storage", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ { |
@@ -13,2 +13,47 @@ (function() { | ||
angular.module('angular-storage.store', ['angular-storage.storage']) | ||
.factory('InternalStore', ["storage", function(storage) { | ||
function InternalStore(namespace, delimiter) { | ||
this.namespace = namespace || null; | ||
this.delimiter = delimiter || '.'; | ||
this.inMemoryCache = {}; | ||
} | ||
InternalStore.prototype.getNamespacedKey = function(key) { | ||
if (!this.namespace) { | ||
return key; | ||
} else { | ||
return [this.namespace, key].join(this.delimiter); | ||
} | ||
} | ||
InternalStore.prototype.set = function(name, elem) { | ||
this.inMemoryCache[name] = elem; | ||
storage.set(this.getNamespacedKey(name), JSON.stringify(elem)); | ||
}; | ||
InternalStore.prototype.get = function(name) { | ||
if (name in this.inMemoryCache) { | ||
return this.inMemoryCache[name]; | ||
} | ||
var saved = storage.get(this.getNamespacedKey(name)); | ||
var obj = saved ? JSON.parse(saved) : null; | ||
this.inMemoryCache[name] = obj; | ||
return obj; | ||
}; | ||
InternalStore.prototype.remove = function(name) { | ||
this.inMemoryCache[name] = null; | ||
storage.remove(this.getNamespacedKey(name)); | ||
} | ||
return InternalStore; | ||
}]); | ||
angular.module('angular-storage.storage', []) | ||
@@ -42,27 +87,12 @@ .service('storage', ["$window", function($window) { | ||
angular.module('angular-storage.store', ['angular-storage.storage']) | ||
.service('store', ["storage", function(storage) { | ||
.factory('store', ["storage", "InternalStore", function(storage, InternalStore) { | ||
this.inMemoryCache = {}; | ||
var store = new InternalStore(); | ||
store.getStoreFor = function(namespace, key) { | ||
return new InternalStore(namespace, key); | ||
} | ||
this.set = function(name, elem) { | ||
this.inMemoryCache[name] = elem; | ||
storage.set(name, JSON.stringify(elem)); | ||
}; | ||
return store; | ||
this.get = function(name) { | ||
if (name in this.inMemoryCache) { | ||
return this.inMemoryCache[name]; | ||
} | ||
var saved = storage.get(name); | ||
var obj = saved ? JSON.parse(saved) : null; | ||
this.inMemoryCache[name] = obj; | ||
return obj; | ||
}; | ||
this.remove = function(name) { | ||
this.inMemoryCache[name] = null; | ||
storage.remove(name); | ||
} | ||
}]); | ||
@@ -69,0 +99,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.storage",[]).service("storage",["$window",function(e){if(e.localStorage)this.set=function(t,r){return e.localStorage.setItem(t,r)},this.get=function(t){return e.localStorage.getItem(t)},this.remove=function(t){return e.localStorage.removeItem(t)};else{var t=$injector.get("$cookieStore");this.set=function(e,r){return t.put(e,r)},this.get=function(e){return t.get(e)},this.remove=function(e){return t.remove(e)}}}]),angular.module("angular-storage.store",["angular-storage.storage"]).service("store",["storage",function(e){this.inMemoryCache={},this.set=function(t,r){this.inMemoryCache[t]=r,e.set(t,JSON.stringify(r))},this.get=function(t){if(t in this.inMemoryCache)return this.inMemoryCache[t];var r=e.get(t),n=r?JSON.parse(r):null;return this.inMemoryCache[t]=n,n},this.remove=function(t){this.inMemoryCache[t]=null,e.remove(t)}}])}(); | ||
!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.store",["angular-storage.storage"]).factory("InternalStore",["storage",function(e){function t(e,t){this.namespace=e||null,this.delimiter=t||".",this.inMemoryCache={}}return t.prototype.getNamespacedKey=function(e){return this.namespace?[this.namespace,e].join(this.delimiter):e},t.prototype.set=function(t,r){this.inMemoryCache[t]=r,e.set(this.getNamespacedKey(t),JSON.stringify(r))},t.prototype.get=function(t){if(t in this.inMemoryCache)return this.inMemoryCache[t];var r=e.get(this.getNamespacedKey(t)),n=r?JSON.parse(r):null;return this.inMemoryCache[t]=n,n},t.prototype.remove=function(t){this.inMemoryCache[t]=null,e.remove(this.getNamespacedKey(t))},t}]),angular.module("angular-storage.storage",[]).service("storage",["$window",function(e){if(e.localStorage)this.set=function(t,r){return e.localStorage.setItem(t,r)},this.get=function(t){return e.localStorage.getItem(t)},this.remove=function(t){return e.localStorage.removeItem(t)};else{var t=$injector.get("$cookieStore");this.set=function(e,r){return t.put(e,r)},this.get=function(e){return t.get(e)},this.remove=function(e){return t.remove(e)}}}]),angular.module("angular-storage.store",["angular-storage.storage"]).factory("store",["storage","InternalStore",function(e,t){var r=new t;return r.getStoreFor=function(e,r){return new t(e,r)},r}])}(); |
0.0.5 / 2014-10-08 | ||
================== | ||
* Added namespaced stores | ||
0.0.4 / 2014-10-07 | ||
@@ -3,0 +8,0 @@ ================== |
{ | ||
"name": "angular-storage", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Martin Gontovnikas", |
@@ -51,2 +51,33 @@ # angular-storage | ||
## Namespaced Storages | ||
You can also create namespaced storages if you want | ||
````js | ||
angular.module('app', ['angular-storage']) | ||
.factory('Auth0Store', function(store) { | ||
reutrn store.getNamespacedStore('auth0'); | ||
}) | ||
.controller('Controller', function(Auth0Store) { | ||
var myObj = { | ||
name: 'mgonto' | ||
}; | ||
// This will be saved in localStorage as auth0.obj | ||
Auth0Store.set('obj', myObj); | ||
// This will look for auth0.obj | ||
var myNewObject = Auth0Store.get('obj'); | ||
angular.equals(myNewObject, myObj); // return true | ||
Auth0Store.remove('obj'); | ||
store.set('number', 2); | ||
typeof(store.get('number')) === 'number' | ||
}); | ||
```` | ||
## API | ||
@@ -66,2 +97,6 @@ | ||
### store.getNamespacedStore(namespace, delimiter) | ||
Returns a new `store` service that will use the `nanespace` and `delimiter` when saving and getting values like the following `namespace[delimiter]key`. For example `auth0.object` considering `auth0` as `namespace` and `.` as a `delimiter` | ||
## Usages | ||
@@ -68,0 +103,0 @@ |
@@ -1,28 +0,13 @@ | ||
angular.module('angular-storage.store', ['angular-storage.storage']) | ||
.service('store', function(storage) { | ||
angular.module('angular-storage.store', ['angular-storage.internalStore']) | ||
.factory('store', function(InternalStore) { | ||
this.inMemoryCache = {}; | ||
var store = new InternalStore(); | ||
store.getNamespacedStore = function(namespace, key) { | ||
return new InternalStore(namespace, key); | ||
} | ||
this.set = function(name, elem) { | ||
this.inMemoryCache[name] = elem; | ||
storage.set(name, JSON.stringify(elem)); | ||
}; | ||
return store; | ||
this.get = function(name) { | ||
if (name in this.inMemoryCache) { | ||
return this.inMemoryCache[name]; | ||
} | ||
var saved = storage.get(name); | ||
var obj = saved ? JSON.parse(saved) : null; | ||
this.inMemoryCache[name] = obj; | ||
return obj; | ||
}; | ||
this.remove = function(name) { | ||
this.inMemoryCache[name] = null; | ||
storage.remove(name); | ||
} | ||
}); | ||
@@ -60,1 +60,68 @@ 'use strict'; | ||
}); | ||
describe('angularStorage new namespaced store', function() { | ||
beforeEach(function() { | ||
module('angular-storage.store'); | ||
}); | ||
var newStore = null; | ||
beforeEach(inject(function(store) { | ||
newStore = store.getNamespacedStore('auth0'); | ||
})); | ||
it('should save items correctly', inject(function($window) { | ||
var value = 1; | ||
newStore.set('myCoolValue', value); | ||
expect(newStore.get('myCoolValue')).to.equal(value); | ||
expect($window.localStorage.getItem('auth0.myCoolValue')).to.exist; | ||
expect($window.localStorage.getItem('myCoolValue')).to.not.exist; | ||
})); | ||
it('should delete items correctly from localStorage', function() { | ||
var value = 1; | ||
newStore.set('gonto', value); | ||
expect(newStore.get('gonto')).to.equal(value); | ||
newStore.remove('gonto'); | ||
expect(newStore.get('gonto')).to.not.exist; | ||
}); | ||
it('should save objects correctly', function() { | ||
var value = { | ||
gonto: 'hola' | ||
}; | ||
newStore.set('gonto', value); | ||
expect(newStore.get('gonto')).to.eql(value); | ||
}); | ||
it('should save objects correctly', function() { | ||
var value = { | ||
gonto: 'hola' | ||
}; | ||
newStore.set('gonto', value); | ||
expect(newStore.get('gonto')).to.eql(value); | ||
}); | ||
it('should save and objects correctly without cache', function() { | ||
var value = { | ||
gonto: 'hola' | ||
}; | ||
newStore.set('gonto', value); | ||
newStore.inMemoryCache = {}; | ||
expect(newStore.get('gonto')).to.eql(value); | ||
expect(newStore.get('gonto')).not.to.equal(value); | ||
}); | ||
it('should save and objects correctly without cache', function() { | ||
var value = { | ||
gonto: 'hola' | ||
}; | ||
newStore.set('gonto', value); | ||
newStore.inMemoryCache = {}; | ||
expect(newStore.get('gonto')).to.eql(value); | ||
expect(newStore.get('gonto')).not.to.equal(value); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26048
24
543
131