Created
June 15, 2014 15:27
-
-
Save allex/a9b431ebcc921f2b309d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple storage warpper. | |
* @module storage | |
* @author Allex Wang (http://iallex.com) | |
*/ | |
(function (define) { | |
define('storage', function (require) { | |
var storage = null; | |
var localStorage = window.localStorage; | |
if (localStorage) { | |
storage = { | |
get: function(k, parse) { | |
var v = localStorage.getItem(k); | |
if (parse) { v = JSON.parse(v); } | |
return v; | |
}, | |
set: function(k, v) { | |
if (typeof v !== 'string') { | |
v = JSON.stringify(v); | |
} | |
localStorage.setItem(k, v); | |
}, | |
remove: function(k) { | |
localStorage.removeItem(k); | |
} | |
}; | |
} | |
else { | |
/** | |
* Get or set cookies with specific name. | |
* @param {String} name Cookie name. | |
* @param {String} value (Optional) the cookie value, set to `NULL` to remove cookie values | |
* @param {Number|Date} expires (Optional) day? * 30(days). | |
* @author Allex Wang ([email protected]) | |
*/ | |
function cookie(name, value, expires, domain) { | |
if (value === undefined) { | |
name += '='; | |
value = document.cookie.split(';'); | |
for (var i = 0, l = value.length; i < l; i++) { | |
for (domain = value[i]; domain.charAt(0) === ' ';) domain = domain.substring(1, domain.length); | |
if (domain.indexOf(name) === 0) { | |
return decodeURIComponent(domain.substring(name.length, domain.length)); | |
} | |
} | |
return null; | |
} else { | |
var date = '', isnum; | |
if (value === null) { | |
value = ''; | |
expires = -1; | |
} | |
if ( (isnum = typeof expires === 'number') || expires instanceof Date ) { | |
if (isnum) { | |
date = new Date; | |
date.setTime(date.getTime() + expires * 864E5); // 30 days | |
} else { | |
date = expires; | |
} | |
date = '; expires=' + date.toUTCString(); | |
} | |
document.cookie = name + '=' + value + date + '; path=/' + (domain ? ';domain=' + domain : ''); | |
} | |
} | |
storage = { | |
get: function(k, parse) { | |
var v = cookie(k); | |
if (parse) { | |
v = JSON.parse(v); | |
} | |
return v; | |
}, | |
set: function(k, v) { | |
if (typeof v !== 'string') { | |
v = JSON.stringify(v); | |
} | |
cookie(k, v); | |
}, | |
remove: function(k) { | |
cookie(k, null); | |
} | |
}; | |
} | |
return storage; | |
}); | |
}(typeof define === 'function' && define.amd ? define : function (id, factory) { | |
if (typeof module !== 'undefined' && module.exports) { | |
//Node | |
module.exports = factory(require); | |
} else { | |
//Create a global function. Only works if the code does not have | |
//dependencies, or dependencies fit the call pattern below. | |
window[id] = factory(function(value) { return window[value]; }); | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment