GitHub-Chinese-Top-Charts icon indicating copy to clipboard operation
GitHub-Chinese-Top-Charts copied to clipboard

【开源自荐】proxy-web-storage:使用proxy封装localStorage、sessionStorage,让其功能更丰富。

Open KID-joker opened this issue 2 years ago • 0 comments

  • 项目名称:proxy-web-storage

  • 项目地址:https://github.com/KID-joker/proxy-web-storage

  • 项目简介 :借助proxy,扩展了web storage的功能,使用起来,更加方便快捷,也更加强大。主要功能为保持值类型不变,可直接操控Object、Array,支持监听数据变化和设置过期时间

  • 示例代码:

import { local, session } from 'proxy-web-storage';

local.test = 'Hello proxy-web-storage'; // works
delete local.test; // works

// number
local.test = 0;
local.test === 0; // true

// boolean
local.test = false;
local.test === false; // true

// undefined
local.test = undefined;
local.test === undefined; // true

// null
local.test = null;
local.test === null; // true

// object
local.test = { hello: 'world' };
local.test.hello = 'proxy-web-storage'; // works

// array
local.test = ['hello'];
local.test.push('proxy-web-storage'); // works
local.test.length // 2

// Date
local.test = new Date('2000-01-01T00:00:00.000Z');
local.test.getTime() === 946684800000; // true

// RegExp
local.test = /d(b+)d/g;
local.test.test("cdbbdbsbz"); // true

// function
local.test = function() {
  return 'Hello proxy-web-storage!';
};
local.test() === 'Hello proxy-web-storage!'; // true

/******************** Subscribe ********************/
local.on('test', function(newVal, oldVal) {
  console.log('test', newVal, oldVal);
});
local.on('test.a', function(newVal, oldVal) {
  console.log('test.a', newVal, oldVal);
});

local.test = {};
// test {} undefined

local.test.a = 1;
// test.a 1 undefined

/******************** Expired ********************/
local.test = 'hello proxy-web-storage';
local.setExpires('test', Date.now() + 10000);

// within 10's
local.test // 'hello proxy-web-storage'

// after 10's
local.test // undefined

KID-joker avatar Oct 15 '22 03:10 KID-joker