Skip to content

Commit

Permalink
Aligned DataSnapshot methods to @firebase/database counterpart
Browse files Browse the repository at this point in the history
  • Loading branch information
rhodgkins committed Jul 31, 2021
1 parent 8a8481e commit 9ec3562
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/v1/providers/database.ts
Expand Up @@ -108,7 +108,7 @@ export function _instanceWithOptions(
*/
export class InstanceBuilder {
/** @hidden */
constructor(private instance: string, private options: DeploymentOptions) {}
constructor(private instance: string, private options: DeploymentOptions) { }

/**
* @return Firebase Realtime Database reference builder interface.
Expand All @@ -134,9 +134,9 @@ export function _refWithOptions(
if (!databaseURL) {
throw new Error(
'Missing expected firebase config value databaseURL, ' +
'config is actually' +
JSON.stringify(firebaseConfig()) +
'\n If you are unit testing, please set process.env.FIREBASE_CONFIG'
'config is actually' +
JSON.stringify(firebaseConfig()) +
'\n If you are unit testing, please set process.env.FIREBASE_CONFIG'
);
}

Expand Down Expand Up @@ -174,7 +174,7 @@ export class RefBuilder {
private apps: apps.Apps,
private triggerResource: () => string,
private options: DeploymentOptions
) {}
) { }

/**
* Event handler that fires every time a Firebase Realtime Database write
Expand Down Expand Up @@ -325,7 +325,7 @@ export function extractInstanceAndPath(
if (!match) {
throw new Error(
`Unexpected resource string for Firebase Realtime Database event: ${resource}. ` +
'Expected string in the format of "projects/_/instances/{firebaseioSubdomain}/refs/{ref=**}"'
'Expected string in the format of "projects/_/instances/{firebaseioSubdomain}/refs/{ref=**}"'
);
}
const [, project, dbInstanceName, path] = match;
Expand Down Expand Up @@ -396,7 +396,7 @@ export class DataSnapshot {
// may be unpopulated in user's unit tests
throw new Error(
'Please supply a Firebase app in the constructor for DataSnapshot' +
' in order to use the .ref method.'
' in order to use the .ref method.'
);
}
if (!this._ref) {
Expand Down Expand Up @@ -471,7 +471,16 @@ export class DataSnapshot {
* @return `true` if this `DataSnapshot` contains any data; otherwise, `false`.
*/
exists(): boolean {
return !_.isNull(this.val());
const val = this.val();
if (_.isNull(val)) {
// Null value
return false;
}
if ((_.isObjectLike(val) || _.isArray(val)) && _.isEmpty(val)) {
// Empty object/array
return false;
}
return true
}

/**
Expand Down Expand Up @@ -512,7 +521,7 @@ export class DataSnapshot {
*/
forEach(action: (a: DataSnapshot) => boolean | void): boolean {
const val = this.val();
if (_.isPlainObject(val)) {
if (_.isObjectLike(val) || _.isArray(val)) {
return _.some(
val,
(value, key: string) => action(this.child(key)) === true
Expand Down Expand Up @@ -546,7 +555,7 @@ export class DataSnapshot {
*/
hasChildren(): boolean {
const val = this.val();
return _.isPlainObject(val) && _.keys(val).length > 0;
return (_.isObjectLike(val) || _.isArray(val)) && !_.isEmpty(val);
}

/**
Expand All @@ -556,7 +565,7 @@ export class DataSnapshot {
*/
numChildren(): number {
const val = this.val();
return _.isPlainObject(val) ? Object.keys(val).length : 0;
return (_.isObjectLike(val) || _.isArray(val)) ? _.keys(val).length : 0;
}

/**
Expand Down Expand Up @@ -588,7 +597,12 @@ export class DataSnapshot {
continue;
}
const childNode = node[key];
obj[key] = this._checkAndConvertToArray(childNode);
const v = this._checkAndConvertToArray(childNode);
if (v === null) {
// Empty child node
continue;
}
obj[key] = v;
numKeys++;
const integerRegExp = /^(0|[1-9]\d*)$/;
if (allIntegerKeys && integerRegExp.test(key)) {
Expand All @@ -598,6 +612,11 @@ export class DataSnapshot {
}
}

if (numKeys === 0) {
// Empty node
return null;
}

if (allIntegerKeys && maxKey < 2 * numKeys) {
// convert to array.
const array: any = [];
Expand Down

0 comments on commit 9ec3562

Please sign in to comment.