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.
- 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.
Install via npm:
npm install lsc-storage
import { lsc } from "lsc-storage";
// Store a simple string
lsc.set("username", "john_doe");
// Store an object
lsc.set("user", { name: "John Doe", age: 30 });
// Retrieve a string value
const username = lsc.get<string>("username");
// Retrieve an object
const user = lsc.get<{ name: string; age: number }>("user");
// Remove an item by key
lsc.remove("username");
// Clear all items from storage
lsc.clear();
// Flush expired items (or force flush all with true)
lsc.flush();
lsc.flush(true);
Method/Function | Signature | Description | Parameters | Return Type |
---|---|---|---|---|
set | `set(key: string, value: T, localConfig?: Omit<StorageConfig, 'storage'>): void | boolean` | Stores a value in storage under the specified key. | key: string value: T localConfig (optional): Configuration object (excluding storage ) |
get | `get(key: string, localConfig?: Omit<StorageConfig, 'storage'>): T | null` | Retrieves the value associated with the specified key. | key: string localConfig (optional): Configuration object (excluding storage ) |
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 |
KeyValuePair | type KeyValuePair<T = unknown> = Record<string, T>; |
Type representing an object with string keys and values of type T . |
None | Type definition |
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
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.
This project is licensed under the MIT License
- Name: Sabbir Hossain Shuvo
- Email: [email protected]
- GitHub: @devlopersabbir
- Buy Me a Coffee: @devlopersabbir
- 🔮 Session Storage: Coming soon!
- 🍪 Cookie Storage: On the roadmap!