Created
July 30, 2010 22:22
-
-
Save deleteme/501416 to your computer and use it in GitHub Desktop.
A coffeescript wrapper around cookies and localstorage
This file contains 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
# 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 | |
} |
This file contains 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
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; | |
} | |
}; | |
} | |
})(); |
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
Right now getCookie() hangs Firefox.
Some corrections
On line #27 and #28, instead of
it should be