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

storages-proxy

v2.1.0

Published

A simple, lightweight JavaScript API for handling browser storages

Downloads

29

Readme

storages-proxy

localStoragesessionStorage 添加作用域,避免全局污染,特别适合在微前端等项目场景中使用。

英文文档

目录

安装

使用 npm 安装:

npm install storages-proxy

或使用 pnpm 安装:

pnpm add storages-proxy

API

createGlobalStorage

createGlobalStorage(type: Type, options?: Partial<Options>): void

创建指定类型的全局存储,并设置作用域。

参数

  • type (Type): 存储类型,可以是 localStoragesessionStorage
  • options (Partial<Options>, 可选): 存储的属性配置,包括路径。

updateGlobalStorage

updateGlobalStorage(type: Type, options?: Partial<Options>, force?: boolean): void

更新指定类型的全局存储的作用域。

参数

  • type (Type): 存储类型,可以是 localStoragesessionStorage
  • options (Partial<Options>, 可选): 存储的属性配置,包括路径。
  • force (boolean, 可选): 是否重置属性配置。

示例

import {
  length,
  createGlobalStorage,
  updateGlobalStorage,
} from 'storages-proxy';

// 创建 `默认作用域为当前路径` 的 localStorage 和 sessionStorage
createGlobalStorage('localStorage');
createGlobalStorage('localStorage', { include: [ 'key' ] });

// 创建 `默认作用域为指定路径` 的 localStorage 和 sessionStorage
createGlobalStorage('localStorage', { path: '/path' });
createGlobalStorage('localStorage', { path: '/path', include: [ 'key' ] });

// 更新 localStorage 的默认作用域为当前路径
updateGlobalStorage('localStorage');

// 更新 localStorage 的默认作用域为指定路径
updateGlobalStorage('localStorage', { path: '/new-path' });

// 设置一个键值对,使用默认作用域
localStorage.key = 'value';
// 或
localStorage.setItem('key1', 'value1');

// 设置一个键值对,使用指定作用域
localStorage.setItem('key2', 'value2', '/path');

// 设置一个键值对,不使用作用域
localStorage.setItem('key3', 'value3', null);

// 获取一个键值对,使用默认作用域
console.log(localStorage.key); // 输出 'value'
// 或
console.log(localStorage.getItem('key1')); // 输出 'value1'

// 获取一个键值对,使用指定作用域
console.log(localStorage.getItem('key2', '/path')); // 输出 'value2'

// 获取一个键值对,不使用作用域
console.log(localStorage.getItem('key3', null)); // 输出 'value3'

// 移除一个键值对,使用默认作用域
delete localStorage.key;
// 或
localStorage.removeItem('key1');

// 移除一个键值对,使用指定作用域
localStorage.removeItem('key2', '/path');

// 移除一个键值对,不使用作用域
localStorage.removeItem('key3', null);

// 获取默认作用域下第 n 个键名
localStorage.key(0);

// 获取指定作用域下第 n 个键名
localStorage.key(0, '/path');

// 获取第 n 个键名
localStorage.key(0, null);

// 清除默认作用域下的所有键值对
localStorage.clear();

// 清除指定作用域下的所有键值对
localStorage.clear('/path');

// 清除所有键值对
localStorage.clear(null);

// 获取默认作用域下的数据项数量
localStorage.length;
// 或
length('localStorage');

// 获取指定作用域下的数据项数量
length('localStorage', '/path');

// 获取数据项数量
length('localStorage', null);

类型定义

export type Type = 'localStorage' | 'sessionStorage';

export type Path = string | (() => string);

export type Options = { path: Path, include?: string[],  exclude?: string[] };

declare global {
  interface Window {
    __STORAGES_DEFAULT__: Record<Type, Options>;
    __STORAGES_INIT__: Record<Type, boolean>;
  }
}

许可

MIT License. 查看 LICENSE 文件了解更多信息。