js: snapshot comments / flow type additions

This commit is contained in:
Salakar 2017-03-09 18:18:10 +00:00
parent 834980f9dd
commit 178b2e1d99
1 changed files with 35 additions and 14 deletions

View File

@ -4,6 +4,9 @@
import Reference from './reference.js';
import { isObject, deepGet, deepExists } from './../../utils';
/**
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot
*/
export default class Snapshot {
static key: String;
static value: Object;
@ -52,6 +55,7 @@ export default class Snapshot {
value,
key: childRef.key,
exists: value !== null,
// todo this is wrong - child keys needs to be the ordered keys, from FB
// todo potential solution is build up a tree/map of a snapshot and its children
// todo natively and send that back to JS to be use in this class.
@ -91,33 +95,50 @@ export default class Snapshot {
return cancelled;
}
getPriority() {
/**
* Gets the priority value of the data in this DataSnapshot.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#getPriority
* @returns {String|Number|null}
*/
getPriority(): String|Number|null {
return this.priority;
}
hasChild(path: string) {
/**
* Returns true if the specified child path has (non-null) data.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#hasChild
* @param path
* @returns {Boolean}
*/
hasChild(path: string): Boolean {
return deepExists(this.value, path);
}
hasChildren() {
/**
* Returns whether or not the DataSnapshot has any non-null child properties.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#hasChildren
* @returns {boolean}
*/
hasChildren(): Boolean {
return this.numChildren() > 0;
}
numChildren() {
/**
* Returns the number of child properties of this DataSnapshot.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren
* @returns {Number}
*/
numChildren(): Number {
if (!isObject(this.value)) return 0;
return Object.keys(this.value).length;
}
/*
* EXTRA API METHODS
/**
* Returns a JSON-serializable representation of this object.
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#toJSON
* @returns {any}
*/
map(fn: (key: string) => mixed) {
const arr = [];
this.forEach((item, i) => arr.push(fn(item, i)));
return arr;
}
reverseMap(fn: (key: string) => mixed) {
return this.map(fn).reverse();
toJSON(): any {
return this.val();
}
}