Created
October 25, 2018 10:41
-
-
Save peponi/643f3fd6247f847878c4f67039dcd147 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
const l = localStorage | |
const crappyStore = { | |
get: (key) => JSON.parse(l.getItem(key)), | |
set: (key, data) => l.setItem(key, JSON.stringify(data)), | |
rm: (key) => l.removeItem(key), | |
getKeysFor: (prefix) => { | |
const ret = [] | |
for (let key in l) { | |
ret.push(key) | |
} | |
return ret.filter((c) => c.substring(0, prefix.length) == prefix) | |
} | |
} | |
// Usage Example | |
const store_prefix = 'user_' | |
const userId = '123456' | |
crappyStore.set(store_prefix + userId, {name: 'Klaus'}) | |
crappyStore.get(store_prefix + userId) | |
const allUserKeys = crappyStore.getKeysFor(store_prefix) | |
allUserKeys.map(userKey => { | |
const user = crappyStore.get(userKey) | |
console.info(user.name) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment