localStorages
This utility function provides a simple interface for interacting with the browser's localStorage. It allows you to get, set, and remove data associated with a specified key, with support for parsing and stringifying JSON data.
Features
- The
localStoragesfunction returns an object with methods for interacting withlocalStorage:get: Retrieves the value associated with the specified key. If the value is JSON, it will be parsed and returned. If parsing fails, the raw string is returned.set: Stores a new value inlocalStorageunder the specified key. The value is stringified as JSON before storage.remove: Removes the value associated with the specified key fromlocalStorage.
- This utility is generic, allowing you to specify the type of the stored value using TypeScript's generic syntax.
Types
export const localStorages: <T = unknown>(
key: string,
) => {
get: () => T | null;
set: (newValue: T) => boolean;
remove: () => boolean;
};
Example
// Example 1: Storing and retrieving a simple string
const userSettingsStorage = localStorages<string>('userSettings');
userSettingsStorage.set('darkMode');
const setting = userSettingsStorage.get();
console.log(setting); // Output: "darkMode"
// Example 2: Storing and retrieving an object
const preferencesStorage = localStorages<{ theme: string; notifications: boolean }>('preferences');
preferencesStorage.set({ theme: 'dark', notifications: true });
const preferences = preferencesStorage.get();
console.log(preferences); // Output: { theme: 'dark', notifications: true }
// Example 3: Removing a value
preferencesStorage.remove();
const removedPreferences = preferencesStorage.get();
console.log(removedPreferences); // Output: null