All files / core/snap Node.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
100% Lines 5/5
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                                                                                                                                                                                                                                                                                                                              15x 7387x               15x 1260x   15x  
/**
 * 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 { Path } from '../util/Path';
import { Index } from './indexes/Index';
 
/**
 * Node is an interface defining the common functionality for nodes in
 * a DataSnapshot.
 *
 * @interface
 */
export interface Node {
  /**
   * Whether this node is a leaf node.
   * @return {boolean} Whether this is a leaf node.
   */
  isLeafNode(): boolean;
 
  /**
   * Gets the priority of the node.
   * @return {!Node} The priority of the node.
   */
  getPriority(): Node;
 
  /**
   * Returns a duplicate node with the new priority.
   * @param {!Node} newPriorityNode New priority to set for the node.
   * @return {!Node} Node with new priority.
   */
  updatePriority(newPriorityNode: Node): Node;
 
  /**
   * Returns the specified immediate child, or null if it doesn't exist.
   * @param {string} childName The name of the child to retrieve.
   * @return {!Node} The retrieved child, or an empty node.
   */
  getImmediateChild(childName: string): Node;
 
  /**
   * Returns a child by path, or null if it doesn't exist.
   * @param {!Path} path The path of the child to retrieve.
   * @return {!Node} The retrieved child or an empty node.
   */
  getChild(path: Path): Node;
 
  /**
   * Returns the name of the child immediately prior to the specified childNode, or null.
   * @param {!string} childName The name of the child to find the predecessor of.
   * @param {!Node} childNode The node to find the predecessor of.
   * @param {!Index} index The index to use to determine the predecessor
   * @return {?string} The name of the predecessor child, or null if childNode is the first child.
   */
  getPredecessorChildName(
    childName: String,
    childNode: Node,
    index: Index
  ): string | null;
 
  /**
   * Returns a duplicate node, with the specified immediate child updated.
   * Any value in the node will be removed.
   * @param {string} childName The name of the child to update.
   * @param {!Node} newChildNode The new child node
   * @return {!Node} The updated node.
   */
  updateImmediateChild(childName: string, newChildNode: Node): Node;
 
  /**
   * Returns a duplicate node, with the specified child updated.  Any value will
   * be removed.
   * @param {!Path} path The path of the child to update.
   * @param {!Node} newChildNode The new child node, which may be an empty node
   * @return {!Node} The updated node.
   */
  updateChild(path: Path, newChildNode: Node): Node;
 
  /**
   * True if the immediate child specified exists
   * @param {!string} childName
   * @return {boolean}
   */
  hasChild(childName: string): boolean;
 
  /**
   * @return {boolean} True if this node has no value or children.
   */
  isEmpty(): boolean;
 
  /**
   * @return {number} The number of children of this node.
   */
  numChildren(): number;
 
  /**
   * Calls action for each child.
   * @param {!Index} index
   * @param {function(string, !Node)} action Action to be called for
   * each child.  It's passed the child name and the child node.
   * @return {*} The first truthy value return by action, or the last falsey one
   */
  forEachChild(index: Index, action: (a: string, b: Node) => void): any;
 
  /**
   * @param {boolean=} exportFormat True for export format (also wire protocol format).
   * @return {*} Value of this node as JSON.
   */
  val(exportFormat?: boolean): Object;
 
  /**
   * @return {string} hash representing the node contents.
   */
  hash(): string;
 
  /**
   * @param {!Node} other Another node
   * @return {!number} -1 for less than, 0 for equal, 1 for greater than other
   */
  compareTo(other: Node): number;
 
  /**
   * @param {!Node} other
   * @return {boolean} Whether or not this snapshot equals other
   */
  equals(other: Node): boolean;
 
  /**
   * @param {!Index} indexDefinition
   * @return {!Node} This node, with the specified index now available
   */
  withIndex(indexDefinition: Index): Node;
 
  /**
   * @param {!Index} indexDefinition
   * @return {boolean}
   */
  isIndexed(indexDefinition: Index): boolean;
}
 
/**
 *
 * @param {!string} name
 * @param {!Node} node
 * @constructor
 * @struct
 */
export class NamedNode {
  constructor(public name: string, public node: Node) {}
 
  /**
   *
   * @param {!string} name
   * @param {!Node} node
   * @return {NamedNode}
   */
  static Wrap(name: string, node: Node) {
    return new NamedNode(name, node);
  }
}