with-storage
v0.0.2
Published
auto bind object properties to store and retrieve values using local storage easily
Downloads
10
Maintainers
Readme
with-storage
A package that auto binds object or class properties to local-storage for persistance and easy manipulation.
Installation Instructions
$ yarn add with-storageExample
import { withStorage } from "with-storage";
const user = withStorage({
isLoggedIn: false,
email: "",
sports: ["football"],
health: { height: "6ft", eyeColor: "blue" },
updateName: () => {}, // <-- funcs are ignored
});
user.email = "[email protected]"; // <-- cookie created
console.log(user.email); // <-- '[email protected]'
// RESTART application and remove setting your email above - try logging the same property
console.log(user.email); // <-- '[email protected]' 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 = "[email protected]";
// check to see if cookie exist with cookie util.
// cookies are stored formatted `${constructor.name || config.name}_{$key}`
console.log(getStorageItem("User_email")); // <-- '[email protected]'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"] });