Skip to content

Instantly share code, notes, and snippets.

@olaferlandsen
Last active September 30, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olaferlandsen/69ffb5134984a2679ef175e2ced6cebd to your computer and use it in GitHub Desktop.
Save olaferlandsen/69ffb5134984a2679ef175e2ced6cebd to your computer and use it in GitHub Desktop.
A angular storage object to persistent data
/**
*
*/
$rootScope.storage = function () {
/**
* Set default API
**/
this.API = window.sessionStorage;
/**
* Set localStorage API
*/
this.enablePersistence = function() {
this.API = window.localStorage;
return this;
};
/**
* get current API
*/
this.getPersistence = function() {
return this.API;
};
/**
* Check if item is Empty.
* Note: If the item don't exists, return true
*
* Example:
* if ($scope.storage.isEmpty('item')) {
* console.log('Is empty!');
* }
*
*/
this.isEmpty = function (key) {
var mixedVar = this.API.getItem(key);
var undef
var key
var i
var len
var emptyValues = [undef, null, false, 0, '', '0']
for (i = 0, len = emptyValues.length; i < len; i++) {
if (mixedVar === emptyValues[i]) {
return true
}
}
if (typeof mixedVar === 'object') {
for (index in mixedVar) {
if (mixedVar.hasOwnProperty(index)) {
return false
}
}
return true
}
return false
};
/**
* Check if item exists
*
* Example:
*
* if ($scope.storage.exists('item')) {
* console.log('The item key exists on storage');
* }
*
* if ($scope.storage.exists(['a', 'b', 'c', 'd'])) {
* console.log('A + B + C + D exists on storage');
* }
*
* $scope.storage.exists({'existsA':'a', 'existsB':'b', 'existsC':'c', 'existsD':'d'});
* // output:
* // {'existsA':true, 'existsB':false, 'existsC':false, 'existsD':false}
*
*/
this.exists = function (nested) {
if (angular.isArray(nested)) {
for (var i = 0; i < nested.length; i++) {
if (this.API.getItem(nested[i]) === null) {
return false;
}
}
return true;
} else if(angular.isObject(nested)) {
var results = {};
for (index in nested) {
results[index] = this.API.getItem(nested[index]) !== null;
}
return results;
} else {
return this.API.getItem(nested) !== null;
}
};
/**
* Return all items
* Example:
*
* $scope.storage.all();
*/
this.all = function() {
return this.API || {};
};
/**
* Set a new item(s)
* Example:
*
* # Define only key(by default value is undefined)
* $scope.storage.set( 'a' ); // value is 'undefined'
*
* # Define key and value as int, float, object, null, string, array, boolean, function
* $scope.storage.set( 'a', 1 ); // value is integer
* $scope.storage.set( 'a', 1.2 ); // value is float
* $scope.storage.set( 'a', "1" ); // value is string
* $scope.storage.set( 'a', { a: 1 } ); // value is object
* $scope.storage.set( 'a', [1,2,3] ); // value is array
* $scope.storage.set( 'a', null ); // value is null
* $scope.storage.set( 'a', true ); // value is boolean
* $scope.storage.set( 'a', false ); // value is boolean
* $scope.storage.set( 'a', function(){}); // value is function
*
* # Store multiple items at same time with a object
* $scope.storage.set({
* a: 1,
* b: true,
* c: "hi",
* d: function() {},
* e: 0.2
* });
*/
this.set = function () {
try {
if (arguments.length == 0) {
throw "need key";
}
var key = arguments[0],
value = undefined;
if (arguments.length > 1) {
value = angular.toJson(arguments[1]);
}
if (angular.isObject(key)) {
for (index in key) {
this.API.setItem( index, angular.toJson(key[index]) );
}
return true;
} else if (!this.exists(key)) {
return this.API.setItem( key, angular.toJson(value) );
}
} catch (e) {
return false;
}
};
/**
* Get a item
* Example:
*
* # Get value from a item
* $scope.storage.get('a'); // return value from key 'a'
*
* # Get items form array
* $scope.storage.get(['a', 'b'])
* // output:
* // [1]
*
* # Return items from dictionary
* $scope.storage.get({
name : 'a',
country : 'b',
language: 'c'
* });
* // Output:
* // {name: 'Olaf', country: 'Chile', language: 'spanish'}
*/
this.get = function(key) {
if (angular.isObject(key)) {
var result = {};
for (index in key) {
result[index] = angular.fromJson(this.API.getItem(index));
}
return result;
} else if (angular.isArray(key)) {
var result = {};
for (var i = 0; i < key.length; i++) {
result[i] = angular.fromJson(this.API.getItem(index));
}
return result;
}
return angular.fromJson(this.API.getItem( key ));
};
/**
* Remove a item(s)
* Example:
*
* # Remove a item
* $scope.storage.remove('a');
*
* # Remove multiple items
* $scope.storage.remove(['a', 'b', 'c'])
*/
this.remove = function (key) {
if (!this.exists(key)) {return false;}
return this.API.removeItem(key)
};
if (arguments.length == 0) {
return this;
} else if (arguments.length == 1) {
if (angular.isArray(arguments[0]) || angular.isObject(arguments[0])) {
return this.exists(arguments[0])
}
return this.get( arguments[0] );
} else if (arguments.length > 1) {
return this.set( arguments[0], arguments[1] );
}
};
$rootScope.storage = $rootScope.storage();
@olaferlandsen
Copy link
Author

API:

  • exists( mixed )
  • set ( mixed )
  • remove ( mixed )
  • get ( mixed key )
  • all ( void )
  • isEmpty ( mixed key )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment