Skip to content

Commit

Permalink
Merge pull request #28 from stropitek/master
Browse files Browse the repository at this point in the history
Simpler and safer handling of non-valid jsons
  • Loading branch information
tsironis committed Feb 25, 2016
2 parents 0e21574 + a1f6662 commit 791a95b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 1 addition & 5 deletions lockr.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@
try {
value = JSON.parse(localStorage.getItem(query_key));
} catch (e) {
try {
if(localStorage[query_key]) {
value = JSON.parse('{"data":"' + localStorage.getItem(query_key) + '"}');
value = {data: localStorage.getItem(query_key)};
} else{
value = null;
}
} catch (e) {
if (console) console.warn("Lockr could not load the item with key " + key);
}
}
if(value === null) {
return missing;
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.

11 changes: 8 additions & 3 deletions specs/lockrSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ describe('Lockr.get', function () {
expect(contents).toContain([2, 3]);
});

describe('with wrong data', function() {
describe('with pre-existing data', function() {
beforeEach(function() {
localStorage.setItem('wrong', ',fluffy,truffly,commas,hell');
localStorage.setItem('unescaped', 'a " double-quote');
});

it('cleans wrong data', function () {
it("if it's not a json we get as-is", function () {
var wrongData = Lockr.get("wrong");

expect(wrongData).toBe(',fluffy,truffly,commas,hell');
});

it('works with unescaped characters', function () {
var unescaped = Lockr.get('unescaped');
expect(unescaped).toBe('a " double-quote');
});
});
});

Expand Down

0 comments on commit 791a95b

Please sign in to comment.