All files / api DataSnapshot.ts

95.08% Statements 58/61
75% Branches 3/4
93.75% Functions 15/16
96.49% Lines 55/57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                12x 12x 12x 12x                   12x             976x 976x 976x                 12x 428x 428x               12x 15x 15x         12x   2x 2x               12x 6x 6x                 12x 21x   21x 21x   21x 21x 21x                         12x 5x 5x   5x 5x               12x 12x     12x                     31x 31x 31x   31x   31x   31x 235x                   12x 3x   3x 2x     12x 278x             12x                 12x 643x   643x     12x 17x   12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { validateArgCount, validateCallback } from '@firebase/util';
import { validatePathString } from '../core/util/validation';
import { Path } from '../core/util/Path';
import { PRIORITY_INDEX } from '../core/snap/indexes/PriorityIndex';
import { Node } from '../core/snap/Node';
import { Reference } from './Reference';
import { Index } from '../core/snap/indexes/Index';
import { ChildrenNode } from '../core/snap/ChildrenNode';
 
/**
 * Class representing a firebase data snapshot.  It wraps a SnapshotNode and
 * surfaces the public methods (val, forEach, etc.) we want to expose.
 */
export class DataSnapshot {
  /**
   * @param {!Node} node_ A SnapshotNode to wrap.
   * @param {!Reference} ref_ The ref of the location this snapshot came from.
   * @param {!Index} index_ The iteration order for this snapshot
   */
  constructor(
    private readonly node_: Node,
    private readonly ref_: Reference,
    private readonly index_: Index
  ) {}
 
  /**
   * Retrieves the snapshot contents as JSON.  Returns null if the snapshot is
   * empty.
   *
   * @return {*} JSON representation of the DataSnapshot contents, or null if empty.
   */
  val(): any {
    validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
    return this.node_.val();
  }
 
  /**
   * Returns the snapshot contents as JSON, including priorities of node.  Suitable for exporting
   * the entire node contents.
   * @return {*} JSON representation of the DataSnapshot contents, or null if empty.
   */
  exportVal(): any {
    validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
    return this.node_.val(true);
  }
 
  // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  // for end-users
  toJSON(): any {
    // Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
    validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
    return this.exportVal();
  }
 
  /**
   * Returns whether the snapshot contains a non-null value.
   *
   * @return {boolean} Whether the snapshot contains a non-null value, or is empty.
   */
  exists(): boolean {
    validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
    return !this.node_.isEmpty();
  }
 
  /**
   * Returns a DataSnapshot of the specified child node's contents.
   *
   * @param {!string} childPathString Path to a child.
   * @return {!DataSnapshot} DataSnapshot for child node.
   */
  child(childPathString: string): DataSnapshot {
    validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
    // Ensure the childPath is a string (can be a number)
    childPathString = String(childPathString);
    validatePathString('DataSnapshot.child', 1, childPathString, false);
 
    const childPath = new Path(childPathString);
    const childRef = this.ref_.child(childPath);
    return new DataSnapshot(
      this.node_.getChild(childPath),
      childRef,
      PRIORITY_INDEX
    );
  }
 
  /**
   * Returns whether the snapshot contains a child at the specified path.
   *
   * @param {!string} childPathString Path to a child.
   * @return {boolean} Whether the child exists.
   */
  hasChild(childPathString: string): boolean {
    validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
    validatePathString('DataSnapshot.hasChild', 1, childPathString, false);
 
    const childPath = new Path(childPathString);
    return !this.node_.getChild(childPath).isEmpty();
  }
 
  /**
   * Returns the priority of the object, or null if no priority was set.
   *
   * @return {string|number|null} The priority.
   */
  getPriority(): string | number | null {
    validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
 
    // typecast here because we never return deferred values or internal priorities (MAX_PRIORITY)
    return this.node_.getPriority().val() as string | number | null;
  }
 
  /**
   * Iterates through child nodes and calls the specified action for each one.
   *
   * @param {function(!DataSnapshot)} action Callback function to be called
   * for each child.
   * @return {boolean} True if forEach was canceled by action returning true for
   * one of the child nodes.
   */
  forEach(action: (d: DataSnapshot) => void): boolean {
    validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
    validateCallback('DataSnapshot.forEach', 1, action, false);
 
    Iif (this.node_.isLeafNode()) return false;
 
    const childrenNode = this.node_ as ChildrenNode;
    // Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
    return !!childrenNode.forEachChild(this.index_, (key, node) => {
      return action(
        new DataSnapshot(node, this.ref_.child(key), PRIORITY_INDEX)
      );
    });
  }
 
  /**
   * Returns whether this DataSnapshot has children.
   * @return {boolean} True if the DataSnapshot contains 1 or more child nodes.
   */
  hasChildren(): boolean {
    validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
 
    if (this.node_.isLeafNode()) return false;
    else return !this.node_.isEmpty();
  }
 
  get key() {
    return this.ref_.getKey();
  }
 
  /**
   * Returns the number of children for this DataSnapshot.
   * @return {number} The number of children that this DataSnapshot contains.
   */
  numChildren(): number {
    validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
 
    return this.node_.numChildren();
  }
 
  /**
   * @return {Reference} The Firebase reference for the location this snapshot's data came from.
   */
  getRef(): Reference {
    validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
 
    return this.ref_;
  }
 
  get ref() {
    return this.getRef();
  }
}