0.0.2 • Published 4 years ago

with-storage v0.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

with-storage

A package that auto binds object or class properties to local-storage for persistance and easy manipulation.

Installation Instructions

$ yarn add with-storage

Example

import { withStorage } from "with-storage";

const user = withStorage({
  isLoggedIn: false,
  email: "",
  sports: ["football"],
  health: { height: "6ft", eyeColor: "blue" },
  updateName: () => {}, // <-- funcs are ignored
});

user.email = "somemail@gmail.com"; // <-- cookie created

console.log(user.email); //  <-- 'somemail@gmail.com'
// RESTART application and remove setting your email above - try logging the same property
console.log(user.email); // <-- 'somemail@gmail.com' is still there

// DESTROY cookie simply just re-assign the value to "", undefined, or delete obj.key.
// setting value to undefined reverts to static defaults upon reload
user.email = "";

class example

import { withStorage, getStorageItem } from "with-cookie";

@withStorage
class User {
  isLoggedIn: false,
  email: "",
};

const user =  new User()

user.email = "somemail@gmail.com";

// check to see if cookie exist with cookie util.
// cookies are stored formatted `${constructor.name || config.name}_{$key}`
console.log(getStorageItem("User_email")); // <-- 'somemail@gmail.com'

Available Configuration

paramdefaulttypedescription
nameconstructor.namestringOptional: A keyname for cookie storage if anonymous object.

Example adjusting configuration. Simply pass in the object as the second param.

const User = {
  isLoggedIn: false,
  email: "",
  card: {
    number: "",
    exp: "",
    cvc: "",
  },
};

const user = withStorage(User, { noStorage: ["card"] });