teach API to slice modal times of days

Expand glob patterns to regexps, optimally, the fast prefix kind.
For @jasoncalabrese, @stavior, @kenstack and friends. :-)
This commit is contained in:
Ben West
2015-08-30 18:35:55 -07:00
parent 6cd5e72a0f
commit e143acddbf
+36
View File
@@ -3,6 +3,7 @@
var consts = require('../../constants');
var es = require('event-stream');
var sgvdata = require('sgvdata');
var expand = require('expand-braces');
var ID_PATTERN = /^[a-f\d]{24}$/;
function isId (value) {
@@ -245,6 +246,41 @@ function configure (app, wares, ctx) {
});
api.get('/echo/:echo/:model?/:spec?', echo_query);
function prep_patterns (req, res, next) {
var pattern = [ ];
if (req.params.prefix) {
pattern.push('^' + req.params.prefix);
}
if (req.params.regex) {
pattern.push('.*' + req.params.regex);
}
pattern = expand(pattern.join(''));
req.pattern = pattern;
var matches = pattern.map(function iter (pat) {
return new RegExp(pat);
});
var query = {
dateString: {
$in: matches
}
};
if (req.query.find) {
if (req.query.find.dateString) {
req.query.find.dateString.$in = query.dateString.$in;
} else {
req.query.find.dateString = query.dateString;
}
} else {
req.query.find = query;
}
next( );
}
api.get('/times/echo/:prefix?/:regex?', prep_patterns, function (req, res, next) {
res.json({ req: { params: req.params, query: req.query}, pattern: req.pattern});
});
api.get('/times/:prefix?/:regex?', prep_patterns, query_models, format_entries);
// Allow previewing your post content, just echos everything you
// posted back out.
api.post('/entries/preview', function (req, res, next) {