Last active
December 16, 2015 19:59
-
-
Save johnsmith17th/5488780 to your computer and use it in GitHub Desktop.
Get filters of query.
This file contains hidden or 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
/** | |
* 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