From ff4067e637c71c9be257b0feb6415827051ea4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 2 Nov 2015 09:59:59 +0100 Subject: [PATCH] Revert "Add first cut on persistent stores" This reverts commit bed067f9bc05f1ef65e3158fab37dfe57fb03c2c. Conflicts: js/actions/user_actions.js js/stores/session_persistent_store.js js/stores/user_store.js js/utils/feature_detection_utils.js --- js/models/ascribe_storage.js | 98 ----------------------------- js/stores/user_store.js | 3 - js/utils/feature_detection_utils.js | 2 +- js/utils/general_utils.js | 9 +-- 4 files changed, 3 insertions(+), 109 deletions(-) delete mode 100644 js/models/ascribe_storage.js diff --git a/js/models/ascribe_storage.js b/js/models/ascribe_storage.js deleted file mode 100644 index 9f71e243..00000000 --- a/js/models/ascribe_storage.js +++ /dev/null @@ -1,98 +0,0 @@ -'use strict'; - -import { sanitize } from '../utils/general_utils'; - -/** - * A tiny wrapper around HTML5's `webStorage`, - * to enable saving JSON objects directly into `webStorage` - */ -export default class AscribeStorage { - /** - * @param {String} `name` A unique storage name - */ - constructor(type, name) { - if(type === 'localStorage' || type === 'sessionStorage') { - this.storage = window[type]; - } else { - throw new Error('"type" can only be either "localStorage" or "sessionStorage"'); - } - - this.name = name; - } - - /** - * Private method, do not use from the outside. - * Constructs a unique identifier for a item in the global `webStorage`, - * by appending the `ÀscribeStorage`'s name - * @param {string} key A unique identifier - * @return {string} A globally unique identifier for a value in `webStorage` - */ - _constructStorageKey(key) { - return `${this.name}-${key}`; - } - - _deconstructStorageKey(key) { - return key.replace(`${this.name}-`, ''); - } - - /** - * Saves a JSON-serializeble object or a string into `webStorage` - * @param {string} key Used as a unique identifier - * @param {oneOfType([String, object])} value Either JSON-serializeble or a string - */ - setItem(key, value) { - // We're "try-catching" errors in this method ourselves, to be able to - // yield more readable error messages - - if(!key || !value) { - throw new Error('"key" or "value" cannot be "falsy" values'); - } else if(typeof value === 'string') { - // since `value` is a string, we can directly write - // it into `this.storage` - this.storage.setItem(this._constructStorageKey(key), value); - } else { - // if `value` is not a string, we need to JSON-serialize it and then - // put it into `this.storage` - - let serializedValue; - try { - serializedValue = JSON.stringify(value); - } catch(err) { - throw new Error('You didn\'t pass valid JSON as "value" into setItem.'); - } - - try { - this.storage.setItem(this._constructStorageKey(key), serializedValue); - } catch(err) { - throw new Error('Failure saving a "serializedValue" in setItem'); - } - } - } - - getItem(key) { - let deserializedValue; - const serializedValue = this.storage.getItem(this._constructStorageKey(key)); - - try { - deserializedValue = JSON.parse(serializedValue); - } catch(err) { - deserializedValue = serializedValue; - } - - return deserializedValue; - } - - toObject() { - let obj = {}; - const storageCopy = JSON.parse(JSON.stringify(this.storage)); - const sanitizedStore = sanitize(storageCopy, s => !s.match(`${this.name}-`), true); - - Object - .keys(sanitizedStore) - .forEach((key) => { - obj[this._deconstructStorageKey(key)] = JSON.parse(sanitizedStore[key]); - }); - - return obj; - } -} \ No newline at end of file diff --git a/js/stores/user_store.js b/js/stores/user_store.js index cdbb40ea..94d7777e 100644 --- a/js/stores/user_store.js +++ b/js/stores/user_store.js @@ -5,9 +5,6 @@ import UserActions from '../actions/user_actions'; import UserSource from '../sources/user_source'; -// import AscribeStorage from '../models/ascribe_storage'; -// import { sessionStorageAvailable } from '../utils/feature_detection_utils'; - class UserStore { constructor() { diff --git a/js/utils/feature_detection_utils.js b/js/utils/feature_detection_utils.js index d086c68c..6cbd533d 100644 --- a/js/utils/feature_detection_utils.js +++ b/js/utils/feature_detection_utils.js @@ -23,4 +23,4 @@ * @type {bool} Is drag and drop available on this browser */ export const dragAndDropAvailable = 'draggable' in document.createElement('div') && - !/Mobile|Android|Slick\/|Kindle|BlackBerry|Opera Mini|Opera Mobi/i.test(navigator.userAgent); \ No newline at end of file + !/Mobile|Android|Slick\/|Kindle|BlackBerry|Opera Mini|Opera Mobi/i.test(navigator.userAgent); diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index 07d42327..7c13f9b5 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -6,12 +6,9 @@ * tagged as false by the passed in filter function * * @param {object} obj regular javascript object - * @param {function} filterFn a filter function for filtering either by key or value - * @param {bool} filterByKey a boolean for choosing whether the object should be filtered by - * key or value * @return {object} regular javascript object without null values or empty strings */ -export function sanitize(obj, filterFn, filterByKey) { +export function sanitize(obj, filterFn) { if(!filterFn) { // By matching null with a double equal, we can match undefined and null // http://stackoverflow.com/a/15992131 @@ -21,9 +18,7 @@ export function sanitize(obj, filterFn, filterByKey) { Object .keys(obj) .map((key) => { - const filterCondition = filterByKey ? filterFn(key) : filterFn(obj[key]); - - if(filterCondition) { + if(filterFn(obj[key])) { delete obj[key]; } });