Snippets

Humaan LocalStorage helper

Updated by Dan Barrett

File LocalStorage.js Modified

  • Ignore whitespace
  • Hide word diff
             };
         }
 
-        localStorage.setItem(key, JSON.stringify(value));
+        try {
+            localStorage.setItem(key, JSON.stringify(value));
+
+            return true;
+        } catch (e) {
+            console.log('Unable to store ' + key + ' in localStorage due to ' + e.name);
 
-        return true;
+            return false;
+        }
     },
 
     /**
Updated by Dan Barrett

File LocalStorage.js Modified

  • Ignore whitespace
  • Hide word diff
     },
 
     /**
+     * Remove all items from localStorage.  Takes no parameters and returns nothing.
+     *
+     * @kind function
+     * @function LocalStorage#clear
+     *
+     * @returns {void} Remove all keys from localStorage
+     */
+    clear: function () {
+        if (this.supportsLocalStorage()) {
+            localStorage.clear();
+        }
+    },
+
+    /**
      * Determines whether the browser supports localStorage or not.
      *
      * This method is required due to iOS Safari in Private Browsing mode incorrectly says it supports localStorage, when it in fact does not.
Updated by Dan Barrett

File LocalStorage.js Modified

  • Ignore whitespace
  • Hide word diff
 /**
- * Library that allows you to easily use the localStorage API with more power and flexibility.
+ * LocalStorage Helper
+ *
+ * Helper library that allows you to use the localStorage API with more power and flexibility.
  *
  * @class function LocalStorage
+ * @module LocalStorage
+ *
+ * @author Dan Barrett <danb@humaan.com.au>
+ * @copyright Humaan 2016
+ *
+ * @license
+ * Copyright (c) 2016 Humaan
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
-
-'use strict';
-
 var LocalStorage = {
     /**
      * Fetches the value associated with key from localStorage.  If the key/value aren't in localStorage, you can optional provide a callback to run as a fallback getter.
      * @param {?function} [optionalCallback=null] - If you want to handle the response in a callback, provide a callback and check the response.
      * @returns {*} Returns null if localStorage isn't supported, or the key/value isn't in localStorage, returns anything if it was in localStorage, or returns a callback if key/value was empty in localStorage and callback was provided.
      */
-    getItem: function(key, optionalCallback) {
+    getItem: function (key, optionalCallback) {
         if (!this.supportsLocalStorage()) {
             return null;
         }
 
-        var callback = function(data) {
+        var callback = function (data) {
             data = typeof data !== 'undefined' ? data : null;
 
             return typeof optionalCallback === 'function' ? optionalCallback(data) : data;
      * @param {?int} [expiry=null] - Time in seconds that the localStorage cache should be considered valid.
      * @returns {boolean} Returns true if it was stored in localStorage, false otherwise.
      */
-    setItem: function(key, value, expiry) {
+    setItem: function (key, value, expiry) {
         if (!this.supportsLocalStorage() || typeof value === 'undefined' || key === null || value === null) {
             return false;
         }
         if (typeof expiry === 'number') {
             value = {
                 __data: value,
-                __expiry: Date.now() + (expiry * 1000)
+                __expiry: Date.now() + (parseInt(expiry) * 1000)
             };
         }
 
      * @param {!string} key - Name of the key in localStorage.
      * @returns {void} Remove the key/value combo from the localStorage storage container.
      */
-    removeItem: function(key) {
+    removeItem: function (key) {
         if (this.supportsLocalStorage()) {
             localStorage.removeItem(key);
         }
             localStorage.removeItem('_');
 
             return true;
-        } catch(e) {
+        } catch (e) {
             return false;
         }
     }
 // CommonJS module support!
 if (typeof module !== 'undefined') {
     module.exports = LocalStorage;
-}
+}
Updated by Dan Barrett

File LocalStorage.js Modified

  • Ignore whitespace
  • Hide word diff
  *
  * @class function LocalStorage
  */
+
+'use strict';
+
 var LocalStorage = {
     /**
      * Fetches the value associated with key from localStorage.  If the key/value aren't in localStorage, you can optional provide a callback to run as a fallback getter.
Updated by Dan Barrett

File LocalStorage.js Modified

  • Ignore whitespace
  • Hide word diff
      */
     getItem: function(key, optionalCallback) {
         if (!this.supportsLocalStorage()) {
-            return null
+            return null;
         }
 
         var callback = function(data) {
                 if (now >= expiry) {
                     this.removeItem(key);
 
-                    return callback()
+                    return callback();
                 } else {
                     // Return the data object only.
-                    return callback(value.__data)
+                    return callback(value.__data);
                 }
             } else {
                 // Value doesn't have expiry data, just send it wholesale.
-                return callback(value)
+                return callback(value);
             }
         } else {
-            return callback()
+            return callback();
         }
     },
 
      */
     setItem: function(key, value, expiry) {
         if (!this.supportsLocalStorage() || typeof value === 'undefined' || key === null || value === null) {
-            return false
+            return false;
         }
 
         if (typeof expiry === 'number') {
      */
     removeItem: function(key) {
         if (this.supportsLocalStorage()) {
-            localStorage.removeItem(key)
+            localStorage.removeItem(key);
         }
     },
 
             localStorage.setItem('_', '_');
             localStorage.removeItem('_');
 
-            return true
+            return true;
         } catch(e) {
-            return false
+            return false;
         }
     }
 };
 
 // CommonJS module support!
 if (typeof module !== 'undefined') {
-    module.exports = LocalStorage
+    module.exports = LocalStorage;
 }
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.