2
0
mirror of synced 2025-02-20 01:58:09 +00:00

js: snapshot comments / flow type additions

This commit is contained in:
Salakar 2017-03-09 18:18:10 +00:00
parent 834980f9dd
commit 178b2e1d99

View File

@ -4,6 +4,9 @@
import Reference from './reference.js'; import Reference from './reference.js';
import { isObject, deepGet, deepExists } from './../../utils'; import { isObject, deepGet, deepExists } from './../../utils';
/**
* @link https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot
*/
export default class Snapshot { export default class Snapshot {
static key: String; static key: String;
static value: Object; static value: Object;
@ -52,6 +55,7 @@ export default class Snapshot {
value, value,
key: childRef.key, key: childRef.key,
exists: value !== null, exists: value !== null,
// todo this is wrong - child keys needs to be the ordered keys, from FB // 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 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. // todo natively and send that back to JS to be use in this class.
@ -91,33 +95,50 @@ export default class Snapshot {
return cancelled; 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; 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); 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; 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; if (!isObject(this.value)) return 0;
return Object.keys(this.value).length; 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) { toJSON(): any {
const arr = []; return this.val();
this.forEach((item, i) => arr.push(fn(item, i)));
return arr;
}
reverseMap(fn: (key: string) => mixed) {
return this.map(fn).reverse();
} }
} }