0.0.4 • Published 2 months ago

lsc-storage v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

🚀 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.

npm version License: MITGitHub issuesGitHub stars


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/FunctionSignatureDescriptionParametersReturn Type
setset<T>(key: string, value: T, localConfig?: Omit<StorageConfig, 'storage'>): void | booleanStores a value in storage under the specified key.key: stringvalue: TlocalConfig (optional): Configuration object (excluding storage)void if successful, false if error occurs
getget<T>(key: string, localConfig?: Omit<StorageConfig, 'storage'>): T | nullRetrieves the value associated with the specified key.key: stringlocalConfig (optional): Configuration object (excluding storage)Returns the stored value of type T or null
removeremove(key: string): voidRemoves the item associated with the specified key from storage.key: stringvoid
clearclear(): voidClears all items from storage.Nonevoid
flushflush(force?: boolean): voidClears expired items from storage. If force is true, clears all items regardless of expiration.force (optional): boolean (defaults to false)void
localMemoryStorelocalMemoryStore(): StorageCreates an in-memory storage object that implements the Storage interface. Used as a fallback mechanism.NoneStorage
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 millisecondsInterface (no return value)
KeyValuePairtype KeyValuePair<T = unknown> = Record<string, T>;Type representing an object with string keys and values of type T.NoneType definition

Development

To set up the development environment:

  1. Clone the repository:
     git clone https://github.com/devlopersabbir/lsc-storage.git
     cd lsc-storage
  2. Install dependencies:
    npm install
  3. Run the development server:
    npm run dev
  4. Run tests:
    npm test
  5. Build the project:
    npm run build

Contributing

Contributions are welcome! 🎉 Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Commit your changes (git commit -m 'Add YourFeature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a pull request.

For detailed contribution guidelines, refer to the CONTRIBUTING.md file.

License

This project is licensed under the MIT License

Author

Future Enhancements

  • 🔮 Session Storage: Coming soon!
  • 🍪 Cookie Storage: On the roadmap!