@mewters/smart-storage
v1.0.0
Published
A JavaScript/TypeScript library designed to simplify working with localStorage, sessionStorage, and cookies by offering automatic object conversion and consistent API.
Downloads
307
Maintainers
Readme
Smart Storage
Smart Storage is a simple and easy-to-use library to manage local storage, session storage, and cookies with a consistent API, TypeScript support and object serialization.
Features
- Simple API: The API is simple and easy to use.
- TypeScript Support: The library is written in TypeScript and has type definitions.
- Object Serialization: The library serializes and deserializes objects automatically. It means you can store and retrieve objects without any extra work.
- Consistent API: The API is consistent across local storage, session storage, and cookies.
- Safe Retrieval: The library provides a safe retrieval by always providing a default value.
- Cookie Expiration: The library allows you to set the number of days the cookie will expire.
- Lightweight: The library is lightweight and has no dependencies.
- Tested: The library is well tested with 100% code coverage.
Install
npm install @mewters/smart-storage
Usage
import {
LocalStorage,
SessionStorage,
CookieStorage,
} from '@mewters/smart-storage';
API
LocalStorage, SessionStorage, and CookieStorage have the same API.
Let's use LocalStorage as an example.
.get
Use the get
method to retrieve a value from storage.
The key
is an unique identifier for the value. The defaultValue
is the value to return if the key does not exist.
If the key does not exist, it will return the default value. For a safe retrieval, always provide a default value.
LocalStorage.get('key1', 'defaultValue');
LocalStorage.get('key2', 5);
LocalStorage.get('key3', { defaultValue: 'value' });
.set
Use the set
method to store a value in storage.
The key
is an unique identifier for the value. The value
is the value to store. It has to be a string, number, or object.
LocalStorage.set('key1', 'value');
LocalStorage.set('key2', 5);
LocalStorage.set('key3', { value: 'value' });
Note: The CookieStorage
has an additional days
parameter to set the number of days the cookie will expire.
By default, the value is 365 days.
CookieStorage.set('key1', 'value');
CookieStorage.set('key2', 5, 30); // 30 days
CookieStorage.set('key3', { value: 'value' }, 7); // 7 days
.remove
Use the remove
method to remove a value from storage.
The key
is an unique identifier for the value to remove.
LocalStorage.remove('key1');
.removeItems
Use the removeItems
method to remove multiple values from storage.
The keys
is an array of unique identifiers for the values to remove.
LocalStorage.removeItems(['key1', 'key2']);
.clear
Use the clear
method to remove all values from storage.
LocalStorage.clear();