Last active
November 10, 2019 22:16
-
-
Save droduit/c784520d161b22c9d75ea3d10a5d0fc7 to your computer and use it in GitHub Desktop.
Cookie-like implementation of localStorage. You can use it as an alternative to cookies.
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
/** | |
* Use the browser's local storage to store the data. | |
* You can optionally give an expiration date in seconds or timestamp. | |
**/ | |
let cookies = { | |
get: (key) => { | |
if (!localStorage.getItem(key) || key === null) { | |
return null; | |
} | |
try { | |
var objectInStorage = JSON.parse(localStorage.getItem(key)); | |
if (objectInStorage.expiration < Date.now()) { | |
return removeLocalStorage(key) ? null : objectInStorage.value; | |
} else { | |
try { | |
return objectInStorage.value; | |
} catch (e) { | |
return null; | |
} | |
} | |
} catch (e) { | |
return localStorage.getItem(key); | |
} | |
}, | |
set: (key, value, expires = null) => { | |
if (key === null) { | |
return false; | |
} | |
if (!(expires === undefined || expires === null)) { | |
expires = Math.abs(expires); | |
} | |
try { | |
if (expires != undefined && expires != null) { | |
let expirationTimestamp = expires < Date.now() ? Date.now() + expires*1000 : expires; | |
localStorage.setItem(key, JSON.stringify({value:value, expiration: expirationTimestamp})); | |
} else { | |
localStorage.setItem(key, value); | |
} | |
} catch(e) { | |
return false; | |
} | |
return true; | |
}, | |
remove: (key) => { | |
try { | |
localStorage.removeItem(key); | |
} catch (e) { | |
return false; | |
} | |
return true; | |
} | |
}; | |
/** | |
=============================================== | |
Usage | |
=============================================== | |
**/ | |
// No expiration date | |
cookies.set("ip", "192.168.0.1"); | |
// Expiration in seconds | |
cookies.set("ip", "192.168.0.1", 1); // Expire after 1 second | |
cookies.set("ip", "192.168.0.1", 60); // Expire after 1 minute | |
cookies.set("ip", "192.168.0.1", 60*60); // Expire after 1 hour | |
cookies.set("ip", "192.168.0.1", 24*60*60); // Expire after 1 day | |
cookies.set("ip", "192.168.0.1", 365*24*60*60); // Expire after 365 days | |
cookies.set("ip", "192.168.0.1", 365*24*60*60); // Expire after 365 days | |
cookies.set("ip", "192.168.0.1", 365*24*60*60); // Expire after 365 days | |
// Timestamp based expiration | |
cookies.set("ip", "192.168.0.1", 1572466709); // Expire on 10/30/2019 @ 8:18pm (UTC) | |
cookies.set("ip", "192.168.0.1", 1605722400); // Expire on 11/18/2020 @ 6:00pm (UTC) | |
// ----------------------------------------- | |
cookies.get("ip"); // 192.168.0.1 (or null if not set) | |
// ----------------------------------------- | |
cookies.remove("ip"); // true if present, else false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment