with-storage
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

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

param default type description
name constructor.name string Optional: 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"] });

Package Sidebar

Install

npm i with-storage

Weekly Downloads

3

Version

0.0.2

License

MIT

Unpacked Size

15.7 kB

Total Files

27

Last publish

Collaborators

  • jeffmendez