JSPM

simple-browser-store

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q21168F
  • License MIT

storage in browser

Package Exports

  • simple-browser-store

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-browser-store) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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