UNPKG

web-shared-preferences

Version:

An simple class to manage the loacal storage, including React like useString, useJSON and more!

160 lines 4.95 kB
import { LocalStorage } from "./LocalStorage"; import { SharedPreferenceError } from "./SharedPreferenceError"; /** * Simple class to manage the web local sotrage */ class SharedPreferences { constructor(storage) { this._storage = storage; } setString(key, value) { this._storage.setItem(key, String(value)); } setBoolean(key, value) { this._storage.setItem(key, String(value)); } setNumber(key, value) { this._storage.setItem(key, String(value)); } /** * Sets an json object to the localstorage. All properties are partially * @param key * @param value */ setJSON(key, value) { this._storage.setItem(key, JSON.stringify(value)); } /** * Retrieve a String value from the preferences. * * @param key The name of the preference to retrieve. * @param defValue Value to return if this preference does not exist. * * @return Returns the preference value if it exists, or defValue. Throws SharedPreferenceError if there is a preference with this name that is not a String. * * @throws SharedPreferenceError */ getString(key, defValue) { try { const get = this._storage.getItem(key); switch (get) { case null: return defValue; case undefined: return defValue; default: return get; } } catch (e) { throw new SharedPreferenceError(e.message); } } /** * Retrieve a boolean value from the preferences. * * @remember if it's from an Switch or Select, put `_switch` or `_select` after the name * * @param key The name of the preference to retrieve. * @param defValue Value to return if this preference does not exist. * * @returns Returns the preference value if it exists, or defValue. Throws SharedPreferenceError if there is a preference with this name that is not a boolean. * * @throws SharedPreferenceError */ getBoolean(key, defValue) { try { const get = this._storage.getItem(key); switch (get) { case null: return defValue; case undefined: return defValue; default: return get === "true"; } } catch (e) { throw new SharedPreferenceError(e.message); } } /** * Retrieve a int value from the preferences. * * @param key The name of the preference to retrieve. * @param defValue Value to return if this preference does not exist. * * @returns Returns the preference value if it exists, or defValue. Throws SharedPreferenceError if there is a preference with this name that is not an int. * * @throws SharedPreferenceError */ getNumber(key, defValue) { try { const get = this._storage.getItem(key); switch (get) { case null: return defValue; case undefined: return defValue; default: return Number(get); } } catch (e) { throw new SharedPreferenceError(e.message); } } getJSON(key, defValue) { try { const get = this._storage.getItem(key); switch (get) { case null: return defValue; case undefined: return defValue; default: return JSON.parse(get); } } catch (e) { throw new SharedPreferenceError(e.message); } } /** * Removes the key/value pair with the given key, if a key/value pair with the given key exists. * * Dispatches a storage event on Window objects holding an equivalent Storage object. */ removePref(key) { this._storage.removeItem(key); } /** * Removes all key/value pairs, if there are any. * * Dispatches a storage event on Window objects holding an equivalent Storage object. */ clearPrefs() { this._storage.clear(); } /** * **Beta** element, be secure with that! */ hasPref(key) { const get = this._storage.getItem(key); switch (get) { case null: return false; case undefined: return false; default: return true; } } } SharedPreferences.TAG = "SharedPreferences"; /** * Static SharedPreferences. Uses `window.localStorage`. */ const sharedpreferences = new SharedPreferences(LocalStorage()); export { SharedPreferences, sharedpreferences }; //# sourceMappingURL=SharedPreferences.js.map