Skip to content

Commit a110af0

Browse files
committed
Cancel further enumeration only if callback returns explicit true
1 parent b2b0b41 commit a110af0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

spec/providers/database.spec.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ describe('DeltaSnapshot', () => {
223223
let count = 0;
224224
let counter = snap => count++;
225225

226-
subject.forEach(counter);
226+
expect(subject.forEach(counter)).to.equal(false);
227227
populate(23, null);
228228

229-
subject.forEach(counter);
229+
expect(subject.forEach(counter)).to.equal(false);
230230
expect(count).to.eq(0);
231231
});
232232

@@ -243,13 +243,21 @@ describe('DeltaSnapshot', () => {
243243
expect(ret).to.equal(true);
244244
});
245245

246-
it('should not cancel further enumeration if callback does not return true', () => {
246+
it('should not cancel further enumeration if callback returns a truthy value', () => {
247+
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
248+
let out = '';
249+
const ret = subject.forEach(snap => {
250+
out += snap.val();
251+
return 1;
252+
});
253+
expect(out).to.equal('bdfh');
254+
expect(ret).to.equal(false);
255+
});
256+
257+
it('should not cancel further enumeration if callback does not return', () => {
247258
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
248259
let out = '';
249260
const ret = subject.forEach(snap => {
250-
if (snap.val() === 'a') {
251-
return true;
252-
}
253261
out += snap.val();
254262
});
255263
expect(out).to.equal('bdfh');

src/providers/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
209209
forEach(action: (a: DeltaSnapshot) => boolean): boolean {
210210
let val = this.val();
211211
if (_.isPlainObject(val)) {
212-
return _.keys(val).some(key => action(this.child(key)));
212+
return _.keys(val).some(key => action(this.child(key)) === true);
213213
}
214214
return false;
215215
}

0 commit comments

Comments
 (0)