Skip to content

Instantly share code, notes, and snippets.

@johnsmith17th
Last active December 16, 2015 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsmith17th/5488780 to your computer and use it in GitHub Desktop.
Save johnsmith17th/5488780 to your computer and use it in GitHub Desktop.
Get filters of query.
/**
* Get filters of query.
*
* @param {HttpRequest} req
* @param {Object} [options]
* - {Boolean} [customizable]
* whether the filter is customizable
* i.e. can be pass from query string with the key $filter
* e.g. $filter=id,key,value
* - {Object} [allow]
* defined fields that can be selected
* e.g. { id:1, key:1, value:1 }
* - {Object} [defaults]
* defined default filter of query
* e.g. { key:1, value:1 }
* @returns {Object} selection part for query
*/
function __filter(req, options) {
var result = null,
opt = options || {};
// customizable
if (opt.customizable) {
result = utils.param(req, '$filter', function(value) {
return value ? utils.objectify(value.split(/[, ]+/)) : null;
});
// allow
if (result && opt.allow) {
for (var i in result) {
if (!opt.allow[i]) {
delete result[i];
};
};
};
};
// default
if (!result && opt.defaults) {
result = opt.defaults;
};
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment