angular-storage
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -13,16 +13,15 @@ (function() { | ||
angular.module('angular-storage.cookieStorage', []) | ||
.service('cookieStorage', ["$injector", function ($injector) { | ||
var $cookieStore = $injector.get('$cookieStore'); | ||
angular.module('angular-storage.cookieStorage', ['ngCookies']) | ||
.service('cookieStorage', ["$cookies", function ($cookies) { | ||
this.set = function (what, value) { | ||
return $cookieStore.put(what, value); | ||
return $cookies.put(what, value); | ||
}; | ||
this.get = function (what) { | ||
return $cookieStore.get(what); | ||
return $cookies.get(what); | ||
}; | ||
this.remove = function (what) { | ||
return $cookieStore.remove(what); | ||
return $cookies.remove(what); | ||
}; | ||
@@ -188,2 +187,2 @@ }]); | ||
}()); | ||
}()); |
{ | ||
"name": "angular-storage", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -7,3 +7,3 @@ # angular-storage | ||
* Uses **`localStorage` by default but if it's not available, it uses `ngCookies`**. | ||
* Uses **`localStorage` or `sessionStorage` by default but if it's not available, it uses `ngCookies`**. | ||
* Lets you **save JS Objects** | ||
@@ -77,4 +77,33 @@ * If **you save a `Number`, you get a `Number`**, not a String | ||
## Changing storage type | ||
```js | ||
angular.module('app', ['angular-storage']) | ||
.config(function(storeProvider) { | ||
// Store defaults to localStorage but we can set sessionStorage or cookieStorage. | ||
storeProvider.setStore('sessionStorage'); | ||
}) | ||
.controller('Controller', function(store) { | ||
var myObj = { | ||
name: 'mgonto' | ||
}; | ||
// This will be saved in sessionStorage as obj | ||
store.set('obj', myObj); | ||
// This will look for obj in sessionStorage | ||
var myNewObject = store.get('obj'); | ||
angular.equals(myNewObject, myObj); // return true | ||
}); | ||
``` | ||
## API | ||
### storeProvider.setStore(storageName) | ||
Sets the underlying store for the `store` service. It can be `localStorage`, `sessionStorage` or `cookieStorage`. Defaults to `localStorage` | ||
### store.set(name, value) | ||
@@ -81,0 +110,0 @@ |
angular.module('angular-storage.cookieStorage', []) | ||
.service('cookieStorage', function ($injector) { | ||
var $cookieStore = $injector.get('$cookieStore'); | ||
.service('cookieStorage', ["$cookies", function ($cookies) { | ||
this.set = function (what, value) { | ||
return $cookieStore.put(what, value); | ||
return $cookies.put(what, value); | ||
}; | ||
this.get = function (what) { | ||
return $cookieStore.get(what); | ||
return $cookies.get(what); | ||
}; | ||
this.remove = function (what) { | ||
return $cookieStore.remove(what); | ||
return $cookies.remove(what); | ||
}; | ||
}); | ||
}]); |
@@ -25,2 +25,6 @@ angular.module('angular-storage.localStorage', ['angular-storage.cookieStorage']) | ||
}; | ||
this.clear = function () { | ||
$window.localStorage.clear(); | ||
}; | ||
} else { | ||
@@ -27,0 +31,0 @@ var cookieStorage = $injector.get('cookieStorage'); |
@@ -101,6 +101,19 @@ 'use strict'; | ||
var provider, windowMock, $cookieStore; | ||
var provider, windowMock, $cookies; | ||
var mockCookieStore = {}; | ||
beforeEach(function() { | ||
module('ngCookies', 'angular-storage.store', function(storeProvider, $provide) { | ||
// decorator to mock the methods of the cookieStore | ||
$provide.decorator('$cookies', function ($delegate) { | ||
$delegate.put = function (key, value) { | ||
mockCookieStore[key] = value; | ||
}; | ||
$delegate.get = function (key) { | ||
return mockCookieStore[key]; | ||
}; | ||
return $delegate; | ||
}); | ||
provider = storeProvider; | ||
@@ -114,4 +127,4 @@ provider.setStore('sessionStorage'); | ||
beforeEach(inject(function( _$cookieStore_) { | ||
$cookieStore = _$cookieStore_; | ||
beforeEach(inject(function( _$cookies_) { | ||
$cookies = _$cookies_; | ||
})); | ||
@@ -124,3 +137,3 @@ | ||
expect(store.get('gonto123')).to.equal(value); | ||
expect($cookieStore.get('gonto123')).to.equal(JSON.stringify(value)); | ||
expect($cookies.get('gonto123')).to.equal(JSON.stringify(value)); | ||
})); | ||
@@ -153,3 +166,3 @@ }); | ||
var provider; | ||
var $cookieStore; | ||
var $cookies; | ||
@@ -163,4 +176,4 @@ beforeEach(function() { | ||
beforeEach(inject(function( _$cookieStore_) { | ||
$cookieStore = _$cookieStore_; | ||
beforeEach(inject(function( _$cookies_) { | ||
$cookies = _$cookies_; | ||
})); | ||
@@ -173,3 +186,3 @@ | ||
expect(store.get('gonto')).to.equal(value); | ||
expect($cookieStore.get('gonto')).to.equal(JSON.stringify(value)); | ||
expect($cookies.get('gonto')).to.equal(JSON.stringify(value)); | ||
})); | ||
@@ -246,14 +259,30 @@ }); | ||
var windowMock, $cookieStore; | ||
var windowMock, $cookies; | ||
var mockCookieStore = {}; | ||
/* provide a mock for $window where localStorage is not defined */ | ||
beforeEach(module('ngCookies', 'angular-storage.store', function ($provide) { | ||
windowMock = { localStorage: undefined }; | ||
$provide.value('$window', windowMock); | ||
})); | ||
/* provide a mock for $window where localStorage is not defined */ | ||
beforeEach(module('ngCookies', 'angular-storage.store', function ($provide) { | ||
beforeEach(inject(function( _$cookieStore_) { | ||
$cookieStore = _$cookieStore_; | ||
})); | ||
// decorator to mock the methods of the cookieStore | ||
$provide.decorator('$cookies', function ($delegate) { | ||
$delegate.put = function (key, value) { | ||
mockCookieStore[key] = value; | ||
}; | ||
$delegate.get = function (key) { | ||
return mockCookieStore[key]; | ||
}; | ||
$delegate.remove = function (key) { | ||
delete mockCookieStore[key]; | ||
}; | ||
return $delegate; | ||
}); | ||
windowMock = { localStorage: undefined }; | ||
$provide.value('$window', windowMock); | ||
})); | ||
beforeEach(inject(function (_$cookies_) { | ||
$cookies = _$cookies_; | ||
})); | ||
it('should save items correctly in localStorage', inject(function(store) { | ||
@@ -263,3 +292,3 @@ var value = 1; | ||
expect(store.get('gonto')).to.equal(value); //this line asserts that value was saved by our service | ||
expect($cookieStore.get('gonto')).to.equal(JSON.stringify(value)); //this line asserts that cookie store was used | ||
expect($cookies.get('gonto')).to.equal(JSON.stringify(value)); //this line asserts that cookie store was used | ||
})); | ||
@@ -271,3 +300,3 @@ | ||
expect(store.get('gonto')).to.equal(null); | ||
expect($cookieStore.get('gonto')).to.equal(JSON.stringify(null)); | ||
expect($cookies.get('gonto')).to.equal(JSON.stringify(null)); | ||
})); | ||
@@ -279,3 +308,3 @@ | ||
expect(store.get('gonto')).to.equal(undefined); | ||
expect($cookieStore.get('gonto')).to.equal(JSON.stringify(undefined)); | ||
expect($cookies.get('gonto')).to.equal(JSON.stringify(undefined)); | ||
})); | ||
@@ -289,3 +318,3 @@ | ||
expect(store.get('gonto')).to.not.exist; | ||
expect($cookieStore.get('gonto')).to.not.exist; | ||
expect($cookies.get('gonto')).to.not.exist; | ||
})); | ||
@@ -307,3 +336,3 @@ | ||
expect(store.get('gonto')).to.eql(value); | ||
expect($cookieStore.get('gonto')).to.equal(JSON.stringify(value)); | ||
expect($cookies.get('gonto')).to.equal(JSON.stringify(value)); | ||
})); | ||
@@ -329,3 +358,3 @@ | ||
expect(store.get('gonto')).not.to.equal(value); | ||
expect($cookieStore.get('gonto')).to.eql(JSON.stringify(value)); | ||
expect($cookies.get('gonto')).to.eql(JSON.stringify(value)); | ||
@@ -411,6 +440,6 @@ })); | ||
describe('with param storage', function () { | ||
var $cookieStore; | ||
var $cookies; | ||
beforeEach(inject(function( _$cookieStore_) { | ||
$cookieStore = _$cookieStore_; | ||
beforeEach(inject(function( _$cookies_) { | ||
$cookies = _$cookies_; | ||
})); | ||
@@ -446,3 +475,3 @@ | ||
expect(cookieStore.get('wayne')).to.equal(value); | ||
expect($cookieStore.get('cc.wayne')).to.equal(JSON.stringify(value)); | ||
expect($cookies.get('cc.wayne')).to.equal(JSON.stringify(value)); | ||
})); | ||
@@ -449,0 +478,0 @@ }); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
49813
1077
158
1