@bonny-kato/localstorage v0.0.2
Overview
@bonny-kato/localstorage
is a highly efficient and user-friendly wrapper library for the browser's LocalStorage API. This robust tool abstracts away the intricacies of dealing with client-side storage, offering an intuitive way to manage data persistence in a user's browser.
Fitted with methods for storing, retrieving, and removing data, this library brings an unparalleled level of convenience to interaction with local storage. Its design accommodates single and batch operations, allowing the setting and removal of multiple key-value pairs at once.
Beyond these, it also offers unique key management to circumvent potential key conflicts, lending itself useful during local development. To add to this, @bonny-kato/localstorage
is Server-Side Rendering (SSR) compatible, catering for a variety of development scenarios.
For those diving into local storage operations for the first time or developers looking to streamline their data persistence process, @bonny-kato/localstorage
is the library of choice.
Installation
npm i @bonny-kato/localstorage
yarn add @bonny-kato/localstorage
Usage
Methods
getValue(key: string | null = null, defaultValue?: any): any
: Retrieves the value stored in the local storage for the given key. If no key is provided, it returns the entire store. If the key doesn't exist, it returns the provided defaultValue or null.setValue(key: string, value: string | boolean | Array<any> | object): void
: Sets the value for a given key in the local storage. If the key already exists, its value will be updated.setValues(data: object): void
: Sets multiple key-value pairs at once in the local storage.removeValues(keys: string | Array<string>): void
: Removes the specified key(s) and their corresponding values from the local storage.has(key: string): boolean
: Checks if a key is present in the local storage.
Examples
const lStorage = new LocalStorage("StorageKey");
// Getting a value
const value = lStorage.getValue("my-key", defaultValueIfExist);
// Setting a value
lStorage.setValue("my-key", {"key":"value"});
// Setting multiple values
lStorage.setValues({ "my-key1": "my-value", "my-key2": {key:"value"}});
// Removing values
lStorage.removeValues(["key1", "key2", "key3"]);
// Checking if a key exists
const hasName = lStorage.has("name");
Features
- Unique Key Management: The LocalStorage library creates localstorage within its own object. This eliminates the potential for key conflicts with other scripts or sites that use the same keys, especially during local development.
- Server Side Rendering (SSR) Compatible: Can be run on the server side without throwing any errors.
- Bulk Removal: The
removeValues
method allows removing multiple key-value pairs from the local storage at once. - Bulk Addition: The
setValues
method enables setting multiple key-value pairs to the local storage simultaneously. - Key Checking: The
has
method allows users to check if a specific key exists in the local storage. - Usability: The interface and methods of the LocalStorage library are straightforward and intuitive, making it easy even for beginners.
- Comprehensive Documentation: The library is thoroughly documented, providing clear instructions on how to use each method.
- Default Value: The
getValue
method allows passing a default value in case a the key does not exist in storage