New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@oyvindher/preserve

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@oyvindher/preserve

A handy localstorage util

  • 0.3.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Preserve

A state lib for keeping track of and change localStorage data

Build Status PRs Welcome

Install

 npm install @oyvindher/preserve

or with Yarn

yarn add @oyvindher/preserve

API

preserve(key:string, initialData?: any)

The preserve function gives you 4 methods in return:

  • get<T>() Gives the current state of your localstorage item.
  • set<T>(data:T) Set the current state of your localstorage item.
  • subscribe(callback: (prevData, nextData) => void) Gives you a callback to listen to current state changes
  • clearItem() Clears the localStorge item

Basic usage

import preserve from '@oyvindher/preserve';

// Make an item you want to keep track of.
// The initial state is optional.
const myItem = preserve('myData', 0);
// Get the current data from localStorage key
myItem.get(); // 0
// Update the localStorage data.
myItem.set(1);
// Listen to changes that happens within your localStorage item
myItem.subscribe((prevData, nextData) => {
  console.log(prevData); // 0
  console.log(nextData); // 1
});
// Clear the item from localStorage
myItem.clearItem();

preserveLogger

PreserveLogger is a handy util for development for logging out previous and next state of any given preserved item when it changes.

This is how you would use it:

import preserve, { preserveLogger } from '@oyvindher/preserve';

const myItem = preserve('myItem');

// Pass the item you want to log.
preserveLogger(myItem);

FAQs

Package last updated on 20 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc