Skip to content

Instantly share code, notes, and snippets.

@gooof
Forked from ksafranski/SimpleStore.js
Last active August 29, 2015 14:26
Show Gist options
  • Save gooof/2ca366e8808a8d578a2f to your computer and use it in GitHub Desktop.
Save gooof/2ca366e8808a8d578a2f to your computer and use it in GitHub Desktop.

Revisions

  1. Kent Safranski revised this gist Sep 25, 2013. 1 changed file with 24 additions and 6 deletions.
    30 changes: 24 additions & 6 deletions SimpleStore.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    /**
    * Simple localStorage with Cookie Fallback
    * v.1.0.0
    *
    * USAGE:
    * ----------------------------------------
    @@ -12,9 +13,9 @@
    * Delete / Remove:
    * store('my_key', null);
    */

    function store(key, value) {

    var store = function store(key, value) {
    var lsSupport = false;

    // Check for native support
    @@ -24,6 +25,11 @@ function store(key, value) {

    // If value is detected, set new or modify store
    if (typeof value !== "undefined" && value !== null) {
    // Convert object values to JSON
    if ( typeof value === 'object' ) {
    value = JSON.stringify(value);
    }
    // Set the store
    if (lsSupport) { // Native support
    localStorage.setItem(key, value);
    } else { // Use Cookie
    @@ -33,11 +39,23 @@ function store(key, value) {

    // No value supplied, return value
    if (typeof value === "undefined") {
    // Get value
    if (lsSupport) { // Native support
    return localStorage.getItem(key);
    data = localStorage.getItem(key);
    } else { // Use cookie
    return readCookie(key);
    data = readCookie(key);
    }

    // Try to parse JSON...
    try {
    data = JSON.parse(data);
    }
    catch(e) {
    data = data;
    }

    return data;

    }

    // Null specified, remove store
    @@ -79,4 +97,4 @@ function store(key, value) {
    return null;
    }

    }
    };
  2. @ksafranski ksafranski created this gist Feb 5, 2013.
    82 changes: 82 additions & 0 deletions SimpleStore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    /**
    * Simple localStorage with Cookie Fallback
    *
    * USAGE:
    * ----------------------------------------
    * Set New / Modify:
    * store('my_key', 'some_value');
    *
    * Retrieve:
    * store('my_key');
    *
    * Delete / Remove:
    * store('my_key', null);
    */

    function store(key, value) {

    var lsSupport = false;

    // Check for native support
    if (localStorage) {
    lsSupport = true;
    }

    // If value is detected, set new or modify store
    if (typeof value !== "undefined" && value !== null) {
    if (lsSupport) { // Native support
    localStorage.setItem(key, value);
    } else { // Use Cookie
    createCookie(key, value, 30);
    }
    }

    // No value supplied, return value
    if (typeof value === "undefined") {
    if (lsSupport) { // Native support
    return localStorage.getItem(key);
    } else { // Use cookie
    return readCookie(key);
    }
    }

    // Null specified, remove store
    if (value === null) {
    if (lsSupport) { // Native support
    localStorage.removeItem(key);
    } else { // Use cookie
    createCookie(key, '', -1);
    }
    }

    /**
    * Creates new cookie or removes cookie with negative expiration
    * @param key The key or identifier for the store
    * @param value Contents of the store
    * @param exp Expiration - creation defaults to 30 days
    */

    function createCookie(key, value, exp) {
    var date = new Date();
    date.setTime(date.getTime() + (exp * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = key + "=" + value + expires + "; path=/";
    }

    /**
    * Returns contents of cookie
    * @param key The key or identifier for the store
    */

    function readCookie(key) {
    var nameEQ = key + "=";
    var ca = document.cookie.split(';');
    for (var i = 0, max = ca.length; i < max; i++) {
    var c = ca[i];
    while (c.charAt(0) === ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
    }

    }