diff --git a/lib/modules/database/snapshot.js b/lib/modules/database/snapshot.js index 9a3ffbc8..bc833a97 100644 --- a/lib/modules/database/snapshot.js +++ b/lib/modules/database/snapshot.js @@ -30,11 +30,22 @@ export default class Snapshot { * DEFAULT API METHODS */ - val() { + /** + * Extracts a JavaScript value from a DataSnapshot. + * @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#val + * @returns {any} + */ + val(): any { return this.value; } - child(path: string) { + /** + * Gets another DataSnapshot for the location at the specified relative path. + * @param path + * @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach + * @returns {Snapshot} + */ + child(path: string): Snapshot { const value = deepGet(this.value, path); const childRef = this.ref.child(path); return new Snapshot(childRef, { @@ -48,12 +59,36 @@ export default class Snapshot { }); } - exists() { + /** + * Returns true if this DataSnapshot contains any data. + * @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists + * @returns {boolean} + */ + exists(): Boolean { return this.value !== null; } - forEach(fn: (key: any) => any) { - return this.childKeys.forEach(key => fn(this.child(key))); + /** + * Enumerates the top-level children in the DataSnapshot. + * @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach + * @param action + */ + forEach(action: (key: any) => any): Boolean { + if (!this.childKeys.length) return false; + let cancelled = false; + + for (let i = 0, len = this.childKeys.length; i < len; i++) { + const key = this.childKeys[i]; + const childSnapshot = this.child(key); + const returnValue = action(childSnapshot); + + if (returnValue === true) { + cancelled = true; + break; + } + } + + return cancelled; } getPriority() {