Skip to content

Default Value option for 'get' function. #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ myApp.controller('MainCtrl', function($scope, localStorageService) {
###get
Directly get a value from local storage.<br/>
If local storage is not supported, use cookies instead.<br/>
**Returns:** `value from local storage`
**Usage:** `localStorageService.get(key, defaultValue[optional])`
**Returns:** `value from local storage, defaultValue if no value is stored`
```js
myApp.controller('MainCtrl', function($scope, localStorageService) {
//...
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
],
"devDependencies": {
"angular": "~1.x",
"angular-mocks": "~1.2.1"
"angular-mocks": "~1.x"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"grunt-contrib-concat": "*",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-uglify": "*",
"grunt-karma": "latest",
"grunt-karma": "~0.11.1",
"karma": "~0.12.16",
"karma-jasmine": "~0.1.5",
"karma-coverage": "^0.2.6",
Expand Down
19 changes: 14 additions & 5 deletions src/angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,25 @@ angularLocalStorage.provider('localStorageService', function() {

// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {
var getFromLocalStorage = function (key, defaultValue) {

if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
}

return getFromCookies(key);
return getFromCookies(key, defaultValue);
}

var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
// angular.toJson will convert null to 'null', so a proper conversion is needed
// FIXME not a perfect solution, since a valid 'null' string can't be stored
if (!item || item === 'null') {
return null;
if (isUndefined(defaultValue)) {
return null;
} else {
return defaultValue;
}
}

try {
Expand Down Expand Up @@ -323,7 +327,7 @@ angularLocalStorage.provider('localStorageService', function() {

// Directly get a value from a cookie
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
var getFromCookies = function (key) {
var getFromCookies = function (key, defaultValue) {
if (!browserSupportsCookies) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
Expand All @@ -344,7 +348,12 @@ angularLocalStorage.provider('localStorageService', function() {
}
}
}
return null;

if (isUndefined(defaultValue)) {
return null;
} else {
return defaultValue;
}
};

var removeFromCookies = function (key) {
Expand Down
28 changes: 28 additions & 0 deletions test/spec/localStorageSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ describe('localStorageService', function() {
expectRemoving('ls.lorem.ipsum')
));

it('should be able to get with a default value', inject(function($rootScope, localStorageService) {
expect([1,2,3]).toEqual(localStorageService.get('property', [1,2,3]));
}));

it('should be able to get with a default value and honor stored value', inject(function($rootScope, localStorageService) {
localStorageService.set('property', 'hello');
expect('hello').toEqual(localStorageService.get('property', [1,2,3]));
}));

it('should be able to get with a default value and not store default', inject(function(localStorageService) {
expect([1,2,3]).toEqual(localStorageService.get('property', [1,2,3]));
expect(null).toEqual(localStorageService.get('property'));
}));

it('should be able to remove multiple items', inject(function($window, localStorageService) {
elmSpy = spyOn($window.localStorage, 'removeItem').andCallThrough();
localStorageService.remove('lorem.ipsum1', 'lorem.ipsum2', 'lorem.ipsum3');
Expand Down Expand Up @@ -600,6 +614,20 @@ describe('localStorageService', function() {
expect(localStorageService.get('cookieKey')).toEqual('cookieValue');
}));

it('should be able to get cookie with a default value', inject(function($rootScope, localStorageService) {
expect([1,2,3]).toEqual(localStorageService.get('cookieKey', [1,2,3]));
}));

it('should be able to get cookie with a default value and honor stored value', inject(function($rootScope, localStorageService) {
localStorageService.set('cookieKey', 'hello');
expect('hello').toEqual(localStorageService.get('cookieKey', [1,2,3]));
}));

it('should be able to get cookie with a default value and not store default', inject(function(localStorageService) {
expect([1,2,3]).toEqual(localStorageService.get('cookieKey', [1,2,3]));
expect(null).toEqual(localStorageService.get('cookieKey'));
}));

it('should be able to set individual cookie with expiry', function() {
inject(expectCookieExpiry(new Date().addDays(10)));
});
Expand Down