Skip to content

Instantly share code, notes, and snippets.

@adardesign
Forked from anonymous/adrma.storage.js
Last active August 29, 2015 14:01
Show Gist options
  • Save adardesign/121763f2f0f2d191b6ce to your computer and use it in GitHub Desktop.
Save adardesign/121763f2f0f2d191b6ce to your computer and use it in GitHub Desktop.
adrma = window.adrma || {};
adrma.storage = {
supported: function() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
},
get: function(type, key, obj) {
if (!this.isSupported) {
return;
}
var item = window[type + "Storage"].getItem(key);
if (item && obj) {
return JSON.parse(item);
}
return item;
},
set: function(type, key, val) {
if (!this.isSupported) {
return;
}
if ($.isPlainObject(val) || $.isArray(val)) {
val = JSON.stringify(val);
}
try {
window[type + "Storage"].setItem(key, val);
return true;
} catch (err) {
//Handle errors here
return false;
}
},
add: function(options) { // need to add sopport for string concat
if (!this.isSupported) {
return;
}
if (typeof options === "string") {
options = {
key: options,
value: arguments[1]
};
}
var defualts = {
type: "local",
key: "",
val: "",
isObj: false,
propsAreObjs: false,
comparePropName: "",
ifFoundStillUpdate: false,
limit: null,
addDirection: "push" // or unshift
};
options = $.extend({}, defualts, options);
var storageedKey = adrma.storage.get(options.type, options.key, options.isObj),
isInArray;
if (storageedKey) {
if ($.isArray(storageedKey)) {
if (options.propsAreObjs) {
var matchedArray = $.grep(storageedKey, function(ele) {
return (options.val[options.comparePropName] === ele[options.comparePropName]);
});
if (matchedArray.length) {
if (!options.ifFoundStillUpdate) {
return false;
}
if ($.isArray(storageedKey)) { //IE8 issue!
storageedKey.splice(storageedKey.indexOf(matchedArray[0]), 1);
}
}
} else {
isInArray = $.inArray(options.val, storageedKey);
if (isInArray !== -1) {
return false;
}
}
if (options.limit && storageedKey.length >= options.limit) {
// just do the opposite of addDirection
storageedKey[options.addDirection === "push" ? "pop" : "shift"]();
}
storageedKey[options.addDirection](options.val);
options.val = storageedKey;
}
} else {
options.val = [options.val];
}
// either way add/update
return adrma.storage.set(options.type, options.key, options.val, options.isObj);
},
remove: function(type, key, val) {
if (!this.isSupported) {
return;
}
var storageedKey = adrma.storage.get(key, true),
isInArray;
if (storageedKey) {
if ($.isArray(storageedKey)) {
// check if its already there.
//isInArray returns the index of the found result in the aaray.
isInArray = $.inArray(val, storageedKey);
adrma.noop = isInArray !== -1 && storageedKey.splice(isInArray, 1);
val = storageedKey;
//console.log(storageedKey);
adrma.storage.set(key, val);
}
}
// either way add/update
},
"delete": function(type, key) {
if (!this.isSupported) {
return;
}
window[type + "Storage"].removeItem(key);
},
sync: function(key, url, direction) {
// sync key, and optional direction...
//adrma.fetchData({});
},
compare: function(key, url) {},
getAll: function(type, format) {
if (!this.isSupported) {
return;
}
var storage = window[type + "Storage"];
if (format === "string") {
return JSON.stringify(storage);
}
return storage;
},
clearAll: function(type) {
if (!this.isSupported) {
return;
}
window[type + "Storage"].clear();
}
};
adrma.storage.isSupported = (function() { // since the supported() is already used, needed a new method..
return adrma.storage.supported();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment