All files / app/lib/elasticsearch mapping.js

15.9% Statements 7/44
100% Branches 1/1
0% Functions 0/2
15.9% Lines 7/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            21x                                               21x 21x 21x 21x 21x 21x
function parse(obj,path,res) {
  path = path || [];
  res = res || {};

  if (obj.field)
    res[obj.field] = {path:path,type:obj.type};
  
  if(obj.properties) {
    Object.keys(obj.properties).forEach(key => {
      parse(obj.properties[key],path.concat(key),res);
    });
  }
  return res;
}
 
function populate(map,src) {
  const obj = {};

  function setValue(path,o,val) {
    o = o || {};
    if (path.length == 1) {
      o[path[0]] = val;
      return o;
    } else {
      o[path[0]] = o[path[0]] || {};
      return setValue(path.slice(1),o[path[0]],val);
    }
  }

  Object.keys(map).forEach(key => {
    let value = src[key];
    if (!isNaN(value) && (map[key].type == 'long' || map[key].type == 'float'))
      value = Number(value);
    if (value !== undefined)
      setValue(map[key].path,obj,value);
  });
  return obj;
}
 
 
module.exports = {
  parse : parse,
  populate : populate
};