# 封装常用持久存储

# sessionStorage

// sessionStorage

let session = {};

const debug = process.NODE_ENV != "production";

session.get = (key) => {
  let json = sessionStorage.getItem(key);
  let result = null;
  if (typeof json == "string") {
    result = JSON.parse(json);
  }
  return result;
};

session.set = (key, data) => {
  let json = JSON.stringify(data);
  if (debug) console.log("session.set", key, data);
  return sessionStorage.setItem(key, json);
};

session.remove = (key) => {
  if (debug) console.log("session.remove", key);
  return sessionStorage.removeItem(key);
};

session.clear = () => {
  if (debug) console.log("session.clear");
  return sessionStorage.clear(key);
};

export default session;

# localStorage

// localStorage

let local = {};
const debug = process.NODE_ENV != "production";

local.get = (key) => {
  let json = localStorage.getItem(key);
  let result = null;
  if (typeof json == "string") {
    result = JSON.parse(json);
  }
  return result;
};

local.set = (key, data) => {
  let json = JSON.stringify(data);
  if (debug) console.log("local.set", key, data);
  return localStorage.setItem(key, json);
};

local.remove = (key) => {
  if (debug) console.log("local.remove", key);
  return localStorage.removeItem(key);
};

local.clear = () => {
  if (debug) console.log("locals.clear");
  return localStorage.clear(key);
};

export default local;
// cookie
import Cookies from "js-cookie";

const cookie = {};

const debug = process.NODE_ENV != "production";

cookie.get = (key) => {
  return Cookies.get(key);
};

cookie.set = (key, data) => {
  if (debug) console.log("cookie.set", key, data);
  return Cookies.set(key, data);
};

cookie.remove = (key) => {
  if (debug) console.log("cookie.remove", key);
  return Cookies.remove(key);
};

export default cookie;

# localforge

TIP

TODO 待续