From e4899fa551d53ec07718659eed97178052982552 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 15 Dec 2015 11:36:14 -0500
Subject: Improved PreferenceStore api when getting values

---
 web/react/stores/preference_store.jsx | 49 ++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
index 543129aca..79eab4fe1 100644
--- a/web/react/stores/preference_store.jsx
+++ b/web/react/stores/preference_store.jsx
@@ -23,8 +23,11 @@ class PreferenceStoreClass extends EventEmitter {
         super();
 
         this.getAllPreferences = this.getAllPreferences.bind(this);
+        this.get = this.get.bind(this);
+        this.getBool = this.getBool.bind(this);
+        this.getInt = this.getInt.bind(this);
         this.getPreference = this.getPreference.bind(this);
-        this.getPreferences = this.getPreferences.bind(this);
+        this.getCategory = this.getCategory.bind(this);
         this.getPreferencesWhere = this.getPreferencesWhere.bind(this);
         this.setAllPreferences = this.setAllPreferences.bind(this);
         this.setPreference = this.setPreference.bind(this);
@@ -41,11 +44,51 @@ class PreferenceStoreClass extends EventEmitter {
         return new Map(BrowserStore.getItem('preferences', []));
     }
 
-    getPreference(category, name, defaultValue = '') {
+    get(category, name, defaultValue = '') {
+        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+
+        if (!preference) {
+            return defaultValue;
+        }
+
+        return preference.value || defaultValue;
+    }
+
+    getBool(category, name, defaultValue = false) {
+        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+
+        if (!preference) {
+            return defaultValue;
+        }
+
+        // prevent a non-false default value from being returned instead of an actual false value
+        if (preference.value === 'false') {
+            return false;
+        }
+
+        return (preference.value !== 'false') || defaultValue;
+    }
+
+    getInt(category, name, defaultValue = 0) {
+        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));
+
+        if (!preference) {
+            return defaultValue;
+        }
+
+        // prevent a non-zero default value from being returned instead of an actual 0 value
+        if (preference.value === '0') {
+            return 0;
+        }
+
+        return parseInt(preference.value, 10) || defaultValue;
+    }
+
+    getPreference(category, name, defaultValue = {}) {
         return this.getAllPreferences().get(getPreferenceKey(category, name)) || defaultValue;
     }
 
-    getPreferences(category) {
+    getCategory(category) {
         return this.getPreferencesWhere((preference) => (preference.category === category));
     }
 
-- 
cgit v1.2.3-1-g7c22

