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

storetify

v1.1.0

Published

Better localStorage, subscribable localStorage, expirable localStorage

Downloads

68

Readme

Build Status codecov license release

English | 简体中文

🦄本地存储localStorage的封装,提供过期时间设置和订阅功能,提供简单API使用,没有依赖,压缩只有 3.81KB(gzipped: 1.39KB)。

✨ Features

  • 比较好的localStorage, 也认为是下一代的localStorage
  • 易学易用
  • 支持数据的过期时间
  • 支持数据变化的监听
  • 使用 TypeScript 构建,提供完整的类型定义文件

🪄 安装

# npm 安装
npm install storetify

# yarn 安装
yarn add storetify

#pnpm 安装
pnpm add storetify

🏗️ 构建

npm run build

🧪 测试

npm test

🔨 使用

import store from 'storetify';
store("test","storetify");

或者在您的HTML中手动下载并引入 storetify.min.js,你也可以通过 UNPKG 进行下载:

<script src="https://unpkg.com/storetify/lib/storetify.min.js"></script>
<script type="text/javascript">
Storetify("test","storetify");
</script>

⚙️ API

set

存储数据 store.set(key, data[, expires]); 效果相同store(key, data[, expires]);

store.set("test","1"); //⇒1
store.set("test","1",3); //⇒1 3秒后test过期

get

获取key的字符串数据 store.get(key) 效果相同store(key)

store.get("test"); // 获取test的字符串数据
store("test"); // 功能同上

remove

删除key下的数据 store.remove(key)

store.remove("test");

clear

清空所有 key/data store.clear()

store.clear(); // 会发布所有key值的变化

has

判断是否存在并返回 true/false store.has(key)

store.has("test"); //⇒true

subscribe

订阅test的数据变化

store.subscribe("test",(e)=>{})

对于事件变量e,是一个来自StorageEvent对象的简略对象,提供了一些实用的属性,可以很好的观察键值对的变化,如下表:

type StoretifyEventValue = Record<string, any> | any[] | null | string | number

| Property | Type | Description| | -------- | ------ | ------------------------------------------------------------ | | key| string | 存储值的键,根据其修改、删除| | oldValue | StoretifyEventValue | 上一次的值 | | newValue | StoretifyEventValue | 当前新的值 | | type| string | 事件类型 | | native | StorageEvent | 原生事件 |

unsubscribe

取消订阅test的数据变化

const someName = (e)=>{}
store.subscribe("test",someName)
store.unsubscribe("test",someName) // ⚠️注意,取消订阅不能是匿名方法
store.unsubscribe("test") // ⚠️注意,会取消test的所有订阅包括匿名函数

getUsed

获取store的存储用量

store.getUsed() // 返回 `0.111 KB`

兼容

来源:localStorage

| 特性 | Chrome | Firefox (Gecko) | Internet Explorer | Opera| Safari (WebKit) | iPhone(IOS) | Android | Opera Mobile | Window Phone | | ------------ | ------ | --------------- | ----------------- | ------ | --------------- | ----------- | ------- | ------------ | ------------ | | localStorage | 4+ | 3.5+| 8+| 10.50+ | 4+| 3.2+| 2.1+| 11+| 8+ |

本地存储大小

JSON.stringify(localStorage).length 当前占用多大容量

检测localstore容量上限