import Lockr from 'lockr'

[](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
```