Skip to content

Instantly share code, notes, and snippets.

@deleteme
Created July 30, 2010 22:22
Show Gist options
  • Save deleteme/501416 to your computer and use it in GitHub Desktop.
Save deleteme/501416 to your computer and use it in GitHub Desktop.
A coffeescript wrapper around cookies and localstorage
# store.js
# a simple wrapper around html5 local storage and cookies
# provides three methods to interact with data
#
# it automatically purges data from localStorage if it's full.
#
# set: key, value
# stores the value in a key
# returns the value
# get: key
# retrieves the value in the key
# returns the value
# expire: key
# removes the data and the key
# returns the value
Store = do ->
localStorageSupported = do ->
try
`(('localStorage' in window) && window['localStorage'] !== null)`
catch e
false
if localStorageSupported
# will displace data until it can successfully save
safeSet = (key, value) ->
try
localStorage.setItem key, value
value
catch e
for num in [0..5]
localStorage.removeItem localStorage.key localStorage.length - 1
safeSet key, value
{
set: safeSet
get: (key) ->
localStorage[key]
expire: (key) ->
value = localStorage[key]
localStorage.removeItem(key)
value
}
else
createCookie = (name, value, days) ->
if days
date = new Date
date.setTime(date.getTime() + (days*24*60*60*1000))
expires = "; expires=" + date.toGMTString()
else
expires = ""
document.cookie = name + "=" + value + expires + "; path=/"
value
getCookie = (key) ->
key = key + "="
for cookieFragment in document.cookie.split(';')
return cookieFragment.replace(/^\s+/, '').substring(key.length, cookieFragment.length) if cookieFragment.indexOf(key) == 0
return null
{
set: (key, value) ->
createCookie key, value, 1
get: getCookie
expire: (key) ->
value = Store.get key
createCookie key, "", -1
value
}
var Store;
Store = (function() {
var createCookie, getCookie, localStorageSupported, safeSet;
localStorageSupported = (function() {
try {
return (('localStorage' in window) && window['localStorage'] !== null);
} catch (e) {
return false;
}
})();
if (localStorageSupported) {
safeSet = function(key, value) {
var num;
try {
localStorage.setItem(key, value);
return value;
} catch (e) {
for (num = 0; num <= 5; num++) {
localStorage.removeItem(localStorage.key(localStorage.length - 1));
}
return safeSet(key, value);
}
};
return {
set: safeSet,
get: function(key) {
return localStorage[key];
},
expire: function(key) {
var value;
value = localStorage[key];
localStorage.removeItem(key);
return value;
}
};
} else {
createCookie = function(name, value, days) {
var date, expires;
if (days) {
date = new Date;
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
return value;
};
getCookie = function(key) {
var cookieFragment, _i, _len, _ref;
key = key + "=";
_ref = document.cookie.split(';');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cookieFragment = _ref[_i];
if (cookieFragment.indexOf(key) === 0) {
return cookieFragment.replace(/^\s+/, '').substring(key.length, cookieFragment.length);
}
}
return null;
};
return {
set: function(key, value) {
return createCookie(key, value, 1);
},
get: getCookie,
expire: function(key) {
var value;
value = Store.get(key);
createCookie(key, "", -1);
return value;
}
};
}
})();
@nasa42
Copy link

nasa42 commented Nov 3, 2011

Right now getCookie() hangs Firefox.

Some corrections

On line #27 and #28, instead of

  cookieFragment: cookieFragment.substring(1, cookieFragment.length) 
return cookieFragment.substring(key.length, cookieFragment.length) if cookieFragment.indexOf(key) == 0

it should be

  cookieFragment = cookieFragment.substring(1, cookieFragment.length) 
return cookieFragment.substring(key.length+1, cookieFragment.length) if cookieFragment.indexOf(key) == 0

@deleteme
Copy link
Author

deleteme commented Nov 3, 2011

@midnightcodr
Copy link

Line 27 should be
set: (key, value) -> localStorage[key]: value

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