Skip to content

Commit 302ebaa

Browse files
authored
Correct the argument types of the forEach function
2 parents 424daec + 640466b commit 302ebaa

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

spec/providers/database.spec.ts

Lines changed: 37 additions & 3 deletions
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 = '';
@@ -223,12 +223,46 @@ 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
});
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 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', () => {
258+
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
259+
let out = '';
260+
const ret = subject.forEach(snap => {
261+
out += snap.val();
262+
});
263+
expect(out).to.equal('bdfh');
264+
expect(ret).to.equal(false);
265+
});
232266
});
233267

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

src/providers/database.ts

Lines changed: 1 addition & 2 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?
210209
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 _.some(val, (value, key: string) => action(this.child(key)) === true);
214213
}
215214
return false;
216215
}

0 commit comments

Comments
 (0)