var inflection = require('inflection');


var NUMERIC = /^-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)$/;
function getNumericValue(params, key) {
  var value = params[key];
  if (!NUMERIC.test(value))
    throw new Error(key + ': ' + value + ' is invalid number');
  return parseFloat(value);
}

var INTEGER = /^-?[0-9]+$/;
function getIntegerValue(params, key) {
  var value = params[key];
  if (!INTEGER.test(value))
    throw new Error(key + ': ' + value + ' is invalid integer');
  return parseInt(value);
}

function searchRequestBuilder(request) {
  var params = request.params;
  if (!params.tableName)
    throw new Error('no source is specified');

  var queryParams = request.query;

  var queries = {};
  var queryCondition = {};
  var scriptCondition = {};
  var queryName = params.tableName;
  try {
    queryName = inflection.tableize(queryName);
    queryName = inflection.camelize(queryName, true);
  } catch(error) {
    queryName = params.tableName.toLowerCase();
  }
  queries[queryName] = {
    source: params.tableName,
    output: {
      elements: [
        'startTime',
        'elapsedTime',
        'count',
        'attributes',
        'records'
      ],
    }
  };

  if ('attributes' in queryParams) {
    queries[queryName].output.attributes = queryParams.attributes.split(',');
  } else {
    queries[queryName].output.attributes = [];
  }
  if ('limit' in queryParams)
    queries[queryName].output.limit = getIntegerValue(queryParams, 'limit');

  if ('match_escalation_threshold' in queryParams)
    queryCondition.matchEscalationThreshold = getIntegerValue(queryParams, 'match_escalation_threshold');
  if ('match_to' in queryParams)
    queryCondition.matchTo = queryParams.match_to.split(',');
  if ('query' in queryParams)
    queryCondition.query = queryParams.query;
  if ('script' in queryParams)
    scriptCondition.script = queryParams.script;
  if ('query' in queryCondition || 'script' in scriptCondition) {
    if (!('query' in queryCondition)) {
      queries[queryName].condition = scriptCondition;
    } else if (!('script' in scriptCondition)) {
      queries[queryName].condition = queryCondition;
    } else {
      queries[queryName].condition = [
        '&&',
        queryCondition,
        scriptCondition
      ]
    }
  }

  if ('offset' in queryParams)
    queries[queryName].output.offset = getIntegerValue(queryParams, 'offset');
  if ('sort_by' in queryParams)
    queries[queryName].sortBy = queryParams.sort_by.split(',');

  var message = {};
  message.queries = queries;

  if ('timeout' in queryParams)
    message.timeout = getIntegerValue(queryParams, 'timeout');

  return message;
}
exports.search = searchRequestBuilder;

@@ -509,492 +509,56 @@
 ion 
-searchRequestBuilder(request) %7B%0A  var params = request.params;%0A  if (!params.tableName)%0A    throw new Error('no source is specified');%0A%0A  var queryParams = request.query;%0A%0A  var queries = %7B%7D;%0A  var queryCondition = %7B%7D;%0A  var scriptCondition = %7B%7D;%0A  var queryName = params.tableName;%0A  try %7B%0A    queryName = inflection.tableize(queryName);%0A    queryName = inflection.camelize(queryName, true);%0A  %7D catch(error
+buildQuery(tableName, queryParams
 ) %7B%0A  
- 
+var
  query
-Name = params.tableName.toLowerCase();%0A  %7D%0A  queries%5BqueryName%5D
  = %7B
@@ -574,15 +574,8 @@
 ce: 
-params.
 tabl
@@ -776,38 +776,25 @@
 ) %7B%0A    quer
-ies%5BqueryName%5D
+y
 .output.attr
@@ -852,38 +852,25 @@
 e %7B%0A    quer
-ies%5BqueryName%5D
+y
 .output.attr
@@ -920,38 +920,25 @@
 ms)%0A    quer
-ies%5BqueryName%5D
+y
 .output.limi
@@ -981,16 +981,71 @@
 mit');%0A%0A
+  var queryCondition = %7B%7D;%0A  var scriptCondition = %7B%7D;%0A
   if ('m
@@ -1556,38 +1556,25 @@
 %7B%0A      quer
-ies%5BqueryName%5D
+y
 .condition =
@@ -1646,38 +1646,25 @@
 %7B%0A      quer
-ies%5BqueryName%5D
+y
 .condition =
@@ -1703,30 +1703,17 @@
     quer
-ies%5BqueryName%5D
+y
 .conditi
@@ -1839,30 +1839,17 @@
     quer
-ies%5BqueryName%5D
+y
 .output.
@@ -1941,22 +1941,9 @@
 quer
-ies%5BqueryName%5D
+y
 .sor
@@ -1977,24 +1977,528 @@
 plit(',');%0A%0A
+  return query;%0A%7D%0A%0Afunction searchRequestBuilder(request) %7B%0A  var params = request.params;%0A  if (!params.tableName)%0A    throw new Error('no source is specified');%0A%0A  var queryParams = request.query;%0A%0A  var queries = %7B%7D;%0A  var queryName = params.tableName;%0A  try %7B%0A    queryName = inflection.tableize(queryName);%0A    queryName = inflection.camelize(queryName, true);%0A  %7D catch(error) %7B%0A    queryName = params.tableName.toLowerCase();%0A  %7D%0A  queries%5BqueryName%5D = buildQuery(params.tableName, queryParams);%0A%0A
   var messag

Extract as a function