Last active
January 4, 2016 20:19
-
-
Save noahjs/fc34a66296a3f63302bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
filter: function(options, expenses, cb){ | |
options = parseStringQuery(options); | |
}, | |
} | |
function runSearch(options, expenses, cb){ | |
var validExpenses = []; | |
async.eachSeries(filterOptions, function iterator(item, callback) { | |
if( runValidationSearchOnObject(options, expense) ){ | |
validExpenses.push( expense ); | |
} | |
}, function done() { | |
return cb(expenses); | |
}); | |
return options; | |
} | |
function runValidationSearchOnObject(options, expense){ | |
var keys = Object.keys(options); | |
for (var i = keys.length - 1; i >= 0; i--) { | |
var option = keys[i]; | |
var query = options[ keys[i] ]; | |
if( option == "lt" ){ | |
if( expense.created_at >= query ){ | |
return false | |
} | |
}else if( option == "gt" ){ | |
if( expense.created_at <= query ){ | |
return false | |
} | |
}else{ | |
if( typeof expense[ field ] == "undefined"){ | |
//debug("Error invalid Field on object: " + field) | |
}else if( expense[ field ] != query ){ | |
return false | |
} | |
} | |
// Negative matching complete | |
return true; | |
}; | |
} | |
function parseStringQuery(options, cb){ | |
options = options || {}; | |
if( options.string ){ | |
var string = options.string; | |
var filterOptions = [ | |
['lt:', 'lt'], | |
['gt:', 'gt'], | |
['category:', 'category_id'], | |
['account:', 'account_id'] | |
]; | |
async.eachSeries(filterOptions, function iterator(item, callback) { | |
parseForAttr(string, filterOptions[0], filterOptions[1], options, function(retString, retOptions){ | |
string = retString; | |
options = retOptions; | |
callback(); | |
}) | |
}, function done() { | |
if( trim(string).length > 0 ){ | |
options.search = string; | |
} | |
return cb(options); | |
}); | |
} | |
return options; | |
} | |
function parseForAttr(string, filter, key, options, cb){ | |
var pos = string.indexOf(filter); | |
if( pos !== -1 ){ | |
var val = string.substring( pos, string.indexOf(" ", pos) ); | |
string = string.replace(filter + val, ""); | |
options[ key ] = val; | |
} | |
return cb(string, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment