simple-browser-store

storage in browser

Usage no npm install needed!

<script type="module">
  import simpleBrowserStore from 'https://cdn.skypack.dev/simple-browser-store';
</script>

README

基于 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