Skip to content

Commit

Permalink
Provide option for getAll method so that it includes keys, enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
georapbox committed Dec 30, 2016
1 parent fa42063 commit 49e0bba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 +152,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.

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

0 comments on commit 49e0bba

Please sign in to comment.