Git Product home page Git Product logo

pandora's Introduction

@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

  • Tiny (< 500B gzipped and minified).
  • 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

pandora's People

Contributors

aulisius avatar

Stargazers

Jay Lathia avatar Tushar Joshi avatar Divyanshu Dhruw avatar Karthick Ramjee avatar

Watchers

 avatar

Forkers

ananthakr

pandora's Issues

Add TS support

Currently the library has no TS support. It'd be useful to have types especially when working with localStorage types.

Example of how the TS API could look like

import pandora from "@faizaanceg/pandora";

type StorageSchema = {
   username: string;
}

pandora.set<StorageSchema>("username", "pandora"); // this'll work
pandora.set<StorageSchema>("username",  1); // this shoud fail

let name: string = pandora.get<StorageSchema>("username"); // this'll work
let name: string = pandora.get<StorageSchema>("username", "pandora"); // this'll work
let name: string = pandora.get<StorageSchema>("username", 1); // this shoud fail

// Also, it should work w/o any type either
let name = pandora.get("username");

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.