All files / src/utils match.js

90.91% Statements 10/11
91.67% Branches 11/12
100% Functions 1/1
90.91% Lines 10/11
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                                    1606x     1606x 1848x 421x   1427x 583x 60x   844x 12x     1113x    
/*
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @flow
 *
 */
 
/**
 * This function takes an AST node and matches it against "pattern". Pattern
 * is simply a (nested) object literal and it is traversed to see whether node
 * contains those (nested) properties with the provided values.
 */
export default function match(node: ASTNode, pattern: Object): boolean {
  Iif (!node) {
    return false;
  }
  for (var prop in pattern) {
    if (!node[prop]) {
      return false;
    }
    if (pattern[prop] && typeof pattern[prop] === 'object') {
      if (!match(node[prop], pattern[prop])) {
        return false;
      }
    } else if (node[prop] !== pattern[prop]) {
      return false;
    }
  }
  return true;
}