UNPKG

web-shared-preferences

Version:

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

84 lines (83 loc) 3.06 kB
export interface StorageImpl { get length(): number; setItem(key: string, value: string): void; getItem(key: string): string | null; clear(): void; removeItem(key: string): void; key(index: number): string | null; [name: string]: any; } /** * Simple class to manage the web local sotrage */ declare class SharedPreferences { private static readonly TAG; private _storage; constructor(storage: StorageImpl); setString(key: string, value: string): void; setBoolean(key: string, value: boolean): void; setNumber(key: string, value: number): void; /** * Sets an json object to the localstorage. All properties are partially * @param key * @param value */ setJSON<T = any>(key: string, value: T): void; /** * 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: string, defValue: string): string; /** * 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: string, defValue: boolean): boolean; /** * 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: string, defValue: number): number; getJSON<T = any>(key: string, defValue: T): T; /** * 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: string): void; /** * Removes all key/value pairs, if there are any. * * Dispatches a storage event on Window objects holding an equivalent Storage object. */ clearPrefs(): void; /** * **Beta** element, be secure with that! */ hasPref(key: string): boolean; } /** * Static SharedPreferences. Uses `window.localStorage`. */ declare const sharedpreferences: SharedPreferences; export { SharedPreferences, sharedpreferences };