
function searchAnnotation(collection, elementName) {
  var ret = [];
  if(elementName === undefined) {
    for(var name in collection) {
      ret.push(collection[name]);
    }
  }
  else {
    if(collection[elementName] !== undefined) {
      ret.push(collection[elementName]);
    }
  }
  return ret;
}

function getElementIfNameEqual(element, elementName) {
  if(elementName === undefined) {
    return element;
  }
  if(elementName === element.Name) {
    return element;
  }
  return undefined;
}

function searchCSDL(root, elementType, elementName, includeReferences) {
  var ret = [];
  for(var key in root) {
    if(key[0] === '_') {
      continue;
    }
    else if(key === 'Annotations') {
      if(elementType === 'Annotation') {
        ret = ret.concat(searchAnnotation(root.Annotations, elementName));
      }
      continue;
    }
    else if(key === 'References' && includeReferences !== true) {
      continue;
    }
    var element = root[key];
    if(element === undefined || element === null) {
      continue;
    }
    if(typeof element !== 'object') {
      continue;
    }
    if(elementType !== undefined) {
      if(elementType === element.constructor.name) {
        var tmp = getElementIfNameEqual(element, elementName);
        if(tmp !== undefined) {
          ret.push(tmp);
        }
      }
      else {
        var tmp = searchCSDL(element, elementType, elementName);
        if(tmp.length > 0) {
          ret = ret.concat(tmp);
        }
      }
    }
    else {
      var tmp = getElementIfNameEqual(element, elementName);
      if(tmp !== undefined) {
        ret.push(tmp);
      }
    }
  }
  return ret;
}

function remapNamespace(references, namespace) {
  for(var i = 0; i < references.length; i++) {
    if(references[i].Includes[namespace] !== undefined) {
      return references[i].Includes[namespace];
    }
  }
  return undefined;
}

function findByType(metadata, typeName) {
  if(typeName.startsWith('Edm')) {
    return typeName;
  }
  var index = typeName.lastIndexOf('.');
  if(index === -1) {
    return null;
  }
  var namespace = typeName.substring(0, index);
  var justType = typeName.substring(index+1);
  var schema = metadata[namespace];
  if(schema === undefined) { 
    schema = metadata._options.cache.getSchema(namespace);
    if(schema === undefined) {
      namespace = remapNamespace(metadata.References, namespace);
      if(namespace !== undefined) {
        schema = metadata._options.cache.getSchema(namespace);
      }
      else {
        return null;
      }
    }
  }
  return schema[justType];
}

module.exports.search = searchCSDL;
module.exports.findByType = findByType;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

@@ -132,24 +132,81 @@
 lection) %7B%0D%0A
+      if(name%5B0%5D === '_') %7B%0D%0A        continue;%0D%0A      %7D%0D%0A
       ret.pu
@@ -572,32 +572,273 @@
 ndefined;%0D%0A%7D%0D%0A%0D%0A
+function pushIfNotUndefined(array, element) %7B%0D%0A  if(element !== undefined) %7B%0D%0A    array.push(element);%0D%0A  %7D%0D%0A%7D%0D%0A%0D%0Afunction elementInvalid(element) %7B%0D%0A  return (element === undefined %7C%7C element === null %7C%7C typeof element !== 'object');%0D%0A%7D%0D%0A%0D%0A
 function searchC
@@ -1316,105 +1316,24 @@
 ment
- === undefined %7C%7C element === null) %7B%0D%0A      continue;%0D%0A    %7D%0D%0A    if(typeof element !== 'object'
+Invalid(element)
 ) %7B%0D
@@ -1452,33 +1452,47 @@
  %7B%0D%0A        
-var tmp =
+pushIfNotUndefined(ret,
  getElementI
@@ -1527,79 +1527,10 @@
 ame)
+)
 ;
-%0D%0A        if(tmp !== undefined) %7B%0D%0A          ret.push(tmp);%0D%0A        %7D
 %0D%0A  
@@ -1725,25 +1725,39 @@
 %0D%0A      
-var tmp =
+pushIfNotUndefined(ret,
  getElem
@@ -1796,73 +1796,10 @@
 ame)
+)
 ;
-%0D%0A      if(tmp !== undefined) %7B%0D%0A        ret.push(tmp);%0D%0A      %7D
 %0D%0A  

Fix Code Climate issues in CSDLSearch.js