Skip to content

Instantly share code, notes, and snippets.

@lastcanal
Created January 13, 2011 15:39
Show Gist options
  • Select an option

  • Save lastcanal/778059 to your computer and use it in GitHub Desktop.

Select an option

Save lastcanal/778059 to your computer and use it in GitHub Desktop.
LocalStorage key patterns
class Crypt
constructor: (@key) ->
GibberishAES.size(128);
decrypt: (str) ->
return str unless str? and @key?
GibberishAES.dec(str, @key)
encrypt: (str) ->
return str unless str? and @key?
crypt = GibberishAES.enc(str, @key)
crypt
#
class LocalStorage extends Crypt
constructor: (key) ->
super(key)
unless localStorage.length
localStorage.setItem('Initialized', new Date)
@
get: (key) ->
val = @decrypt localStorage.getItem(key)
JSON.parse(val) if val
set: (key, val) ->
localStorage[key] = @encrypt JSON.stringify(val)
keymatch: (key, pattern) ->
return true if pattern is "*"
if pattern.substr(-1) is "*"
prefix = pattern.substr(0, pattern.length - 1)
return key.substr(0, prefix.length) is prefix
getPattern: (pattern, cont) ->
unless pattern.substr(-1) is "*"
return [@get(pattern)]
result = []
len = localStorage.length
while --len
key = localStorage.key( len )
if key and @keymatch(key, pattern)
result.push @get( key )
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment