var through = require('through2');
var formatAddress = require('../lib/formatAddress');


module.exports = function(addr, city, state, zip){
  if(!addr) throw new Error('Must provide address at minimum.');
  var prefix = '';
  var suffix = '';

  //Chunk is a GeoJSON feature
  function transform(chunk, enc, cb){
    chunk = JSON.parse(chunk);
    var props = chunk.properties;
    var payload; 

    try{ 
      payload = {
        type: "Feature",
        properties: {
          address: formatAddress(
                       props[addr],
                       props[city],
                       props[state],
                       props[zip]
                       ),
          alt_address: "",
          load_date: Date.now()
        },
        geometry: {
          type: "Point",
          coordinates: chunk.geometry.coordinates
        }
      };
    }catch(e){
      //possibly log the error
      return cb();
    }
    //Elaticsearch bulk wants newline separated values
    this.push(prefix + JSON.stringify(payload) + suffix);
    cb();
  }

  return function(pre, suf){
    if(pre) prefix = pre;
    if(suf) suffix = suf;
    return through(transform);
  }
};

@@ -199,16 +199,55 @@
 mum.');%0A
+  var args = %5Baddr, city, state, zip%5D;%0A
   var pr
@@ -440,16 +440,185 @@
   try%7B %0A
+      %0A      var vals = args.map(function(arg)%7B%0A        console.log(arg);%0A        if(typeof arg === 'function') return arg(props);%0A        return props%5Barg%5D;%0A      %7D);%0A%0A
       pa
@@ -735,18 +735,14 @@
     
-props%5Baddr
+vals%5B0
 %5D,%0A 
@@ -767,18 +767,14 @@
     
-props%5Bcity
+vals%5B1
 %5D,%0A 
@@ -799,19 +799,14 @@
     
-props%5Bstate
+vals%5B2
 %5D,%0A 
@@ -831,17 +831,14 @@
     
-props%5Bzip
+vals%5B3
 %5D%0A  
@@ -1045,16 +1045,17 @@
      %7D;%0A
+%0A
     %7Dcat

Allow functions in transformers