simple-browser-store
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

基于 key-value 键值对(object 对象)的 cookie/localStorage/sessionStorage 浏览器存储

  1. 安装 ,npm / yarn 安装
npm install simple-browser-store
yarn add simple-browser-store
  1. 使用
import { getData, setData, removeData } from 'simple-browser-store';

// cookie 存储
const appKey = '__app__';
const cookieData = getData('cookie', appKey);
setData('cookie', appKey, { newProp: 'new prop value' });

// localStorage 存储
const localStorageData = getData('localStorage', appKey);
setData('localStorage', appKey, { newProp: 'new prop value' });
  1. typescript 类型定义
export declare type StorageType = 'cookie' | 'localStorage' | 'sessionStorage';

export declare const getData: (type: StorageType, key: string) => Record<string, unknown>;

export declare const setData: (
  type: StorageType,
  key: string,
  value: Record<string, unknown>,
  cookieOptions?: CookieAttributes
) => void;

export declare const removeData: (type: StorageType, key: string,cookieOptions?: CookieAttributes) => void;
  1. getData 返回包含 key-value 键值对的 object 对象(不会为 null)

  2. setData 以对象扩展 (Object.assign)方式更新或者添加 key-value pair

Package Sidebar

Install

npm i simple-browser-store

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

13.6 kB

Total Files

9

Last publish

Collaborators

  • leonwgc