npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smart-web-storage

v0.0.11

Published

localstorage操作工具

Downloads

18

Readme

smart-web-storage

webStorage(localStorage/sessionStorage)中只能存取字符串,但是在很多情况下我们需要在一个key中存储JSON格式的数据,这时对JSON格式数据的读写就非常麻烦,需要:

  1. 读取webStorage对应key的字符串值
  2. 对字符串值进行解析
  3. 获取对象对应key的值
  4. 修改解析后的对象
  5. 序列化对象为字符串
  6. 将字符串写入webStorage中
  7. 添加try..catch..防止读写出错

使用smart-web-storage可以通过操作对象属性的方式更加方便的对webStorage进行读写。

注意:smart-web-storage库基于Proxy实现,所以需要浏览器环境支持Proxy。

注意:smart-web-storage不会将webStorage第一层的字符串作为JSON数据解析,a: 1,这里的1是字符串"1",而不是数字1。boolean值同理a: true,"true"是字符串而不是boolean值。

安装

npm install smart-web-storage
yarn add smart-web-storage

使用

import lstorage, { sstorage } from 'smart-web-storage'

注:lstorage表示localstorage, sstorage表示sessionStorage,两者用法相同。

1. 正常使用

// localStorage
// a: 1
lstorage.a() // '1'
lstorage.a = 2
// localStorage
// a: 2
lstorage.a() // '2'
lstorage.b.c = 1
// localStorage
// a: 2
// b: {"c": 1}
lstorage.b() // {c: 1}
lstorage.b.e = 2
lstorage.b() // {c: 1, e: 2}
lstorage.b.b() // undefined
lstorage.b.b.b() // undefined

// localStorage
// empty
lstorage.c = {c1: 1, c2: 2} 
// localStorage
// c: {"c1": 1, "c2": 2}
lstorage.c.c1() // 1

2. 获取不存在的localStorage值和使用默认值

// localStorage值如下:
// a: 1
lstorage.notfound() // null

lstorage.notfound(1) // 1

lstorage.notfound((v) => 1) // 1

3. 删除值

// localStorage
// c: {"c1": 1, "c2": 2}
delete lstorage.c.c1 
// localStorage
// c: {"c2": 2}
delete lstorage.self // 清楚localStorage所有值
// localStorage
// empty

4. 获取全部localStorage值

// localStorage值如下:
// c: 1
// b: {"a": 1}
lstorage() // {c: "1", b: {a: 1}}

5. 设置全部localStorage值

// localStorage
// empty
lstorage.self = {a: 1, b: {a: 1}} 
// localStorage
// a: 1
// b: {"a": 1}

// localStorage
// empty
lstorage.self = [1, {a: 1}] 
// localStorage
// 0: 1
// 1: {"a": 1}

注意:webStorage的读取和写入都有失败风险,所以设置和读取都有可能出现意料之外的情况,虽然smartWebStoreage会兜底异常,但是异常后读取的值无法兜底需要用户手动处理。

License

MIT