Skip to content

Commit cc49b11

Browse files
committed
Aligned DataSnapshot methods to @firebase/database counterpart
1 parent 35ff54f commit cc49b11

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/v1/providers/database.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,16 @@ export class DataSnapshot {
471471
* @return `true` if this `DataSnapshot` contains any data; otherwise, `false`.
472472
*/
473473
exists(): boolean {
474-
return !_.isNull(this.val());
474+
const val = this.val();
475+
if (_.isNull(val)) {
476+
// Null value
477+
return false;
478+
}
479+
if ((_.isObjectLike(val) || _.isArray(val)) && _.isEmpty(val)) {
480+
// Empty object/array
481+
return false;
482+
}
483+
return true;
475484
}
476485

477486
/**
@@ -512,7 +521,7 @@ export class DataSnapshot {
512521
*/
513522
forEach(action: (a: DataSnapshot) => boolean | void): boolean {
514523
const val = this.val();
515-
if (_.isPlainObject(val)) {
524+
if (_.isObjectLike(val) || _.isArray(val)) {
516525
return _.some(
517526
val,
518527
(value, key: string) => action(this.child(key)) === true
@@ -546,7 +555,7 @@ export class DataSnapshot {
546555
*/
547556
hasChildren(): boolean {
548557
const val = this.val();
549-
return _.isPlainObject(val) && _.keys(val).length > 0;
558+
return (_.isObjectLike(val) || _.isArray(val)) && !_.isEmpty(val);
550559
}
551560

552561
/**
@@ -556,7 +565,7 @@ export class DataSnapshot {
556565
*/
557566
numChildren(): number {
558567
const val = this.val();
559-
return _.isPlainObject(val) ? Object.keys(val).length : 0;
568+
return _.isObjectLike(val) || _.isArray(val) ? _.keys(val).length : 0;
560569
}
561570

562571
/**
@@ -588,7 +597,12 @@ export class DataSnapshot {
588597
continue;
589598
}
590599
const childNode = node[key];
591-
obj[key] = this._checkAndConvertToArray(childNode);
600+
const v = this._checkAndConvertToArray(childNode);
601+
if (v === null) {
602+
// Empty child node
603+
continue;
604+
}
605+
obj[key] = v;
592606
numKeys++;
593607
const integerRegExp = /^(0|[1-9]\d*)$/;
594608
if (allIntegerKeys && integerRegExp.test(key)) {
@@ -598,6 +612,11 @@ export class DataSnapshot {
598612
}
599613
}
600614

615+
if (numKeys === 0) {
616+
// Empty node
617+
return null;
618+
}
619+
601620
if (allIntegerKeys && maxKey < 2 * numKeys) {
602621
// convert to array.
603622
const array: any = [];

0 commit comments

Comments
 (0)