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

jsonify-storage

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonify-storage

Jsonify the payload for local and session storage.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

jsonify-storage

Build Status npm module Coverage Status Dependency Status

Introduction

There is a unfriendly case when we use the the native localStorage, such as

localStorage.setItem('foo', { a: 123 })
localStorage.getItem('foo') // => [object Object]

localStorage.setItem('bar', 123)
localStorage.getItem('bar') // => '123'

So, jsonify it!

Install

⚠Notice: Can only works in browser environment, other scene need define global variable to mock Storage.

// In node you have to write like this.

global.localStorage = yourLocalStorage
global.sessionStorage = yourSessionStorage

Node:

npm i -D jsonify-storage

Browser:

<script src="./dist/jsonify-storage.umd.min.js"></script>

Usage

Import in Node:

// es mode
import { local, session } from 'jsonify-storage'
// or cjs mode
const { local, session } = require('jsonify-storage')

In Browser:

const { local, session } = jsonifyStorage
  • local means localStorage
  • session means sessionStorage

Then you can use it like this:

local.set('foo', { a: 123 })
local.get('foo') // => { a: 123 }
local.get('foo').a === 123 // => true, will retain the type.

Payload will be json-stringified before save.

Raw data seems like this:

localStorage.getItem('foo') // => '{"a":123}'

If you try to get something can't parsed by JSON or not existed will return null

localStorage.setItem('bar', '我我我')
local.get('bar') // => null
local.get('baz') // => null

Others apis sames as native Storage, list below.

  • setItem -> set
  • getItem -> get
  • removeItem -> remove
  • clear -> clear
  • key -> key
  • length -> length

More details see API documentation.

Keywords

FAQs

Package last updated on 27 Apr 2020

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