import Lockr from 'lockr'
![Lockr logo](http://i.imgur.com/m5kPjkB.png)
[![Code
Climate](https://codeclimate.com/github/tsironis/lockr/badges/gpa.svg)](https://codeclimate.com/github/tsironis/lockr)
> 一个用于本地存储的最小API包装器。就像你的高中橱柜一样简单。
Lockr (发音 /ˈlɒkəʳ/) 是一个非常轻量级的库 (<2kb 当压缩后),设计用于方便您与localStorage的交互。保存 objects and arrays, numbers 或者其他数据类型, accessible via a Redis-like API, heavily inspired by [node_redis](https://github.com/mranney/node_redis/).
## How to use lockr
In order to user lockr, you firstly need to install it in your project.
```js
bower install lockr
```
或者从这里手动下载 [here](https://raw2.github.com/tsironis/lockr/master/lockr.js) and hook it in your HTML.
```html
<script src="/path/to/lockr.js" type="text/javascript"></script>
```
## API 参考
```Lockr.set``` - 参数: *[ key, value ]* {String, Number, Array or Object}
> 在一个散列键下设置一个特定值或散列对象 (```Object``` or ```Array```)
*例子*
```js
Lockr.set('username', 'Coyote'); // 保存为字符串
Lockr.set('user_id', 12345); // 保存为数字
Lockr.set('users', [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]);//保存为对象
```
---
```Lockr.get``` - 参数: *[ key or hash_key, default value ]*
> 返回给定键的保存值,即使保存的值是hash object.。如果值为null或未定义,则返回一个默认值。
*例子*
```js
Lockr.get('username');
> "Coyote"
Lockr.get('user_id');
> 12345
Lockr.get('users');
> [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]
Lockr.get('score', 0):
> 0
Lockr.set('score', 3):
Lockr.get('score', 0):
> 3
```
---
```Lockr.rm``` - 参数: *[ key ]* {String}
> 从```本地存储```localStorage完全删除一个密钥。
*例子*
```js
Lockr.set('username', 'Coyote'); //保存为字符串
Lockr.get('username');
> "Coyote"
Lockr.rm('username');
Lockr.get('username');
> undefined
```
---
```Lockr.sadd``` - 参数 *[ key, value ]*{String, Number, Array or Object}
>在一个散列键下添加一个惟一的值。 Adds a unique value to a particular set under a hash key.
*例子*
```js
Lockr.sadd("wat", 1); // [1]
Lockr.sadd("wat", 2); // [1, 2]
Lockr.sadd("wat", 1); // [1, 2]
```
---
```Lockr.smembers``` - 参数 *[ key ]*
> Returns the values of a particular set under a hash key.
*例子*
```js
Lockr.sadd("wat", 42);
Lockr.sadd("wat", 1337);
Lockr.smembers("wat"); // [42, 1337]
```
---
```Lockr.sismember``` - 参数 *[ key, value ]*
> 返回值是否存在于一个散列键下的特定集合中值。Returns whether the value exists in a particular set under a hash key.
*例子*
```js
Lockr.sadd("wat", 1);
Lockr.sismember("wat", 1); // true
Lockr.sismember("wat", 2); // false
```
---
```Lockr.srem``` - 参数 *[ key, value ]*
> 从散列键下的特定集合中删除一个值。Removes a value from a particular set under a hash key.
*例子*
```js
Lockr.sadd("wat", 1);
Lockr.sadd("wat", 2);
Lockr.srem("wat", 1);
Lockr.smembers("wat"); // [2]
```
---
```Lockr.getAll``` - 参数: *null*
> 返回所有保存的值和对象,在一个数组中。Returns all saved values & objects, in an ```Array```
*例子*
```js
Lockr.getAll();
> ["Coyote", 12345, [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]]
```
---
```Lockr.flush()``` - 参数: *null*
>清空本地存储。 Empties localStorage().
*例子*
```js
localStorage.length;
> 3
Lockr.flush();
localStorage.length;
> 0
```