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

lin-storage

v1.0.1

Published

响应式localStorage实现

Downloads

5

Readme

说明

lin-storage是仅1.03k的单文件js模块,灵感来源于antfu的@vueuse/core库中的useLocalStorage,因为我在使用时发现了个问题,感兴趣的可以看看issues#1283

你可以将想要保存的数据初始化为一个变量,然后就可以通过这个变量更新和获取值并自动同步localStorage,并且在更新和获取数据时不再需要像localStorage那样关心JSON转换,api设计基本跟localStorage一致,具体见API参考

快速使用

1、安装

npm install lin-storage

2、使用案例(这里以pinia为例)

import { defineStore } from "pinia"
import { linStorage } from 'lin-storage'
export const useTestStore = defineStore('test', {
    state() {
        return {
            userData: linStorage.init('userData', {}),   //初始化一个object数据
            token: linStorage.init('token', '')     //初始化一个基本类型数据
        }
    },
    actions: {
        changeUserData() {
            //object批量更新
            this.userData.setValue({name: 'lin'})
            this.userData.value = {name: 'lin'}
            //object局部更新(数组暂不支持)
            this.userData.setItem('name','lin')
            this.userData.name = 'lin'
            //基本类型更新
            this.token.setValue('this is token')
            this.token.value = 'this is token'
            //取值
            console.log(this.userData)          //{name: 'lin'}
            console.log(this.userData.name)     //lin
            //基本类型数据通过value获取)
            console.log(this.token.value)       //this is token
            //你也可以直接使用linStorage全局操作所有值
            linStorage.token.setValue('new token')
            linStorage.clear()  //清空所有
        }
    }
})

API参考

1、属性

| 属性名 | 类型 | 说明 | | --- | --- | --- | | length | Number | 存储的数据条数 | | value | --- | 主要用来取基本类型数据的值;也可以设置值,但其实是调用了setValue方法,结合性能和语义化建议使用setValue |

1、方法

| 方法名 | 参数 | 说明 | 返回值 | | --- | --- | --- | --- | | init | key, value | 传入key和defaultValue初始化一项数据;如果key已经存在则初始化默认值无效 | 被监听后的Proxy实例| | getItem | key | 获取指定值 | 指定值 | | setItem | key, value | 设置指定值 | 指定值 | | removeItem | key | 删除指定值 | 指定值 | | clear | --- | 清空值 | --- | | setValue | value | 批量更新值 | 更新后的值 |