Coder Social home page Coder Social logo

storejs's Introduction

GitHub issues GitHub forks GitHub stars

安装

bower

$ bower install storejs

npm

$ npm install storejs

本地存储APIs

store(key, data);                 //单个存储字符串数据
store({key: data, key2: data2});  //批量存储多个字符串数据
store(key);                       //获取key的字符串数据
store();                          //获取所有key/data
//store(false);(弃用)🔫          //因为传入空值 或者报错很容易清空库
store(key,false);                 //删除key包括key的字符串数据

store.set(key, data[, overwrite]);//=== store(key, data);
store.setAll(data[, overwrite]);  //=== store({key: data, key2: data});
store.get(key[, alt]);            //=== store(key);
store.getAll();                   //=== store();
store.remove(key);                //===store(key,false)
store.clear();                    //清空所有key/data
store.keys();                     //返回所有key的数组
store.forEach(callback);          //循环遍历,返回false结束遍历

store.has(key);         //⇒判断是否存在返回true/false          


//⇒ 提供callback方法处理数据
store("test",function(arr){
    console.log(arr)//这里处理 通过test获取的数据
    return [3,4,5]//返回数据并存储
})

store(["key","key2"],function(arr){
    //获取多个key的数据处理,return 并保存;
    console.log("arr:",arr)
    return "逐个更改数据"
})

// 即创建/更新/删除数据项时,触发该事件
store.onStorage(function(key,val){
    console.log('onStorage:',key,val)
})

set

单个存储或删除字符串数据
store.set(key, data[, overwrite]);
效果相同store(key, data);

store.set("wcj","1")   //⇒  1
store.set("wcj")       //⇒  删除wcj及字符串数据

setAll

批量存储多个字符串数据
store.setAll(data[, overwrite])
效果相同store({key: data, key2: data});

store.setAll({
    "wcj1":123,
    "wcj2":345
}) //存储两条字符串数据

store.setAll(["w1","w2","w3"]) 
//存储三条字符串数据 
//  0⇒ "w1"
//  1⇒ "w2"
//  2⇒ "w3"

get

获取key的字符串数据
store.get(key[, alt])
效果相同store(key)

store.get("wcj1") //获取wcj1的字符串数据
store("wcj1") //功能同上

getAll

获取所有key/data
store.getAll()
效果相同store()

store.getAll() //⇒JSON
store() //功能同上

clear

清空所有key/data
store.clear()

弃用 store(false) 因为传入空值 或者报错很容易清空库

store.clear() //

keys

返回所有key的数组
store.keys()

store.keys() //⇒["w1", "w2", "w3"]

has

判断是否存在返回true/false
store.has(key)

store.has("w1"); //⇒true

remove

删除key包括key的字符串数据 store.remove(key)

store.remove("w1"); //删除w1 返回 w1的value

store("w1",false) //这样也是 删除w1

forEach

循环遍历,返回false结束遍历

store.forEach(function(k,d){
    console.log(k,d)
    if (k== 3) return false
})

定时清除

(做个笔记,未来将定时清除封装起来,有思路)

if (+new Date() > +new Date(2014, 11, 30)) {
    localStorage.removeItem("c");    //清除c的值
    // or localStorage.clear();
}

storage事件

不知为毛不支持参看下面 onStorage =>HTML5的本地存储,还提供了一个storage事件,可以对键值对的改变进行监听,使用方法如下:

if(window.addEventListener){
     window.addEventListener("storage",handle_storage,false);
}else if(window.attachEvent){
    window.attachEvent("onstorage",handle_storage);
}
function handle_storage(e){
    if(!e){e=window.event;}
    //showStorage();
}

对于事件变量e,是一个StorageEvent对象,提供了一些实用的属性,可以很好的观察键值对的变化,如下表:

Property Type Description
key String The named key that was added, removed, or moddified
oldValue Any The previous value(now overwritten), or null if a new item was added
newValue Any The new value, or null if an item was added
url/uri String The page that called the method that triggered this change

onStorage

即创建/更新/删除数据项时,触发该事件

store.onStorage(function(type,key,val){
   console.log("event:",type,key,val)
})

store({"a":1,"v":undefined})
//⇒ event: set a 1
//⇒ event: remove v 

链式书写

store.set('ad',234).get('ad')

store.onStorage(function(type){
 console.log('type:',type)
}).set('wcj',12).clear().setAll({"a":1,b:2}).get('wcj')
//⇒ type: set
//⇒ type: clear
//⇒ type: set

兼容

来源:sessionStorage localStorage

特性 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit) iPhone(IOS) Android Opera Mobile Window Phone
localStorage 4+ 3.5+ 8+ 10.50+ 4+ 3.2+ 2.1+ 11+ 8+
sessionStorage 5+ 2+ 8+ 10.50+ 4+ 3.2+ 2.1+ 11+ 8+

本地存储大小

JSON.stringify(localStorage).length 当前占用多大容量

检测localstore容量上限

storejs's People

Contributors

2betop avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

zhangkaivictor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.