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

Provide option for getAll method so that it includes keys, enhancement #34 #40

Merged
merged 3 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- 6.9.2
branches:
only:
- master
before_script:
- npm install
script:
- npm run build
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
![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)


> A minimal API wrapper for localStorage. Simple as your high-school locker.

[![Build Status](https://travis-ci.org/tsironis/lockr.svg?branch=master)](https://travis-ci.org/tsironis/lockr)
[![npm version](https://badge.fury.io/js/lockr.svg)](http://badge.fury.io/js/lockr)
[![CodeClimate](https://codeclimate.com/github/tsironis/lockr/badges/gpa.svg)](https://codeclimate.com/github/tsironis/lockr)
[![Dependencies](https://david-dm.org/tsironis/lockr.svg?theme=shields.io)](https://david-dm.org/tsironis/lockr)
[![devDependency Status](https://david-dm.org/tsironis/lockr/dev-status.svg)](https://david-dm.org/tsironis/lockr#info=devDependencies)

Lockr (pronounced /ˈlɒkəʳ/) is an extremely lightweight library (<2kb when minified), designed to facilitate how you interact with localStorage. Saving objects and arrays, numbers or other data types, accessible via a Redis-like API, heavily inspired by [node_redis](https://github.com/mranney/node_redis/).

## How to use lockr
Expand All @@ -15,7 +18,7 @@ In order to user lockr, you firstly need to install it in your project.
bower install lockr
```

or you use npm to install
or you use npm to install

```js
npm i lockr --save
Expand Down Expand Up @@ -152,6 +155,17 @@ Lockr.smembers("wat"); // [2]
Lockr.getAll();
> ["Coyote", 12345, [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]]
```

```Lockr.getAll``` - arguments: *[includeKeys] {Boolean}*

> Returns contents of `localStorage` as an Array of dictionaries that contain key and value of the saved item.

*Example*

```js
Lockr.getAll(true);
> [{"username": "Coyote"}, {"user_id": 12345}, {"users": [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]}]
```
---

```Lockr.flush()``` - arguments: *null*
Expand Down
12 changes: 11 additions & 1 deletion lockr.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,18 @@
return keys;
};

Lockr.getAll = function () {
Lockr.getAll = function (includeKeys) {
var keys = Lockr.keys();

if (includeKeys) {
return keys.reduce(function (accum, key) {
var tempObj = {};
tempObj[key] = Lockr.get(key);
accum.push(tempObj);
return accum;
}, []);
}

return keys.map(function (key) {
return Lockr.get(key);
});
Expand Down
2 changes: 1 addition & 1 deletion lockr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"LICENSE"
],
"main": "lockr.js",
"scripts": {
"build": "grunt"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.11.0",
"grunt-contrib-jasmine": "~1.0.0",
Expand Down
9 changes: 9 additions & 0 deletions specs/lockrSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ describe('Lockr.get', function () {
expect(contents).toContain([2, 3]);
});

it('gets all contents of the localStorage as Array of dictionaries (key/value)', function () {
var contents = Lockr.getAll(true);

expect(contents.length).toBe(6);
expect(contents).toContain({"hash": {"test": 123, "hey": "whatsup"}});
expect(contents).toContain({"test": 123});
expect(contents).toContain({"array": [2, 3]});
});

describe('with pre-existing data', function() {
beforeEach(function() {
localStorage.setItem('wrong', ',fluffy,truffly,commas,hell');
Expand Down