Last active
August 29, 2015 13:58
-
-
Save qcom/9943554 to your computer and use it in GitHub Desktop.
control flow confusion
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
/* map query string fields to search filters */ | |
var inexactFilters = { | |
date_added : function(queryVal, job, cb) { | |
cb(moment(job.date_added).format('MM-DD-YYYY') === queryVal); | |
}, | |
tags : function(queryVal, job, cb) { | |
console.log(cb.toString()); | |
app.get('db').tag.getBelongingToJob(job.job_id).then(function(tags) { | |
cb(queryVal.split(',').filter(function(tag) { return tags.map(function(tag) { return tag.tag_content; }).indexOf(tag) !== -1; }).length > 0); | |
}); | |
} | |
}; | |
/* filter the list of filter functions to only include those pertinent to the query string fields */ | |
function selectFilters(filters, query) { | |
var result = []; | |
for (var key in query) | |
if (key in filters) | |
result.push(filters[key].bind(null, key)); | |
return result; | |
} | |
/* if query string is present, filter out the job set */ | |
app.route('/search-jobs') | |
.all(middleware.restrict) | |
.all(middleware.onlyEmployers) | |
.get(function(req, res) { | |
var query = separateQuery(req.query); | |
if (Object.keys(query).length > 0) { | |
app.get('db').job.search(query.exact).then(function(jobs) { | |
console.log(jobs.length + ' jobs found'); | |
var filters = selectFilters(inexactFilters, query.inexact); | |
async.each(jobs, function(job, cb) { | |
async.applyEach(filters, job, function() { | |
}); | |
}); | |
console.log(jobs.length + ' jobs found post inexact filters'); | |
res.render('search-jobs'); | |
}); | |
} else { | |
res.render('search-jobs'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment