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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@xiaohaih/storage

v0.0.1

Published

本项目提供了一个灵活的本地存储工具, 基于 `TypeScript` 实现, 支持 `localStorage`, `sessionStorage` 及自定义存储; 通过 `StorageCore` 类, 可以方便地进行数据的序列化, 反序列化, 存储和读取。

Downloads

8

Readme

存储库说明

本项目提供了一个灵活的本地存储工具, 基于 TypeScript 实现, 支持 localStorage, sessionStorage 及自定义存储; 通过 StorageCore 类, 可以方便地进行数据的序列化, 反序列化, 存储和读取。

特性

  • 支持 localStorage, sessionStorage 或自定义存储实现
  • 支持键和值的序列化与反序列化
  • 提供 setItem, getItem, removeItem, clear 等常用方法
  • 可自定义序列化/反序列化逻辑

快速上手

import { local, session, StorageCore } from '@xiaohaih/storage';

// 使用 localStorage
local.setItem('key', { a: 1 });
const value = local.getItem('key'); // { a: 1 }
local.removeItem('key');
local.clear();

session.setItem('key', { a: 1 });
const value2 = session.getItem('key'); // { a: 1 }
session.removeItem('key');
session.clear();

// 使用自定义存储实现
const customProvider = {
    setItem: (key, value) => { /* ... */ },
    getItem: (key) => { /* ... */ },
    removeItem: (key) => { /* ... */ },
    clear: () => { /* ... */ },
};
const customStorage = new StorageCore({ storageProvider: customProvider });

构造参数 StorageOption

  • serializeKey/deserializeKey: 键的序列化/反序列化函数
  • serializeValue/deserializeValue: 值的序列化/反序列化函数
  • storageProvider: 自定义存储实现, 需实现 setItem/getItem/removeItem/clear 接口

工具函数

  • noop(value):原样返回输入
  • stringify(val):将值序列化为 JSON 字符串
  • parse(val, defaultValue):将字符串解析为对象, 失败时返回默认值或 null

预设实例

  • local: localStorage 封装实例
  • session: sessionStorage 封装实例

运行测试

本项目使用 Vitest 进行单元测试

pnpm install
pnpm test
# 或
npx vitest

目录结构

  • src/storage.ts:核心存储实现
  • src/storage.test.ts:单元测试

如有建议或问题, 欢迎提 issue!