checkbox_normalizer.js | |
---|---|
Hidden/Checkbox serialization of checkboxes, turn these into booleans. This is intended for use as a Connect/Express middleware. | var _ = require('underscore'); |
Any field whose name matches this will be assumed to be a checkbox, so
we'll normalize the matching Array, if any, into a | var RE_FLAGS = /(?:correct|exact|valid|[stv]ed|bled?)$/i; |
A recursive normalization function for HTTP params, coming either from the query string or request body. Recursive for complex param structures (nested objects, etc.). | function normalize(o) {
_.each(o, function(v, k) {
if (RE_FLAGS.test(k)) {
o[k] = normalizeEntry(v);
} else if (_.isObject(v)) {
normalize(v);
}
});
} |
Turns a single param, or Array param, into a | function normalizeEntry(v) {
return +_.last(_.flatten([v])) > 0;
} |
This is our middleware function proper. This operates on the query string params
( | module.exports = function normalizeCheckboxes(req, res, next) {
normalize(req.query);
normalize(req.body);
next();
};
|