Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【开源自荐】stokado 借助 proxy,更好地更方便地管理 storage,实现了相关语法糖、序列化、监听订阅、设置过期、一次性取值等功能 #2402

Open
KID-joker opened this issue Oct 15, 2022 · 0 comments

Comments

@KID-joker
Copy link

KID-joker commented Oct 15, 2022

推荐项目

  • 项目地址:https://github.com/KID-joker/stokado

  • 类别:JS

  • 项目标题:使用proxy封装localStorage、sessionStorage

  • 项目描述:借助 proxy,更好地更方便地管理 storage,实现了相关语法糖、序列化、监听订阅、设置过期、一次性取值等功能

  • 亮点:

  1. 保存到sotrage里的值,取值时类型不会改变,存值前的类型为number,取值后的类型也是number
  2. 可以像正常对象数组类型一样,直接操作对象和数组,不用再JSON解压。
  3. 监听数据变化。对于对象数组,还支持二级监听,即obj.alist[0],以及数组长度的监听。
  4. 设置过期时间,轻松实现指定时间过期的逻辑。
  5. 一次性取值。
  • 示例代码:
import { local, session } from 'stokado';

local.test = 'Hello stokado'; // 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 = 'stokado'; // works

// array
local.test = ['hello'];
local.test.push('stokado'); // 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 stokado!';
};
local.test() === 'Hello stokado!'; // 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.setItem('test', 'hello stokado', { expires: Date.now() + 10000 })
// local.test = 'hello stokado'
// local.setExpires('test', Date.now() + 10000)

// within 10's
local.test // 'hello stokado'

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

/******************** Disposable ********************/
local.setItem('test', 'hello stokado', { disposable: true })
// local.test = 'hello stokado'
// local.setDisposable('test')

local.test // 'hello stokado'
local.test // undefined
  • 后续更新计划:
  1. 完善项目发布流程
@KID-joker KID-joker changed the title 【开源自荐】proxy-web-storage:使用proxy封装localStorage、sessionStorage,让其功能更丰富。 【开源自荐】stokado 借助 proxy,更好地更方便地管理 storage,实现了相关语法糖、序列化、监听订阅、设置过期、一次性取值等功能 Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants