Skip to content

Commit

Permalink
feat: Optionally reject on null lookup
Browse files Browse the repository at this point in the history
This is a backwards-compatible way of allowing rejection if the value of
the getItem lookup is null. If you actually want to store and retrieve
null as a value, just use the single arity version of `getItem`.

Fixes #111
  • Loading branch information
Scott Trinh authored and Scott Trinh committed Jun 3, 2016
1 parent b31c1a8 commit efd0e66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
};

// Directly get a value from storage
LocalForageInstance.prototype.getItem = function getItem(key) {
LocalForageInstance.prototype.getItem = function getItem(key, rejectOnNull) {
// throw error on undefined key
if(angular.isUndefined(key)) {
throw new Error("You must define a key to get");
Expand All @@ -162,16 +162,26 @@
return res;
}
}).then(function() {
var shouldResolve = true;
for (var i = 0; i < key.length; i++) {
if (angular.isUndefined(res[i])) {
res[i] = null;
shouldResolve = false;
}
}
deferred.resolve(res);
if (shouldResolve || !rejectOnNull) {
deferred.resolve(res);
} else {
deferred.reject(res);
}
});
} else {
promise = self._localforage.getItem(self.prefix() + key).then(function(item) {
deferred.resolve(item);
if (rejectOnNull && item === null) {
deferred.reject(item);
} else {
deferred.resolve(item);
}
});
}

Expand Down
31 changes: 28 additions & 3 deletions tests/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ describe('Module: LocalForageModule', function() {
}, done);
});

it('getItem produces a rejected promise for unknown key if second parameter is true', function(done) {
var interval = triggerDigests();

$localForage.getItem('this key is unknown', true)
.catch(function(err) {
stopDigests(interval);
expect(err).toBe(null);
done();
});
})

describe('getItem for an array with unknown keys', function() {
it('should produce null values with all unknown keys', function(done) {
var interval = triggerDigests();
Expand Down Expand Up @@ -175,16 +186,30 @@ describe('Module: LocalForageModule', function() {
$localForage.setItem(['myName', 'myPassion', 'myHobbie'], values).then(function(data) {
expect(data).toEqual(values);

$localForage.getItem(['myHobbie', 'myName']).then(function(data) {
$localForage.getItem(['myHobbie', 'myName', 'notInDatabase']).then(function(data) {
stopDigests(interval);
expect(data.length).toEqual(2);
expect(data).toEqual(['Open Source', 'Olivier Combe']);
expect(data.length).toEqual(3);
expect(data).toEqual(['Open Source', 'Olivier Combe', null]);
done();
}, done);

}, done);
});

it('getItem should reject if one of the values is null if true is passed as the second argument', function(done) {
var interval = triggerDigests();

$localForage.setItem(['myName', 'myPassion'], ['Scott Trinh', 'Anarchy']).then(function(data) {
$localForage.getItem(['myPassion', 'myName', 'notInDatabase'], true).catch(function(data) {
stopDigests(interval);
expect(data.length).toEqual(3);
expect(data).toEqual(['Anarchy', 'Scott Trinh', null]);
done();
});
});

})

it('iterate should be defined', function() {
expect($localForage.iterate).toBeDefined();
expect(typeof $localForage.iterate).toBe('function');
Expand Down

0 comments on commit efd0e66

Please sign in to comment.