Skip to content

Commit

Permalink
Implement keys function, issue #31
Browse files Browse the repository at this point in the history
  • Loading branch information
tsironis committed Feb 29, 2016
1 parent 626aa8c commit ceee274
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lockr.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@
return Lockr.smembers(key).indexOf(value) > -1;
};

Lockr.getAll = function () {
Lockr.keys = function() {
var keys = Object.keys(localStorage);
keys = keys.filter(function (key) {
return key.indexOf(Lockr.prefix) !== -1

return keys.filter(function (key) {
return key.indexOf(Lockr.prefix) !== -1;
});
};

Lockr.getAll = function () {
var keys = Lockr.keys();
return keys.map(function (key) {
return Lockr.get(key.substr(Lockr.prefix.length));
});
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.

27 changes: 27 additions & 0 deletions specs/lockrSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ describe('Lockr.get', function () {
expect(value).toEqual(0);
});

it('gets Lockr keys', function() {
Lockr.flush();
Lockr.set('one', 1);
Lockr.set('two', 2);
Lockr.set('three', 3);
Lockr.set('four', 4);

var keys = Lockr.keys();

expect(keys.length).toBe(4);
expect(keys).toContain('one', 'two', 'three', 'four');
});

it('gets all contents of the localStorage', function () {
var contents = Lockr.getAll();

Expand Down Expand Up @@ -187,4 +200,18 @@ describe('Lockr::Prefixed', function() {
Lockr.set("justlikeyou", true);
expect("imaprefixjustlikeyou" in localStorage).toEqual(true);
});
it('gets Lockr keys (with prefix)', function() {
Lockr.flush();

Lockr.set('one', 1);
Lockr.set('two', 2);
Lockr.set('three', 3);
Lockr.set('four', 4);

var keys = Lockr.keys();

expect(keys.length).toBe(4);
expect(keys).toContain('imaprefixone', 'imaprefixtwo', 'imaprefixthree', 'imaprefixfour');

This comment has been minimized.

Copy link
@avgerin0s

avgerin0s Feb 29, 2016

Collaborator

Maybe it should strip prefix to make the keys accessible from Lockr

This comment has been minimized.

Copy link
@tsironis

tsironis Mar 1, 2016

Author Owner

@eavgerinos see latest commit.

});

});

0 comments on commit ceee274

Please sign in to comment.