Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A angular-storage
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Auth0
  • angular-storage
  • Merge requests
  • !16

Prevent storage of null or undefined values

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/kpgarrod/master into master 10 years ago
  • Overview 2
  • Commits 4
  • Pipelines 0
  • Changes 4

Created by: kpgarrod

If you accidentally store a null or undefined value, localStorage will save it OK, but JSON.parse will blow up when you try to get it back, because 'undefined' is not a valid JSON token. This fix prevents undefined values from being stored and values being stored in undefined places.

Compare
  • master (base)

and
  • latest version
    309f266f
    4 commits, 2 years ago

4 files
+ 23
- 18

    Preferences

    File browser
    Compare changes
di‎st‎
angular-s‎torage.js‎ +12 -7
angular-sto‎rage.min.js‎ +1 -1
src/angularSt‎orage/services‎
stora‎ge.js‎ +2 -2
packag‎e.json‎ +8 -8
dist/angular-storage.js
+ 12
- 7
  • View file @ 309f266f


@@ -25,7 +25,7 @@ angular.module('angular-storage.internalStore', ['angular-storage.storage'])
} else {
return [this.namespace, key].join(this.delimiter);
}
}
};
@@ -35,19 +35,24 @@ angular.module('angular-storage.internalStore', ['angular-storage.storage'])
};
InternalStore.prototype.get = function(name) {
var obj = null;
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;
try {
obj = saved ? JSON.parse(saved) : null;
this.inMemoryCache[name] = obj;
} catch(e) {
this.remove(name);
}
return obj;
};
InternalStore.prototype.remove = function(name) {
this.inMemoryCache[name] = null;
storage.remove(this.getNamespacedKey(name));
}
};
return InternalStore;
@@ -56,10 +61,10 @@ angular.module('angular-storage.internalStore', ['angular-storage.storage'])
angular.module('angular-storage.storage', [])
.service('storage', ["$window", function($window) {
.service('storage', ["$window", "$injector", function($window, $injector) {
if ($window.localStorage) {
this.set = function(what, value) {
return $window.localStorage.setItem(what, value);
return (!what || !value) ? null : $window.localStorage.setItem(what, value);
};
this.get = function(what) {
return $window.localStorage.getItem(what);
@@ -70,7 +75,7 @@ angular.module('angular-storage.storage', [])
} else {
var $cookieStore = $injector.get('$cookieStore');
this.set = function(what, value) {
return $cookieStore.put(what, value);
return (!what || !value) ? null : $cookieStore.put(what, value);
};
this.get = function(what) {
return $cookieStore.get(what);
dist/angular-storage.min.js
+ 1
- 1
  • View file @ 309f266f

!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.internalStore",["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.internalStore"]).factory("store",["InternalStore",function(e){var t=new e;return t.getNamespacedStore=function(t,r){return new e(t,r)},t}])}();
\ No newline at end of file
!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.internalStore",["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){var r=null;if(t in this.inMemoryCache)return this.inMemoryCache[t];var n=e.get(this.getNamespacedKey(t));try{r=n?JSON.parse(n):null,this.inMemoryCache[t]=r}catch(o){this.remove(t)}return r},t.prototype.remove=function(t){this.inMemoryCache[t]=null,e.remove(this.getNamespacedKey(t))},t}]),angular.module("angular-storage.storage",[]).service("storage",["$window","$injector",function(e,t){if(e.localStorage)this.set=function(t,r){return t&&r?e.localStorage.setItem(t,r):null},this.get=function(t){return e.localStorage.getItem(t)},this.remove=function(t){return e.localStorage.removeItem(t)};else{var r=t.get("$cookieStore");this.set=function(e,t){return e&&t?r.put(e,t):null},this.get=function(e){return r.get(e)},this.remove=function(e){return r.remove(e)}}}]),angular.module("angular-storage.store",["angular-storage.internalStore"]).factory("store",["InternalStore",function(e){var t=new e;return t.getNamespacedStore=function(t,r){return new e(t,r)},t}])}();
\ No newline at end of file
src/angularStorage/services/storage.js
+ 2
- 2
  • View file @ 309f266f


@@ -2,7 +2,7 @@ angular.module('angular-storage.storage', [])
.service('storage', function($window, $injector) {
if ($window.localStorage) {
this.set = function(what, value) {
return $window.localStorage.setItem(what, value);
return (!what || !value) ? null : $window.localStorage.setItem(what, value);
};
this.get = function(what) {
return $window.localStorage.getItem(what);
@@ -13,7 +13,7 @@ angular.module('angular-storage.storage', [])
} else {
var $cookieStore = $injector.get('$cookieStore');
this.set = function(what, value) {
return $cookieStore.put(what, value);
return (!what || !value) ? null : $cookieStore.put(what, value);
};
this.get = function(what) {
return $cookieStore.get(what);
package.json
+ 8
- 8
  • View file @ 309f266f


@@ -7,24 +7,24 @@
},
"dependencies": {},
"devDependencies": {
"chai": "^1.9.1",
"chai-jquery": "^1.2.3",
"chai": "^1.10.0",
"chai-jquery": "^2.0.0",
"gulp": "^3.8.7",
"gulp-concat": "^2.3.4",
"gulp-ng-annotate": "^0.3.3",
"gulp-concat": "^2.4.2",
"gulp-ng-annotate": "^0.4.2",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"gulp-uglify": "^1.0.2",
"karma": "^0.12.22",
"karma-chai": "^0.1.0",
"karma-chai-jquery": "^1.0.0",
"karma-chrome-launcher": "^0.1.4",
"karma-chrome-launcher": "^0.1.7",
"karma-jasmine": "^0.1.5",
"karma-jquery": "^0.1.0",
"karma-mocha": "^0.1.8",
"karma-mocha": "^0.1.10",
"karma-mocha-reporter": "^0.3.1",
"karma-phantomjs-launcher": "^0.1.4",
"karma-sinon-chai": "^0.2.0",
"mocha": "^1.21.4",
"mocha": "^2.0.1",
"sinon": "^1.10.3",
"sinon-chai": "^2.5.0"
},
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
2
CLA Signed dependencies
2
CLA Signed dependencies
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: facebook/create-react-app!11323
Source branch: github/fork/kpgarrod/master

Menu

Explore Projects Groups Snippets