0.0.4 • Published 2 months ago
lsc-storage v0.0.4
🚀 lsc-storage
A lightweight storage library that makes managing your web storage a breeze! 🌟
Currently supports local storage with plans for session storage and cookie storage in future releases.
Table of Contents
Features
- Unified API: Simple methods to interact with your storage.
- TypeScript Support: Enjoy type safety and IntelliSense.
- Fallback Mechanism: Automatically falls back to an in-memory storage if
localStorage
is unavailable. - Extensible: Easily add support for session storage and cookie storage in future updates.
Installation
Install via npm:
npm install lsc-storage
Usage
Importing the Library
import { lsc } from "lsc-storage";
Basic Examples for lsc-storage
Storing Data
// Store a simple string
lsc.set("username", "john_doe");
// Store an object
lsc.set("user", { name: "John Doe", age: 30 });
Retrieving Data
// Retrieve a string value
const username = lsc.get<string>("username");
// Retrieve an object
const user = lsc.get<{ name: string; age: number }>("user");
Removing Data
// Remove an item by key
lsc.remove("username");
Clearing All Data
// Clear all items from storage
lsc.clear();
Flushing Data
// Flush expired items (or force flush all with true)
lsc.flush();
lsc.flush(true);
API Reference
Method/Function | Signature | Description | Parameters | Return Type |
---|---|---|---|---|
set | set<T>(key: string, value: T, localConfig?: Omit<StorageConfig, 'storage'>): void | boolean | Stores a value in storage under the specified key. | key: stringvalue: TlocalConfig (optional): Configuration object (excluding storage ) | void if successful, false if error occurs |
get | get<T>(key: string, localConfig?: Omit<StorageConfig, 'storage'>): T | null | Retrieves the value associated with the specified key. | key: stringlocalConfig (optional): Configuration object (excluding storage ) | Returns the stored value of type T or null |
remove | remove(key: string): void | Removes the item associated with the specified key from storage. | key: string | void |
clear | clear(): void | Clears all items from storage. | None | void |
flush | flush(force?: boolean): void | Clears expired items from storage. If force is true , clears all items regardless of expiration. | force (optional): boolean (defaults to false ) | void |
localMemoryStore | localMemoryStore(): Storage | Creates an in-memory storage object that implements the Storage interface. Used as a fallback mechanism. | None | Storage |
StorageConfig | { storage?: Storage; ttl?: number | null } | Interface defining configuration options for storage operations. | storage (optional): Storage mechanism (e.g., localStorage )ttl (optional): Time-to-live in milliseconds | Interface (no return value) |
KeyValuePair | type KeyValuePair<T = unknown> = Record<string, T>; | Type representing an object with string keys and values of type T . | None | Type definition |
Development
To set up the development environment:
- Clone the repository:
git clone https://github.com/devlopersabbir/lsc-storage.git cd lsc-storage
- Install dependencies:
npm install
- Run the development server:
npm run dev
- Run tests:
npm test
- Build the project:
npm run build
Contributing
Contributions are welcome! 🎉 Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -m 'Add YourFeature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a pull request.
For detailed contribution guidelines, refer to the CONTRIBUTING.md file.
License
This project is licensed under the MIT License
Author
- Name: Sabbir Hossain Shuvo
- Email: devlopersabbir@gmail.com
- GitHub: @devlopersabbir
- Buy Me a Coffee: @devlopersabbir
Future Enhancements
- 🔮 Session Storage: Coming soon!
- 🍪 Cookie Storage: On the roadmap!