npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

storage-util

v1.1.4

Published

整合sessionStorage、localStorage、cookie的小插件

Downloads

23

Readme

storage-util npm

一个整合sessionStorage、localStorage、cookie的小插件,你可以更方便地使用这三个存储对象,也无需担心因兼容性问题而导致页面崩溃报错。

安装

npm install storage-util --save

使用方式

相关参数

/*
  type: 可选 值->sessionStorage(0)、localStorage(1)、cookie(2)之一,默认sessionStorage(0)
  success: 可选 设置成功后的回调,注意要放在对象里,下同
  fail: 可选 设置失败后的回调
  
  方法:
    get 获取值
    set 设置值
    remove 删除值
    isSupport 判断是否支持sessionStorage/localStorage/cookie
    setType 修改type
*/
var storage = new StorageUtil(type,{
  success:function(){
    console.log('成功了');
  },
  fail:function(){
    console.log('失败了');
  }
});

环境检测

注意:在兼容性方面,工具并不会自动降级处理,如果需要,可以在回调函数中做相关操作

new StorageUtil().isSupport();//sessionStorage
// new StorageUtil('localStorage').isSupport(); 
new StorageUtil(1).isSupport();//localStorage
new StorageUtil(2).isSupport();//cookie

增删改查

设置cookie略有不同,可选设置时间

//sessionStorage
var storage = new StorageUtil();

storage.set('sessionStoragekey',1);
storage.get('sessionStoragekey');//1
sessionStorage.sessionStoragekey//1

//localStorage
var storage = new StorageUtil(1);

storage.set('localStoragekey',1);
storage.get('localStoragekey');//1
localStorage.localStoragekey//1

//cookie
var storage = new StorageUtil(2),
    time = 5 * 60 * 60 * 1000; //5小时,默认2小时

storage.set('cookiekey',1,time);
console.log(storage.get('cookiekey'));//1

链式调用

由于get为取值操作,这里的链式操作只能是set或remove

//set/remove
new StorageUtil().set('key1',1).set('key2',2).remove('key1').get('key2');//2

直接存储对象

无需手动转换对象数据

//sessionStorage/localStorage/cookie
new StorageUtil().set('obj',{'test':1}).get('obj')//{test: 1}
new StorageUtil(1).set('obj',{'test':1}).get('obj')//{test: 1}
new StorageUtil(2).set('obj',{'test':1}).get('obj')//{test: 1}

批量操作

批量设置cookie时,time参数往前移一位

//批量get
new StorageUtil().get('key1,key2');
//批量set sessionStorage/localStorage
new StorageUtil().set({ke1:1,key2:2});
//批量set cookie time 可选
var time = 5 * 60 * 60 * 1000;

new StorageUtil(2).set({ke1:1,key2:2},time);
//批量删除 sessionStorage/localStorage/cookie
new StorageUtil().remove('key1,key2');

变换type

只需一行代码,就可以玩转三个存储对象

new StorageUtil().set('key1',1).setType(1).set('key2',2).
	.setType(2).set('key3',3)

无限链式

new StorageUtil().set('msg','你翩翩地路过').get('msg',function(msg){
    console.log(msg);
  }).setType(1).set('msg','以为不曾留下什么').get('msg',function(msg){
    console.log(msg);
  }).setType(2).set('msg','却在我心里有了思念').get('msg',function(msg){
    console.log(msg);
  }).setType().set('msg','若你还记得').get('msg',function(msg){
    console.log(msg);
  }).setType(1).set('msg','那个蝉鸣的夏天').get('msg',function(msg){
    console.log(msg);
  }).setType(2).set('msg','有一个你,也有一个我').get('msg',function(msg){
    console.log(msg);
  })

无限链式+批量

new StorageUtil().set({key3:3,key4:4}).get('key3,key4',function(key3,key4){
    console.log(key3,key4);
  }).remove('key3,key4');

清空所有

new StorageUtil().clear();
new StorageUtil(1).clear();
new StorageUtil(2).clear();

License

MIT