Skip to content

aulisius/pandora

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@faizaanceg/pandora

A tiny wrapper over LocalStorage to improve DX.

Motivation

Ever felt that localStorage is good but it could be better? This library bridges that gap and gives you a pleasant experience when working with localStorage.

Benefits

  • Smooth use with objects.
  • Expirable values (configurable with ttl option).

Installation

npm install @faizaanceg/pandora --save

or

yarn add @faizaanceg/pandora

Usage

import pandora from "@faizaanceg/pandora";

// Set an item

/*
  localStorage.setItem("username", "pandora");
*/
pandora.set("username", "pandora");

// Get an item

/*
  let value = localStorage.getItem("key");
*/
let value = pandora.get("key");

// Managing default values

/*
  let defaultValue = 1;
  let count = localStorage.getItem("count") || defaultValue;
*/
let count = pandora.get("count", 1);

// Dealing with objects

/*
  let object = { someKey: "value" };
  localStorage.setItem("object", JSON.stringify(object));

  let fromStorage = JSON.parse(localStorage.getItem("object"));
  console.log(fromStorage.someKey); // value
*/
let object = { someKey: "value" };
pandora.set("object", object);

let fromStorage = pandora.get("object"); // JSON.parse happens internally
console.log(fromStorage.someKey); // value;

// Clear items

/*
  localStorage.clear()
*/
pandora.clear();

// Persist values

/*
  let value  = localStorage.getItem("key");
  localStorage.clear();
  localStorage.setItem("key", value);
*/
pandora.set("key", value, { shouldPersist: true });
pandora.clear();
pandora.get("key") === value; // true

For more examples, you can check out the index.test.ts file.

Tests

npm install
npm test

Dependencies

None

License

MIT

About

A tiny 📦 to improve localStorage

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks