All files / core/snap nodeFromJSON.ts

100% Statements 50/50
91.3% Branches 42/46
100% Functions 4/4
100% Lines 50/50
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                                15x 15x 15x 15x 15x 15x 15x 15x 15x     15x                 15x   12616x   6545x 3025x     3520x 538x     3520x               3520x 420x       3520x 2860x 2860x     660x 656x 656x 656x 656x 1881x   1763x 1763x 1762x   1762x         656x 7x     649x     1762x     649x 119x       119x                 530x             4x 4x 4x 13x 13x   13x 13x 13x         4x       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 { ChildrenNode } from './ChildrenNode';
import { LeafNode } from './LeafNode';
import { NamedNode, Node } from './Node';
import { forEach, contains } from '@firebase/util';
import { assert } from '@firebase/util';
import { buildChildSet } from './childSet';
import { NAME_COMPARATOR, NAME_ONLY_COMPARATOR } from './comparators';
import { IndexMap } from './IndexMap';
import { PRIORITY_INDEX, setNodeFromJSON } from './indexes/PriorityIndex';
import { SortedMap } from '../util/SortedMap';
 
const USE_HINZE = true;
 
/**
 * Constructs a snapshot node representing the passed JSON and returns it.
 * @param {*} json JSON to create a node for.
 * @param {?string|?number=} priority Optional priority to use.  This will be ignored if the
 * passed JSON contains a .priority property.
 * @return {!Node}
 */
export function nodeFromJSON(
  json: any | null,
  priority: string | number | null = null
): Node {
  if (json === null) {
    return ChildrenNode.EMPTY_NODE;
  }
 
  if (typeof json === 'object' && '.priority' in json) {
    priority = json['.priority'];
  }
 
  assert(
    priority === null ||
      typeof priority === 'string' ||
      typeof priority === 'number' ||
      (typeof priority === 'object' && '.sv' in (priority as object)),
    'Invalid priority type found: ' + typeof priority
  );
 
  if (typeof json === 'object' && '.value' in json && json['.value'] !== null) {
    json = json['.value'];
  }
 
  // Valid leaf nodes include non-objects or server-value wrapper objects
  if (typeof json !== 'object' || '.sv' in json) {
    const jsonLeaf = json as string | number | boolean | object;
    return new LeafNode(jsonLeaf, nodeFromJSON(priority));
  }
 
  if (!(json instanceof Array) && USE_HINZE) {
    const children: NamedNode[] = [];
    let childrenHavePriority = false;
    const hinzeJsonObj: { [k: string]: any } = json as object;
    forEach(hinzeJsonObj, (key: string, child: any) => {
      if (typeof key !== 'string' || key.substring(0, 1) !== '.') {
        // Ignore metadata nodes
        const childNode = nodeFromJSON(hinzeJsonObj[key]);
        if (!childNode.isEmpty()) {
          childrenHavePriority =
            childrenHavePriority || !childNode.getPriority().isEmpty();
          children.push(new NamedNode(key, childNode));
        }
      }
    });
 
    if (children.length == 0) {
      return ChildrenNode.EMPTY_NODE;
    }
 
    const childSet = buildChildSet(
      children,
      NAME_ONLY_COMPARATOR,
      namedNode => namedNode.name,
      NAME_COMPARATOR
    ) as SortedMap<string, Node>;
    if (childrenHavePriority) {
      const sortedChildSet = buildChildSet(
        children,
        PRIORITY_INDEX.getCompare()
      );
      return new ChildrenNode(
        childSet,
        nodeFromJSON(priority),
        new IndexMap(
          { '.priority': sortedChildSet },
          { '.priority': PRIORITY_INDEX }
        )
      );
    } else {
      return new ChildrenNode(
        childSet,
        nodeFromJSON(priority),
        IndexMap.Default
      );
    }
  } else {
    let node: Node = ChildrenNode.EMPTY_NODE;
    const jsonObj = json as object;
    forEach(jsonObj, (key: string, childData: any) => {
      Eif (contains(jsonObj, key)) {
        Eif (key.substring(0, 1) !== '.') {
          // ignore metadata nodes.
          const childNode = nodeFromJSON(childData);
          Eif (childNode.isLeafNode() || !childNode.isEmpty())
            node = node.updateImmediateChild(key, childNode);
        }
      }
    });
 
    return node.updatePriority(nodeFromJSON(priority));
  }
}
 
setNodeFromJSON(nodeFromJSON);