Skip to content

Commit b2b0b41

Browse files
committed
Correct the return value of the forEach function
1 parent f3afac4 commit b2b0b41

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

spec/providers/database.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('DeltaSnapshot', () => {
208208
});
209209
});
210210

211-
describe('#forEach(childAction: Function)', () => {
211+
describe('#forEach(action: (a: DeltaSnapshot) => boolean): boolean', () => {
212212
it('should iterate through child snapshots', () => {
213213
populate({ a: 'b' }, { c: 'd' });
214214
let out = '';
@@ -229,6 +229,32 @@ describe('DeltaSnapshot', () => {
229229
subject.forEach(counter);
230230
expect(count).to.eq(0);
231231
});
232+
233+
it('should cancel further enumeration if callback returns true', () => {
234+
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
235+
let out = '';
236+
const ret = subject.forEach(snap => {
237+
if (snap.val() === 'f') {
238+
return true;
239+
}
240+
out += snap.val();
241+
});
242+
expect(out).to.equal('bd');
243+
expect(ret).to.equal(true);
244+
});
245+
246+
it('should not cancel further enumeration if callback does not return true', () => {
247+
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
248+
let out = '';
249+
const ret = subject.forEach(snap => {
250+
if (snap.val() === 'a') {
251+
return true;
252+
}
253+
out += snap.val();
254+
});
255+
expect(out).to.equal('bdfh');
256+
expect(ret).to.equal(false);
257+
});
232258
});
233259

234260
describe('#numChildren()', () => {

src/providers/database.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,10 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
206206
return valAt(this._delta, this._childPath) !== undefined;
207207
}
208208

209-
// TODO(inlined) what is this boolean for?
210-
forEach(action: (a: DeltaSnapshot) => void): boolean {
209+
forEach(action: (a: DeltaSnapshot) => boolean): boolean {
211210
let val = this.val();
212211
if (_.isPlainObject(val)) {
213-
_.keys(val).forEach(key => action(this.child(key)));
212+
return _.keys(val).some(key => action(this.child(key)));
214213
}
215214
return false;
216215
}

0 commit comments

Comments
 (0)