@mewters/smart-storage
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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.

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

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();

smart-storage

smart-storage

Package Sidebar

Install

npm i @mewters/smart-storage

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

25.6 kB

Total Files

23

Last publish

Collaborators

  • hanashiro