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 🙏

© 2026 – Pkg Stats / Ryan Hefner

matias-storage

v0.3.0

Published

A zero-dependency Web Storage wrapper: automatic JSON serialization, lossless Date/Map/Set/RegExp/BigInt round-trips, typed keys with TS inference, and optional runtime validation on read.

Readme

matias-storage

English

一个零依赖的 Web Storage 轻封装:自动 JSON 序列化/反序列化、内置引用类型无损往返、TypeScript 类型自动推断、可选的读取运行时校验。

特性

  • 零运行时依赖 —— 体积极小,适用于任何框架(或无框架)。
  • 自动 JSON 序列化 —— 直接存对象/数组/基础类型,无需手动 JSON.stringify
  • 引用类型无损往返(0.3.0 起)—— DateMapSetRegExpBigIntNaN±Infinity 与嵌套 undefined 读写后完整还原(instanceof 保持)。
  • 类型安全 key(0.3.0 起)—— defineStorageKey<T>() 让 TS 在读取时自动推断存储类型。
  • 运行时校验(0.3.0 起)—— storageRead 可传 type guard,校验失败告警并返回 null
  • 向后兼容 —— 0.2.x 写入的数据原样可读;普通值仍以裸 JSON 存储。

安装

$ pnpm add matias-storage

使用

基础用法(字符串 key,与 0.2.x 一致)

import { storageWrite, storageRead, storageRemove, storageRemoveAll, WebStorageType } from 'matias-storage'

storageWrite('MY_OBJECT', { value: 100 })                     // 默认 localStorage
storageWrite('MY_OBJECT', { value: 100 }, WebStorageType.SESSION) // sessionStorage

const value = storageRead<{ value: number }>('MY_OBJECT')     // { value: number } | null

storageRemove('MY_OBJECT')
storageWrite('MY_OBJECT', undefined)                          // 存 undefined 等同删除
storageRemoveAll()                                            // 清空 localStorage
storageRemoveAll(WebStorageType.SESSION)                      // 清空 sessionStorage

typed key —— 类型自动推断(0.3.0)

import { defineStorageKey, storageWrite, storageRead, WebStorageType } from 'matias-storage'

interface User { name: string; age: number }

const userKey = defineStorageKey<User>('USER')

storageWrite(userKey, { name: 'matias', age: 18 })
const user = storageRead(userKey)   // 类型自动推断为 User | null,无需传泛型

引用类型 —— 无需类型参数自动解析(0.3.0)

storageWrite('DATE', new Date())
storageRead('DATE')                 // Date 实例

storageWrite('MAP', new Map([['a', 1]]))
storageRead('MAP')                  // Map 实例,entries 完整

storageWrite('NESTED', {
  date: new Date(),
  map: new Map([['set', new Set([1n, 2n])]]),
  regexp: /ab+c/gi,
  maybe: undefined,                 // 读取时保留
})

说明:Array 本就支持往返;类实例按 JSON.stringify 语义序列化(自有可枚举属性、尊重 toJSON),不还原原型。

读取运行时校验(0.3.0)

const isUser = (v: unknown): v is User =>
  !!v && typeof v === 'object' && typeof (v as User).name === 'string'

storageRead('USER', WebStorageType.LOCAL, isUser)  // 通过返回 User
storageRead(userKey, isUser)                        // typed key 同样支持
// 校验失败:console.warn + 返回 null

使用 zod 的同学可包装 (v) => schema.safeParse(v).success 作为 guard,库本身不引入依赖。

支持的值

| 类型 | 存储格式 | 读取结果 | |---|---|---| | object / array | 裸 JSON | 普通对象/数组 | | string / boolean / number | 裸 JSON | 同类型基础值 | | null | 裸 JSON | null | | undefined(顶层) | ——(删除) | —— | | Date / Map / Set / RegExp / BigInt | 类型标签 JSON | 原始类型 | | NaN / Infinity / -Infinity | 类型标签 JSON | 原始值 | | 嵌套 undefined | 类型标签 JSON | undefined | | 循环引用、顶层 function/symbol | 拒绝 | false + console.warn |

storageRead 在 key 不存在、存值为 null、解析失败、guard 校验失败时均返回 null

保留字段

同时含有 __matias_tag____matias_value__ 且 tag 已知的数据会被视为内部类型标签并还原。请避免在自己的数据中使用这两个属性名;未知 tag 不会被还原。

说明

  • 数据量大时建议考虑 IndexedDB —— Web Storage 是同步 API 且有约 5MB 限制。
  • 版本历史见 CHANGELOG.md

许可

MIT